I am trying to create a collapsable container component that could be reused for different parts of a form in order to show the fields or a summary of that section. In my case, each container would contain a Form object passed as a child to that container. The base of my code goes as follows :
function CollapsableContainer({ title, children }: CollapsableContainerProps) {
const [isOpen, setIsOpen] = useState(false)
return (
<div className="comp-collapsable-container">
{isOpen ? (
{children}
) : (
<>
<div className="header-section">
<Typography variant="h6">
{title}
</Typography>
<Button onClick={() => setIsOpen(true)} startIcon={<EditIcon />}>
Modify
</Button>
</div>
<div className="summary-section">
/* Summary here */
</div>
</>
)}
</div>
)
}
I am wondering if it's possible to pass setIsOpen to the children in order to allow the container to collapse once the user pressed a button that that child would contain instead of having to replicate that behaviour multiple times. (Basically without copy/pasting my code)
To give more context, a child would look something like this, where the setIsOpen would be added to the onSubmit function :
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={onSubmit}
>
<Form>
<FormikTextField
id="name"
name="name"
label="name"
fullWidth
required
/>
<FormikTextField
id="description"
name="description"
label="description"
fullWidth
required
/>
</Form>
</Formik>
Thank you for any help or suggestion!