Giter Site home page Giter Site logo

Comments (7)

marcin-piela avatar marcin-piela commented on May 27, 2024 37

I spent some time on that issue... cause special function is anti-pattern for me

and It's doable 🎉 , you just need to pass rest props to ie. Collapse.Panel (check source code of rc-panel to understand it)

export const CustomPanel: React.FC = ({ header, children, ...props }) => {
  // do whatever you like

  return <Collapse.Panel header={header} {...props}>
    {children}
  </Collapse.Panel>
};

and then use it like that:

<Collapse>
    <CustomPanel key="first" header="Header 1">Body</CustomPanel>
    <CustomPanel key="second" header="Header 2">Body</CustomPanel>
</Collapse>

from collapse.

benjycui avatar benjycui commented on May 27, 2024 27

Only Collapse.Panel can be the children of Collapse.

If you want to extract common logic, try:

function somePanelGenerator(props) {
  return <Collapse.Panel {...props} />
}

<Collapse>
  {somePanelGenerator(props)}
</Collapse>

from collapse.

DeepAnchor avatar DeepAnchor commented on May 27, 2024 5

If you're using typescript both hoc and Panel types are not typed correctly so you'll need to pass at least isActive, openProps and key props

Just to expand a little on this for the Typescript users out there:

If you want to wrap inner components like Collapse.Panel with custom behaviour, all you have to do is add CollapsePanelProps to the props definition of your wrapper component and then spread the props in the actual Panel component that is returned. i.e -

import {  CollapsePanelProps, Panel } from "antd"

type PanelWrapperProps = {
  // your panel wrapper props
} & CollapsePanelProps

const PanelWrapper = (props: PanelWrapperProps) => {
  return (
     <Panel {...props} />
  )
}

You can see what's happening under the hood here: The Collapse clones all of its direct children (which it expects to be Panel) and injects additional props into them. So all you need to do is allow any arbitrary direct child of Collapse to accept those props.

You can also override whichever props you want like isActive, just make sure you add it after the {...props} spread. This will allow you to have granular control over the Panel's behaviour. I.e -

const PanelWrapper = (props: PanelWrapperProps) => {
  const [state, setState] = useState<{isActive:boolean}>({isActive:false})
  return (
     <Panel {...props}  isActive={state.isActive} onItemClick={()=>{setState((prev) => !prev.isActive)}} />
  )
}

from collapse.

davidakerr avatar davidakerr commented on May 27, 2024 1

This would be a great addition to the docs.

from collapse.

masives avatar masives commented on May 27, 2024 1

If you're using typescript both hoc and Panel types are not typed correctly so you'll need to pass at least isActive, openProps and key props

from collapse.

homura avatar homura commented on May 27, 2024

@benjycui Thanks for you answer.

from collapse.

M-Umais-Hassan avatar M-Umais-Hassan commented on May 27, 2024

We have a prop name "forceRender" that we can pass inside the Panel component of Collapse, it will help to render content inside Panel without clicking on the header.

<Panel header="Some header name" key="1" forceRender>
   Content goes here
</Panel>

from collapse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.