UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

74 lines (73 loc) 2.64 kB
"use client"; import React, { useCallback, useContext, useEffect, useRef } from 'react'; import clsx from 'clsx'; 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"; import { jsx as _jsx } from "react/jsx-runtime"; 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(undefined); 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 _jsx(Button, { variant: "tertiary", className: clsx('dnb-forms-iterate__done-button', className), icon: check, iconPosition: "left", onClick: doneHandler, ...restProps, children: doneButton }); } //# sourceMappingURL=DoneButton.js.map