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

90 lines
2.3 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-12-04 14:19:54 +03:00
import { CrossIcon } from '../Icons';
2023-07-25 20:27:29 +03:00
import Button from './Button';
2023-12-04 14:19:54 +03:00
import MiniButton from './MiniButton';
import Overlay from './Overlay';
2023-07-22 12:24:14 +03:00
2023-09-11 17:56:32 +03:00
export interface ModalProps {
2023-07-22 12:24:14 +03:00
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-12-07 01:21:27 +03:00
className?: string
2023-07-22 12:24:14 +03:00
}
2023-08-23 01:36:17 +03:00
function Modal({
title, hideWindow, onSubmit,
readonly, onCancel, canSubmit,
2023-12-07 01:21:27 +03:00
submitInvalidTooltip, className,
2023-08-23 01:36:17 +03:00
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-12-04 14:19:54 +03:00
return (
<>
2023-09-05 23:18:21 +03:00
<div className='fixed top-0 left-0 w-full h-full z-navigation clr-modal-backdrop' />
<div ref={ref}
2023-12-07 01:21:27 +03:00
className='fixed -translate-x-1/2 translate-y-1/2 border shadow-md bottom-1/2 left-1/2 z-modal clr-app'
2023-08-08 23:04:21 +03:00
>
2023-12-07 01:21:27 +03:00
<Overlay position='right-[0.3rem] top-2' className='text-disabled'>
2023-12-04 14:19:54 +03:00
<MiniButton
tooltip='Закрыть диалоговое окно [ESC]'
icon={<CrossIcon size={5}/>}
onClick={handleCancel}
/>
</Overlay>
2023-12-04 14:19:54 +03:00
2023-12-07 01:21:27 +03:00
{title ? <h1 className='px-12 py-2 text-lg select-none'>{title}</h1> : null}
2023-12-04 14:19:54 +03:00
2023-12-07 01:21:27 +03:00
<div
className={`w-full h-fit ${className}`}
style={{
maxHeight: 'calc(100vh - 8rem)',
maxWidth: 'calc(100vw - 2rem)',
overflow: 'auto'
}}
>
2023-07-22 12:24:14 +03:00
{children}
</div>
2023-12-04 14:19:54 +03:00
2023-12-07 01:21:27 +03:00
<div className='flex justify-center w-full gap-12 px-6 py-3 z-modal-controls min-w-fit'>
{!readonly ?
<Button autoFocus
2023-07-22 12:24:14 +03:00
text={submitText}
2023-08-23 01:36:17 +03:00
tooltip={!canSubmit ? submitInvalidTooltip: ''}
2023-12-07 01:21:27 +03:00
dimensions='min-w-[8rem] min-h-[2.6rem]'
2023-11-05 16:31:49 +03:00
colors='clr-btn-primary'
2023-07-22 12:24:14 +03:00
disabled={!canSubmit}
onClick={handleSubmit}
/> : null}
2023-07-25 20:27:29 +03:00
<Button
2023-08-08 23:04:21 +03:00
text={readonly ? 'Закрыть' : 'Отмена'}
2023-12-07 01:21:27 +03:00
dimensions='min-w-[8rem] min-h-[2.6rem]'
2023-07-22 12:24:14 +03:00
onClick={handleCancel}
/>
</div>
</div>
2023-12-02 00:15:56 +03:00
</>);
2023-07-22 12:24:14 +03:00
}
2023-07-25 20:27:29 +03:00
export default Modal;