import React, { useEffect } from 'react'; import PropTypes from 'prop-types'; import { CFormGroup, CCol, CLabel, CButton, CModal, CModalHeader, CModalTitle, CModalBody, CPopover, CDataTable, } from '@coreui/react'; import CIcon from '@coreui/icons-react'; import { cilMinus, cilPlus, cilSave, cilX } from '@coreui/icons'; import useToggle from '../../../hooks/useToggle'; const CustomMultiModal = ({ children, t, columns, label, value, updateValue, save, firstCol, secondCol, length, modalSize, itemName, noTable, toggleAdd, reset, }) => { const [show, toggle] = useToggle(); const getLabel = () => { if (length === 0) return `Manage ${itemName}`; return `Manage ${itemName} (${length})`; }; const remove = (v) => { const key = Object.keys(v)[0]; const index = value.findIndex((obj) => obj[key] === v[key]); if (index > -1) { const newList = [...value]; newList.splice(index, 1); updateValue(newList); } }; const closeAndSave = () => { save(); toggle(); }; useEffect(() => { if (show && reset !== null) reset(); }, [show]); return ( {label} {getLabel()} {label}
{children} {!noTable ? ( ( remove(item)} > ), }} /> ) : null}
); }; CustomMultiModal.propTypes = { children: PropTypes.node.isRequired, t: PropTypes.func.isRequired, columns: PropTypes.instanceOf(Array).isRequired, value: PropTypes.instanceOf(Array).isRequired, updateValue: PropTypes.func.isRequired, save: PropTypes.func.isRequired, label: PropTypes.string.isRequired, firstCol: PropTypes.string, secondCol: PropTypes.string, length: PropTypes.number.isRequired, modalSize: PropTypes.string, itemName: PropTypes.string.isRequired, noTable: PropTypes.bool, toggleAdd: PropTypes.func, reset: PropTypes.func, }; CustomMultiModal.defaultProps = { firstCol: 6, secondCol: 6, modalSize: 'lg', noTable: false, toggleAdd: null, reset: null, }; export default CustomMultiModal;