ConceptPortal-public/rsconcept/frontend/src/components/Common/Modal.tsx

72 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-07-25 20:27:29 +03:00
import { useRef } from 'react';
2023-07-25 22:29:33 +03:00
import useEscapeKey from '../../hooks/useEscapeKey';
2023-07-25 20:27:29 +03:00
import Button from './Button';
2023-07-22 12:24:14 +03:00
interface ModalProps {
title?: string
submitText?: string
2023-08-23 01:36:17 +03:00
submitInvalidTooltip?: string
2023-08-08 23:04:21 +03:00
readonly?: boolean
2023-07-29 03:31:21 +03:00
canSubmit?: boolean
2023-07-25 22:29:33 +03:00
hideWindow: () => void
2023-08-08 23:04:21 +03:00
onSubmit?: () => void
2023-07-22 12:24:14 +03:00
onCancel?: () => void
children: React.ReactNode
}
2023-08-23 01:36:17 +03:00
function Modal({
title, hideWindow, onSubmit,
readonly, onCancel, canSubmit,
submitInvalidTooltip,
children,
submitText = 'Продолжить'
}: ModalProps) {
2023-07-22 12:24:14 +03:00
const ref = useRef(null);
2023-07-25 22:29:33 +03:00
useEscapeKey(hideWindow);
2023-07-22 12:24:14 +03:00
const handleCancel = () => {
2023-07-25 22:29:33 +03:00
hideWindow();
2023-07-25 20:27:29 +03:00
if (onCancel) onCancel();
2023-07-22 12:24:14 +03:00
};
const handleSubmit = () => {
2023-07-25 22:29:33 +03:00
hideWindow();
2023-08-08 23:04:21 +03:00
if (onSubmit) onSubmit();
};
2023-07-22 12:24:14 +03:00
return (
<>
2023-08-23 01:36:17 +03:00
<div className='fixed top-0 left-0 z-50 w-full h-full opacity-50 clr-modal' />
2023-08-08 23:04:21 +03:00
<div
ref={ref}
className='fixed bottom-1/2 left-1/2 translate-y-1/2 -translate-x-1/2 px-6 py-4 flex flex-col w-fit max-w-[95vw] h-fit z-[60] clr-card border shadow-md mb-[5rem]'
>
{ title && <h1 className='mb-2 text-xl font-bold text-center'>{title}</h1> }
2023-08-23 01:36:17 +03:00
<div className='max-h-[calc(95vh-15rem)]'>
2023-07-22 12:24:14 +03:00
{children}
</div>
2023-08-08 23:04:21 +03:00
<div className='flex justify-center w-full gap-4 pt-4 mt-2 border-t-4'>
2023-08-23 01:36:17 +03:00
{!readonly &&
<Button
2023-07-22 12:24:14 +03:00
text={submitText}
2023-08-23 01:36:17 +03:00
tooltip={!canSubmit ? submitInvalidTooltip: ''}
2023-07-28 01:37:26 +03:00
widthClass='min-w-[6rem] min-h-[2.6rem] w-fit h-fit'
2023-07-22 12:24:14 +03:00
colorClass='clr-btn-primary'
disabled={!canSubmit}
onClick={handleSubmit}
2023-07-25 22:29:33 +03:00
autoFocus
2023-08-08 23:04:21 +03:00
/>}
2023-07-25 20:27:29 +03:00
<Button
2023-08-08 23:04:21 +03:00
text={readonly ? 'Закрыть' : 'Отмена'}
2023-07-28 01:37:26 +03:00
widthClass='min-w-[6rem] min-h-[2.6rem] w-fit h-fit'
2023-07-22 12:24:14 +03:00
onClick={handleCancel}
/>
</div>
</div>
</>
);
}
2023-07-25 20:27:29 +03:00
export default Modal;