@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
72 lines (71 loc) • 2.67 kB
JavaScript
"use client";
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
import React, { useCallback, useContext, useEffect, useRef } from 'react';
import classnames from 'classnames';
import { Button } from "../../../../components/index.js";
import useTranslation from "../../hooks/useTranslation.js";
import IterateItemContext from "../IterateItemContext.js";
import ToolbarContext from "../Toolbar/ToolbarContext.js";
import FieldBoundaryContext from "../../DataContext/FieldBoundary/FieldBoundaryContext.js";
import PushContainerContext from "../PushContainer/PushContainerContext.js";
import { check } from "../../../../icons/index.js";
export default function DoneButton(props) {
const {
className,
...restProps
} = props;
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 || 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 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