ente/src/components/MessageDialog.tsx

110 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-04-05 11:59:22 +00:00
import React from 'react';
2021-05-29 06:27:52 +00:00
import {Button, Modal} from 'react-bootstrap';
2021-04-05 12:17:33 +00:00
import constants from 'utils/strings/constants';
2021-04-05 11:59:22 +00:00
export interface MessageAttributes {
title?: string;
staticBackdrop?: boolean;
2021-05-11 07:36:14 +00:00
nonClosable?: boolean;
content?: any;
close?: { text?: string; variant?: string };
proceed?: {
text: string;
action: () => void;
2021-04-25 15:35:43 +00:00
variant: string;
disabled?: boolean;
};
}
export type SetDialogMessage = React.Dispatch<
React.SetStateAction<MessageAttributes>
>;
2021-04-19 17:29:03 +00:00
type Props = React.PropsWithChildren<{
2021-04-05 11:59:22 +00:00
show: boolean;
onHide: () => void;
2021-04-07 09:21:44 +00:00
attributes: MessageAttributes;
2021-04-26 04:21:14 +00:00
size?: 'sm' | 'lg' | 'xl';
2021-04-19 17:29:03 +00:00
}>;
2021-04-07 09:21:44 +00:00
export default function MessageDialog({
attributes,
children,
...props
}: Props) {
2021-04-20 10:57:25 +00:00
if (!attributes) {
return <Modal />;
}
2021-04-05 11:59:22 +00:00
return (
<Modal
2021-04-26 04:21:14 +00:00
{...props}
2021-05-11 07:36:14 +00:00
onHide={attributes.nonClosable ? () => null : props.onHide}
centered
2021-04-20 10:57:25 +00:00
backdrop={attributes.staticBackdrop ? 'static' : 'true'}
>
2021-05-11 07:36:14 +00:00
<Modal.Header
2021-05-29 06:27:52 +00:00
style={{borderBottom: 'none'}}
2021-05-11 07:36:14 +00:00
closeButton={!attributes.nonClosable}
>
2021-04-20 10:57:25 +00:00
{attributes.title && (
2021-04-05 11:59:22 +00:00
<Modal.Title>
<strong>{attributes.title}</strong>
</Modal.Title>
)}
</Modal.Header>
2021-04-19 17:29:03 +00:00
{(children || attributes?.content) && (
2021-05-29 06:27:52 +00:00
<Modal.Body style={{borderTop: '1px solid #444'}}>
{children || <h5>{attributes.content}</h5>}
2021-04-19 11:21:46 +00:00
</Modal.Body>
)}
{(attributes.close || attributes.proceed) && (
2021-05-29 06:27:52 +00:00
<Modal.Footer style={{borderTop: 'none'}}>
<div
style={{
display: 'flex',
flexWrap: 'wrap',
}}
>
{attributes.close && (
<Button
variant={`outline-${
attributes.close?.variant ?? 'secondary'
}`}
onClick={props.onHide}
style={{
padding: '6px 3em',
margin: '0 20px',
marginBottom: '20px',
flex: 1,
whiteSpace: 'nowrap',
}}
>
{attributes.close?.text ?? constants.OK}
</Button>
)}
{attributes.proceed && (
<Button
variant={`outline-${
attributes.proceed?.variant ?? 'primary'
}`}
onClick={() => {
attributes.proceed.action();
props.onHide();
}}
style={{
padding: '6px 3em',
margin: '0 20px',
marginBottom: '20px',
flex: 1,
whiteSpace: 'nowrap',
}}
disabled={attributes.proceed.disabled}
>
{attributes.proceed.text}
</Button>
)}
</div>
</Modal.Footer>
)}
2021-04-05 11:59:22 +00:00
</Modal>
);
}