UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

93 lines (92 loc) 3.23 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; var _Fragment; const _excluded = ["onClick", "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 { close } from '../../../../icons'; import RemoveButton from '../RemoveButton'; export default function CancelButton(props) { const { onClick, className } = props, rest = _objectWithoutProperties(props, _excluded); const { restoreOriginalValue, switchContainerMode, containerMode, arrayValue, isNew, index } = useContext(IterateItemContext) || {}; const { setShowBoundaryErrors } = useContext(FieldBoundaryContext) || {}; const { setShowError } = useContext(ToolbarContext) || {}; const { cancelButton, removeButton } = 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 cancelHandler = useCallback(_ref => { let { event } = _ref; onClick === null || onClick === void 0 ? void 0 : onClick(event); restoreOriginalValue === null || restoreOriginalValue === void 0 ? void 0 : restoreOriginalValue(valueBackupRef.current); setShowError(false); setShowBoundaryErrors === null || setShowBoundaryErrors === void 0 ? void 0 : setShowBoundaryErrors(false); switchContainerMode === null || switchContainerMode === void 0 ? void 0 : switchContainerMode('view'); }, [onClick, restoreOriginalValue, setShowBoundaryErrors, setShowError, switchContainerMode]); const wasNew = useWasNew({ isNew, containerMode }); if (containerMode === 'edit' && (arrayValue === null || arrayValue === void 0 ? void 0 : arrayValue.length) === 0) { return _Fragment || (_Fragment = React.createElement(React.Fragment, null)); } if (wasNew) { return React.createElement(RemoveButton, _extends({ text: removeButton, onClick: onClick }, rest)); } return React.createElement(Button, _extends({ variant: "tertiary", className: classnames('dnb-forms-iterate__cancel-button', className), icon: close, icon_position: "left", on_click: cancelHandler }, rest), cancelButton); } export function useWasNew(_ref2) { let { isNew, containerMode } = _ref2; const wasNewRef = useRef(isNew); useEffect(() => { if (containerMode === 'view') { wasNewRef.current = false; } }, [isNew, containerMode]); return wasNewRef.current; } //# sourceMappingURL=CancelButton.js.map