I'm using a react-table library and when a user clicks on an expand icon I want it to open the table in a modal using (react-bootstrap modal). I need the modal to be generic and each time will get a different graph component or even just regular content. I'm wondering if this is the best practice to implement this feature. I know that it's not recommended to use React ref and according to react docs a dialog is not a good place to use this strategy. But I can't think of other options which will let me pass dynamically the graph component or any other component. Would love to hear other options.
This is my modal component:
const SiteModal = React.forwardRef((props, ref) => {
return (
<Modal {...props} size="lg" aria-labelledby="contained-modal-title-vcenter" centered ref={ref}
dialogClassName={classes.modalContainer}>
<Modal.Header closeButton>
<Modal.Title id="contained-modal-title-vcenter">
Modal heading
</Modal.Title>
</Modal.Header>
<Modal.Body>
<h4>{props.children}</h4>
</Modal.Body>
</Modal>
);
});
This is my parent component which is also a ref element:
const CollapseEelemet = React.forwardRef((props, ref) => {
const [open, setOpen] = useState(true);
const [modalShow, setModalShow] = useState(false);
return (
<div className={classes.collapseWrapper}>
<div onClick={() => setOpen(!open)} className={classes.collapseTitle} aria-controls={props.id}
aria-expanded={open}>
{props.title}
<span onClick={() => setModalShow(true)} className="icon-launch"></span>
**<SiteModal show={modalShow} onHide={() => setModalShow(false)}>
{props.children}
</SiteModal>**
</div>
<Collapse in={true}>
<div id={props.id} ref={ref}>
{props.children}
</div>
</Collapse>
</div>
);
});
And this is my top parent:
const Cost = (props) => {
const renderMainGraph = () => {
const ref = React.createRef();
return (
<div className={classes.mainChartWidget}>
<CollapseEelemet ref={ref} id={constant.mainGraph.id} title={constant.mainGraph.title}>
<LineChartFusion />
<GroupColumnChart />
</CollapseEelemet >
</div>
);
};
return (
<div>{renderMainGraph ()}</div>
);
}