import React, { forwardRef, useImperativeHandle, useRef, useState } from 'react' import {Button, Card, CardBody, CardHeader, Modal} from 'reactstrap'; import cx from "classnames" import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faRemove } from '@fortawesome/free-solid-svg-icons'; import "../../assets/styles/utils/modal.scss"; const index = forwardRef( //forwardRef allows parent components to access the function of child via ref, i.e forwards its ref to the child ( { containerClass, children, title, showButtons = false, showCloseButton = false, modalType = "md", onCloseModal= () => {} }, //props ref ) => { const [ showModal, setShowModal ] = useState(false) const [ modalData, setModalData ] = useState({}) const innerRef = useRef() const show = () => { setShowModal(true) } const hide = () => { setShowModal(false); onCloseModal(); } const getData = () => { return modalData } const setData = (data) => { setModalData(data) } useImperativeHandle(ref, () => ({ //imperativeHandle exposes listed functions to the ref, so that they can be used by the parent show, hide, getData, setData })) //adding modal props to child component const childrenWithProps = React.Children.map(children, child => React.cloneElement(child, {data: modalData, closeModal: hide}) ); return (
{title && {title}} {showCloseButton && }
{showModal? childrenWithProps : ""} {showButtons &&
}
) }) export default index