UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

100 lines (99 loc) 4.29 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", "showWhen"]; 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 FieldBoundaryContext from '../../DataContext/FieldBoundary/FieldBoundaryContext'; import { reset } from '../../../../icons'; import useHasContentChanged from './useHasContentChanged'; import { omitDataValueReadWriteProps } from '../../types'; import useDataContextSnapshot from './useDataContextSnapshot'; import useHandleStatus from './useHandleStatus'; import { isolationError } from './IsolatedContainer'; import IsolationContext from './IsolationContext'; export default function IsolationResetButton(props) { const { text, children, onClick, className, hidden, showConfirmDialog = true, showWhen } = props, restProps = _objectWithoutProperties(props, _excluded); const { outerContext, preventUncommittedChanges } = useContext(IsolationContext); const { setShowBoundaryErrors } = useContext(FieldBoundaryContext) || {}; const { handleReset } = useDataContextSnapshot({ enabled: true }); const { hasContentChanged } = useHasContentChanged(); const { showStatus: showCommitStatus } = useHandleStatus({ outerContext, preventUncommittedChanges, error: isolationError, name: 'isolation-reset-button' }); 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); handleReset(); setShowBoundaryErrors === null || setShowBoundaryErrors === void 0 ? void 0 : setShowBoundaryErrors(false); (_buttonWrapperRef$cur = buttonWrapperRef.current) === null || _buttonWrapperRef$cur === void 0 ? void 0 : _buttonWrapperRef$cur.focus(); }, [onClick, handleReset, setShowBoundaryErrors]); if (hidden) { return null; } const triggerAttributes = _objectSpread({ className: classnames('dnb-forms-isolate__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", hidden: !(showWhen === 'uncommittedChangeDetected' ? Boolean(showCommitStatus) : true) }, showConfirmDialog ? React.createElement(Dialog, { variant: "confirmation", title: confirmResetText, triggerAttributes: triggerAttributes, onConfirm: handleClick }) : React.createElement(Button, _extends({}, triggerAttributes, { on_click: handleClick }, buttonProps), resetButton)); } //# sourceMappingURL=IsolationResetButton.js.map