aws-northstar
Version:
NorthStar Design System
69 lines (65 loc) • 4.46 kB
JavaScript
/** *******************************************************************************************************************
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. *
******************************************************************************************************************** */
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import Inline from '../../layouts/Inline';
import Stack from '../../layouts/Stack';
import Box from '../../layouts/Box';
import Modal from '../../components/Modal';
import FormField from '../../components/FormField';
import Input from '../../components/Input';
import Button from '../../components/Button';
/**
* 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 = (_a) => {
var { visible, onDeleteClicked, onCancelClicked, loading = false, enabled = true, title, children, deleteButtonText } = _a, props = __rest(_a, ["visible", "onDeleteClicked", "onCancelClicked", "loading", "enabled", "title", "children", "deleteButtonText"]);
const [confirmation, setConfirmation] = useState('');
const [isMatched, setIsMatched] = useState(props.variant === 'confirmation');
const confirmationText = (props.variant !== 'confirmation' && props.confirmationText) || 'delete';
const handleDelete = useCallback(() => {
setConfirmation('');
onDeleteClicked === null || onDeleteClicked === void 0 ? void 0 : onDeleteClicked();
}, [onDeleteClicked]);
const handleCancel = useCallback(() => {
setConfirmation('');
onCancelClicked === null || onCancelClicked === void 0 ? void 0 : onCancelClicked();
}, [onCancelClicked]);
const actions = useMemo(() => (React.createElement(Box, { display: "flex", width: "100%", justifyContent: "flex-end" },
React.createElement(Inline, null,
React.createElement(Button, { label: "close", onClick: handleCancel }, "Cancel"),
React.createElement(Button, { label: "delete", variant: "primary", disabled: !enabled || !isMatched || loading, loading: loading, onClick: handleDelete }, deleteButtonText || 'Delete')))), [deleteButtonText, handleCancel, handleDelete, loading, isMatched, enabled]);
useEffect(() => {
setIsMatched(props.variant === 'confirmation' || confirmationText === confirmation);
}, [setIsMatched, confirmation, confirmationText, props.variant]);
return (React.createElement(Modal, { visible: visible, title: title, footer: actions, onClose: handleCancel },
React.createElement(Stack, null,
children,
props.variant !== 'confirmation' && (React.createElement(FormField, { label: props.label || (React.createElement(React.Fragment, null,
"To confirm deletion, type ",
React.createElement("i", null, "delete"),
" below")), controlId: "confirmation", hintText: props.hintText },
React.createElement(Input, { type: "text", placeholder: props.placeholderText || confirmationText, value: confirmation, onChange: setConfirmation }))))));
};
export default DeleteConfirmationDialog;