@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
74 lines (73 loc) • 2.84 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["className"];
import React, { useCallback, useContext, useEffect, useRef } from 'react';
import classnames from 'classnames';
import { Button } from '../../../../components';
import useTranslation from '../../hooks/useTranslation';
import IterateItemContext from '../IterateItemContext';
import ToolbarContext from '../Toolbar/ToolbarContext';
import FieldBoundaryContext from '../../DataContext/FieldBoundary/FieldBoundaryContext';
import PushContainerContext from '../PushContainer/PushContainerContext';
import { check } from '../../../../icons';
export default function DoneButton(props) {
const {
className
} = props,
restProps = _objectWithoutProperties(props, _excluded);
const {
switchContainerMode,
containerMode,
arrayValue,
index
} = useContext(IterateItemContext) || {};
const {
hasError,
hasVisibleError,
setShowBoundaryErrors
} = useContext(FieldBoundaryContext) || {};
const {
commitHandleRef
} = useContext(PushContainerContext) || {};
const {
setShowError
} = useContext(ToolbarContext) || {};
const {
doneButton
} = useTranslation().IterateEditContainer;
const valueBackupRef = useRef();
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 = useCallback(() => {
if (hasError) {
setShowBoundaryErrors === null || setShowBoundaryErrors === void 0 ? void 0 : setShowBoundaryErrors(true);
if (hasVisibleError) {
setShowError(true);
}
} else {
setShowBoundaryErrors === null || setShowBoundaryErrors === void 0 ? void 0 : setShowBoundaryErrors(false);
setShowError(false);
if (commitHandleRef) {
var _commitHandleRef$curr;
(_commitHandleRef$curr = commitHandleRef.current) === null || _commitHandleRef$curr === void 0 ? void 0 : _commitHandleRef$curr.call(commitHandleRef);
} else {
switchContainerMode === null || switchContainerMode === void 0 ? void 0 : switchContainerMode('view');
}
}
}, [commitHandleRef, hasError, hasVisibleError, setShowBoundaryErrors, setShowError, switchContainerMode]);
return React.createElement(Button, _extends({
variant: "tertiary",
className: classnames('dnb-forms-iterate__done-button', className),
icon: check,
icon_position: "left",
on_click: doneHandler
}, restProps), doneButton);
}
//# sourceMappingURL=DoneButton.js.map