// Copyright 2019-2020 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import * as React from 'react'; import classNames from 'classnames'; import { LocalizerType } from '../types/Util'; export type ActionSpec = { text: string; action: () => unknown; style?: 'affirmative' | 'negative'; }; export type OwnProps = { readonly actions: Array; readonly cancelText?: string; readonly children?: React.ReactNode; readonly i18n: LocalizerType; readonly onClose: () => unknown; readonly title?: string | React.ReactNode; }; export type Props = OwnProps; function focusRef(el: HTMLElement | null) { if (el) { el.focus(); } } export const ConfirmationDialog = React.memo( ({ i18n, onClose, cancelText, children, title, actions }: Props) => { React.useEffect(() => { const handler = ({ key }: KeyboardEvent) => { if (key === 'Escape') { onClose(); } }; document.addEventListener('keydown', handler); return () => { document.removeEventListener('keydown', handler); }; }, [onClose]); const handleCancel = React.useCallback( (e: React.MouseEvent) => { if (e.target === e.currentTarget) { onClose(); } }, [onClose] ); const handleAction = React.useCallback( (e: React.MouseEvent) => { onClose(); if (e.currentTarget.dataset.action) { const actionIndex = parseInt(e.currentTarget.dataset.action, 10); const { action } = actions[actionIndex]; action(); } }, [onClose, actions] ); return (
{title ? (

{title}

) : null}
{children}
{actions.length > 0 && (
{actions.map((action, i) => ( ))}
)}
); } );