UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

88 lines (87 loc) 4 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["text", "children", "onClick", "className", "hidden", "showConfirmDialog"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import React, { useCallback, useContext } from 'react'; import classnames from 'classnames'; import { Button, Dialog } from '../../../../components'; import useTranslation from '../../hooks/useTranslation'; import IterateItemContext from '../IterateItemContext'; import ToolbarContext from '../Toolbar/ToolbarContext'; import FieldBoundaryContext from '../../DataContext/FieldBoundary/FieldBoundaryContext'; import { reset } from '../../../../icons'; import useHasContentChanged from '../../Form/Isolation/useHasContentChanged'; import { omitDataValueReadWriteProps } from '../../types'; export default function ResetButton(props) { const { text, children, onClick, className, hidden, showConfirmDialog = true } = props, restProps = _objectWithoutProperties(props, _excluded); const { restoreOriginalValue } = useContext(IterateItemContext) || {}; const { setShowBoundaryErrors } = useContext(FieldBoundaryContext) || {}; const { setShowError } = useContext(ToolbarContext) || {}; const { hasContentChanged } = useHasContentChanged({ enabled: true }); const buttonProps = omitDataValueReadWriteProps(restProps); const { resetButton } = useTranslation().IterateEditContainer; const { confirmResetText } = useTranslation().IterateEditContainer; const textContent = text || children || resetButton; const buttonWrapperRef = React.useRef(null); const handleClick = useCallback(({ close, event }) => { var _buttonWrapperRef$cur; close === null || close === void 0 ? void 0 : close(); onClick === null || onClick === void 0 ? void 0 : onClick(event); restoreOriginalValue === null || restoreOriginalValue === void 0 ? void 0 : restoreOriginalValue(); setShowError(false); setShowBoundaryErrors === null || setShowBoundaryErrors === void 0 ? void 0 : setShowBoundaryErrors(false); (_buttonWrapperRef$cur = buttonWrapperRef.current) === null || _buttonWrapperRef$cur === void 0 ? void 0 : _buttonWrapperRef$cur.focus(); }, [onClick, restoreOriginalValue, setShowBoundaryErrors, setShowError]); if (hidden) { return null; } const triggerAttributes = _objectSpread({ className: classnames('dnb-forms-iterate__reset-button', className), text: textContent, variant: textContent ? 'tertiary' : 'secondary', icon: reset, icon_position: 'left', disabled: typeof hasContentChanged === 'boolean' ? !hasContentChanged : false }, buttonProps); return React.createElement("span", { tabIndex: -1, ref: buttonWrapperRef, className: "dnb-no-focus" }, showConfirmDialog ? React.createElement(Dialog, { variant: "confirmation", title: confirmResetText, triggerAttributes: triggerAttributes, onConfirm: handleClick }) : React.createElement(Button, _extends({}, triggerAttributes, { on_click: handleClick }, buttonProps), resetButton)); } //# sourceMappingURL=ResetButton.js.map