UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

71 lines (70 loc) 2.26 kB
"use client"; import _extends from "@babel/runtime/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?.[index]; } if (containerMode === 'view') { valueBackupRef.current = null; } }, [arrayValue, containerMode, index]); const doneHandler = useCallback(() => { if (hasError) { setShowBoundaryErrors?.(true); if (hasVisibleError) { setShowError(true); } } else { setShowBoundaryErrors?.(false); setShowError(false); if (commitHandleRef) { commitHandleRef.current?.(); } else { 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