import React, { PureComponent } from 'react'; import type { Base16Theme } from 'react-base16-styling'; import createStyledComponent from '../utils/createStyledComponent'; import * as styles from './styles'; import Button from '../Button'; import Form from '../Form'; import { Props as FormProps } from '../Form/Form'; const DialogWrapper = createStyledComponent(styles); export interface DialogProps { open?: boolean; title?: string; children?: React.ReactNode; actions?: React.ReactNode[]; submitText?: string; fullWidth?: boolean; noHeader?: boolean; noFooter?: boolean; modal?: boolean; onDismiss: ( e: React.MouseEvent | false, ) => void; onSubmit: () => void; theme?: Base16Theme; } type Rest

= Omit< DialogProps & FormProps

, | 'modal' | 'open' | 'fullWidth' | 'title' | 'children' | 'actions' | 'noHeader' | 'noFooter' | 'submitText' | 'onDismiss' >; function isForm

(rest?: FormProps

): rest is FormProps

{ return (rest as FormProps

).schema !== undefined; } export default class Dialog

extends PureComponent< DialogProps | (Omit & FormProps

) > { submitButton?: HTMLInputElement | null; onSubmit = () => { if (this.submitButton) this.submitButton.click(); else (this.props.onSubmit as () => void)(); }; getFormButtonRef: React.RefCallback = (node) => { this.submitButton = node; }; onKeyDown: React.KeyboardEventHandler = (e) => { if (e.keyCode === 27 /* esc */) { e.preventDefault(); this.props.onDismiss(false); } }; render() { const { modal, open, fullWidth, title, children, actions, noHeader, noFooter, submitText, onDismiss, ...rest } = this.props; const schema = (rest as Rest

).schema; return (

{!noHeader && (
{schema ? schema.title || title : title}
{!modal && }
)}
{children} {isForm(rest as FormProps

) && (

)}> {!noFooter && ( )}
)}
{!noFooter && (actions ? (
{submitText ? [ ...actions, , ] : actions}
) : (
))}
); } }