2023-07-25 20:27:29 +03:00
|
|
|
import { useRef } from 'react';
|
|
|
|
|
|
|
|
import useClickedOutside from '../../hooks/useClickedOutside';
|
|
|
|
import Button from './Button';
|
2023-07-22 12:24:14 +03:00
|
|
|
|
|
|
|
interface ModalProps {
|
|
|
|
title?: string
|
|
|
|
submitText?: string
|
|
|
|
show: boolean
|
|
|
|
canSubmit: boolean
|
|
|
|
toggle: () => void
|
|
|
|
onSubmit: () => void
|
|
|
|
onCancel?: () => void
|
|
|
|
children: React.ReactNode
|
|
|
|
}
|
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
function Modal({ title, show, toggle, onSubmit, onCancel, canSubmit, children, submitText = 'Продолжить' }: ModalProps) {
|
2023-07-22 12:24:14 +03:00
|
|
|
const ref = useRef(null);
|
2023-07-25 20:27:29 +03:00
|
|
|
useClickedOutside({ ref, callback: toggle })
|
2023-07-22 12:24:14 +03:00
|
|
|
|
|
|
|
if (!show) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleCancel = () => {
|
|
|
|
toggle();
|
2023-07-25 20:27:29 +03:00
|
|
|
if (onCancel) onCancel();
|
2023-07-22 12:24:14 +03:00
|
|
|
};
|
|
|
|
|
2023-07-23 15:23:01 +03:00
|
|
|
const handleSubmit = () => {
|
|
|
|
toggle();
|
|
|
|
onSubmit();
|
|
|
|
};
|
|
|
|
|
2023-07-22 12:24:14 +03:00
|
|
|
return (
|
|
|
|
<>
|
2023-07-25 20:27:29 +03:00
|
|
|
<div className='fixed top-0 left-0 z-50 w-full h-full opacity-50 clr-modal'>
|
2023-07-22 12:24:14 +03:00
|
|
|
</div>
|
|
|
|
<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 z-[60] clr-card border shadow-md'>
|
|
|
|
{ title && <h1 className='mb-4 text-xl font-bold'>{title}</h1> }
|
|
|
|
<div className='py-2'>
|
|
|
|
{children}
|
|
|
|
</div>
|
2023-07-25 20:27:29 +03:00
|
|
|
<div className='flex justify-between w-full pt-4 mt-2 border-t-4'>
|
2023-07-22 12:24:14 +03:00
|
|
|
<Button
|
|
|
|
text={submitText}
|
|
|
|
widthClass='min-w-[6rem] w-fit h-fit'
|
|
|
|
colorClass='clr-btn-primary'
|
|
|
|
disabled={!canSubmit}
|
2023-07-23 15:23:01 +03:00
|
|
|
onClick={handleSubmit}
|
2023-07-22 12:24:14 +03:00
|
|
|
/>
|
2023-07-25 20:27:29 +03:00
|
|
|
<Button
|
2023-07-22 12:24:14 +03:00
|
|
|
text='Отмена'
|
|
|
|
onClick={handleCancel}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-25 20:27:29 +03:00
|
|
|
export default Modal;
|