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

89 lines
2.7 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';
import { resolveWidgetFieldValue, resolveReferences } from '../../_helpers/utils';
2021-05-09 18:06:11 +00:00
export const Modal = function Modal({
id,
component,
height,
2021-05-10 11:36:33 +00:00
mode,
containerProps,
currentState
2021-05-09 18:06:11 +00:00
}) {
const [show, showModal] = useState(false);
const parentRef = useRef(null);
const titleProp = component.definition.properties.title;
const title = titleProp ? titleProp.value : '';
const sizeProp = component.definition.properties.size;
const size = sizeProp ? sizeProp.value : 'lg';
const disabledState = component.definition.styles?.disabledState?.value ?? false;
const parsedDisabledState = typeof disabledState !== 'boolean' ? resolveWidgetFieldValue(disabledState, currentState) : disabledState;
2021-05-09 18:06:11 +00:00
useEffect(() => {
const componentState = containerProps.currentState.components[component.name];
const canShowModel = componentState ? componentState.show : false;
showModal(canShowModel);
}, [containerProps.currentState.components[component.name]]);
function hideModal() {
containerProps.onComponentOptionChanged(component, 'show', false);
showModal(false);
}
return (
<div data-disabled={parsedDisabledState}>
<BootstrapModal
2021-05-09 18:06:11 +00:00
contentClassName="modal-component"
show={show}
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={() => showModal(false)}
>
2021-05-10 14:31:14 +00:00
{containerProps.mode === 'edit' &&
<ConfigHandle
id={id}
component={component}
configHandleClicked={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="light" size="sm" onClick={hideModal}>
x
</Button>
</div>
</BootstrapModal.Header>
<BootstrapModal.Body style={{ height }} ref={parentRef}>
2021-05-09 18:06:11 +00:00
<SubContainer
parent={id}
{...containerProps}
parentRef={parentRef}
/>
<SubCustomDragLayer
snapToGrid={true}
2021-05-09 18:06:11 +00:00
parentRef={parentRef}
parent={id}
currentLayout={containerProps.currentLayout}
2021-05-09 18:06:11 +00:00
/>
</BootstrapModal.Body>
</BootstrapModal>
</div>
);
};