ToolJet/frontend/src/Editor/Components/Modal.jsx

76 lines
2.2 KiB
React
Raw Normal View History

2021-05-09 18:06:11 +00:00
import React, { useRef, useState, useEffect } from 'react';
import { default as BootstrapModal } from 'react-bootstrap/Modal';
2021-05-09 18:06:11 +00:00
import Button from 'react-bootstrap/Button';
import { SubCustomDragLayer } from '../SubCustomDragLayer';
import { SubContainer } from '../SubContainer';
2021-05-10 13:44:58 +00:00
import { ConfigHandle } from '../ConfigHandle';
2021-05-09 18:06:11 +00:00
export const Modal = function Modal({
id,
component,
height,
containerProps,
darkMode,
properties,
styles,
exposedVariables,
setExposedVariable,
}) {
const [showModal, setShowModal] = useState(false);
2021-05-09 18:06:11 +00:00
const parentRef = useRef(null);
const title = properties.title ?? '';
const size = properties.size ?? 'lg';
const { disabledState } = styles;
2021-05-09 18:06:11 +00:00
useEffect(() => {
const canShowModal = exposedVariables.show ?? false;
setShowModal(canShowModal);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [exposedVariables.show]);
2021-05-09 18:06:11 +00:00
function hideModal() {
setExposedVariable('show', false);
setShowModal(false);
2021-05-09 18:06:11 +00:00
}
return (
<div data-disabled={disabledState}>
<BootstrapModal
2021-05-09 18:06:11 +00:00
contentClassName="modal-component"
show={showModal}
container={document.getElementsByClassName('canvas-area')[0]}
size={size}
backdrop={true}
keyboard={true}
enforceFocus={false}
2021-05-09 18:06:11 +00:00
animation={false}
onEscapeKeyDown={() => hideModal()}
2021-05-09 18:06:11 +00:00
>
{containerProps.mode === 'edit' && (
<ConfigHandle id={id} component={component} setSelectedComponent={containerProps.onComponentClick} />
)}
2021-05-09 18:06:11 +00:00
<BootstrapModal.Header>
<BootstrapModal.Title>{title}</BootstrapModal.Title>
2021-05-09 18:06:11 +00:00
<div>
<Button variant={darkMode ? 'secondary' : 'light'} size="sm" onClick={hideModal}>
2021-05-09 18:06:11 +00:00
x
</Button>
</div>
</BootstrapModal.Header>
2021-11-25 08:47:18 +00:00
<BootstrapModal.Body style={{ height }} ref={parentRef} id={id}>
<SubContainer parent={id} {...containerProps} parentRef={parentRef} />
<SubCustomDragLayer
snapToGrid={true}
parentRef={parentRef}
parent={id}
currentLayout={containerProps.currentLayout}
/>
2021-05-09 18:06:11 +00:00
</BootstrapModal.Body>
</BootstrapModal>
</div>
);
};