UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

73 lines (72 loc) 2.24 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?.[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 _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