UNPKG

@aws-northstar/ui

Version:
47 lines (43 loc) 3.73 kB
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime"; /** ******************************************************************************************************************* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. * ******************************************************************************************************************** */ import { useCallback, useEffect, useMemo, useState } from 'react'; import SpaceBetween from '@cloudscape-design/components/space-between'; import Modal from '@cloudscape-design/components/modal'; import FormField from '@cloudscape-design/components/form-field'; import Input from '@cloudscape-design/components/input'; import Button from '@cloudscape-design/components/button'; import Box from '@cloudscape-design/components/box'; /** * A model dialog used to verify users truly intend to perform deletion or some kind of destructive action. <br/> * When deleting resources, you can choose between different levels of friction: <b>friction</b> or <b>confirmation</b>. * */ const DeleteConfirmationDialog = ({ visible = false, onDeleteClicked, onCancelClicked, loading = false, enabled = true, title, children, deleteButtonText, ...props }) => { const [confirmation, setConfirmation] = useState(''); const [isMatched, setIsMatched] = useState(props.variant === 'confirmation'); const confirmationText = (props.variant !== 'confirmation' && props.confirmationText) || 'delete'; const handleDelete = useCallback(() => { setConfirmation(''); onDeleteClicked?.(); }, [onDeleteClicked]); const handleCancel = useCallback(() => { setConfirmation(''); onCancelClicked?.(); }, [onCancelClicked]); const actions = useMemo(() => (_jsx(Box, { float: "right", children: _jsxs(SpaceBetween, { direction: "horizontal", size: "xs", children: [_jsx(Button, { ariaLabel: "close", onClick: handleCancel, children: "Cancel" }), _jsx(Button, { ariaLabel: "delete", variant: "primary", disabled: !enabled || !isMatched || loading, loading: loading, onClick: handleDelete, children: deleteButtonText || 'Delete' })] }) })), [deleteButtonText, handleCancel, handleDelete, loading, isMatched, enabled]); useEffect(() => { setIsMatched(props.variant === 'confirmation' || confirmationText === confirmation); }, [setIsMatched, confirmation, confirmationText, props.variant]); return (_jsx(Modal, { visible: visible, header: title, footer: actions, onDismiss: handleCancel, ...props, children: _jsxs(SpaceBetween, { direction: "vertical", size: "s", children: [children, props.variant !== 'confirmation' && (_jsx(FormField, { label: props.label || (_jsxs(_Fragment, { children: ["To confirm deletion, type ", _jsx("i", { children: "delete" }), " below"] })), controlId: "confirmation", constraintText: props.hintText, children: _jsx(Input, { type: "text", placeholder: props.placeholderText || confirmationText, value: confirmation, onChange: ({ detail }) => setConfirmation(detail.value) }) }))] }) })); }; export default DeleteConfirmationDialog;