UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

80 lines (79 loc) 3.12 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = DoneButton; var _react = require("react"); var _clsx = _interopRequireDefault(require("clsx")); var _index = require("../../../../components/index.js"); var _useTranslation = _interopRequireDefault(require("../../hooks/useTranslation.js")); var _IterateItemContext = _interopRequireDefault(require("../IterateItemContext.js")); var _ToolbarContext = _interopRequireDefault(require("../Toolbar/ToolbarContext.js")); var _FieldBoundaryContext = _interopRequireDefault(require("../../DataContext/FieldBoundary/FieldBoundaryContext.js")); var _PushContainerContext = _interopRequireDefault(require("../PushContainer/PushContainerContext.js")); var _index2 = require("../../../../icons/index.js"); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function DoneButton(props) { const { className, ...restProps } = props; const { switchContainerMode, containerMode, arrayValue, index } = (0, _react.useContext)(_IterateItemContext.default) || {}; const { hasError, hasVisibleError, setShowBoundaryErrors } = (0, _react.useContext)(_FieldBoundaryContext.default) || {}; const { commitHandleRef } = (0, _react.useContext)(_PushContainerContext.default) || {}; const { setShowError } = (0, _react.useContext)(_ToolbarContext.default) || {}; const { doneButton } = (0, _useTranslation.default)().IterateEditContainer; const valueBackupRef = (0, _react.useRef)(undefined); (0, _react.useEffect)(() => { if (containerMode === 'edit' && !valueBackupRef.current) { valueBackupRef.current = arrayValue === null || arrayValue === void 0 ? void 0 : arrayValue[index]; } if (containerMode === 'view') { valueBackupRef.current = null; } }, [arrayValue, containerMode, index]); const doneHandler = (0, _react.useCallback)(() => { if (hasError) { setShowBoundaryErrors === null || setShowBoundaryErrors === void 0 || setShowBoundaryErrors(true); if (hasVisibleError) { setShowError(true); } } else { setShowBoundaryErrors === null || setShowBoundaryErrors === void 0 || setShowBoundaryErrors(false); setShowError(false); if (commitHandleRef) { var _commitHandleRef$curr; (_commitHandleRef$curr = commitHandleRef.current) === null || _commitHandleRef$curr === void 0 || _commitHandleRef$curr.call(commitHandleRef); } else { switchContainerMode === null || switchContainerMode === void 0 || switchContainerMode('view'); } } }, [commitHandleRef, hasError, hasVisibleError, setShowBoundaryErrors, setShowError, switchContainerMode]); return (0, _jsxRuntime.jsx)(_index.Button, { variant: "tertiary", className: (0, _clsx.default)('dnb-forms-iterate__done-button', className), icon: _index2.check, iconPosition: "left", onClick: doneHandler, ...restProps, children: doneButton }); } //# sourceMappingURL=DoneButton.js.map