UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

94 lines (93 loc) 3.56 kB
"use client"; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties"; const _excluded = ["pushValue", "className", "path", "itemPath", "text", "children"]; import "core-js/modules/es.string.replace.js"; import "core-js/modules/web.dom-collections.iterator.js"; import React, { useCallback, useContext, useMemo } from 'react'; import classnames from 'classnames'; import { Button } from '../../../../components'; import IterateItemContext from '../IterateItemContext'; import { useArrayLimit, useItemPath, useSwitchContainerMode } from '../hooks'; import { omitDataValueReadWriteProps } from '../../types'; import { add } from '../../../../icons'; import DataContext from '../../DataContext/Context'; import useDataValue from '../../hooks/useDataValue'; import { usePath } from '../../hooks'; import { convertJsxToString } from '../../../../shared/component-helper'; function PushButton(props) { const { handlePathChange } = useContext(DataContext) || {}; const iterateItemContext = useContext(IterateItemContext); const { handlePush } = iterateItemContext !== null && iterateItemContext !== void 0 ? iterateItemContext : {}; const { pushValue, className, path, itemPath, text, children } = props, restProps = _objectWithoutProperties(props, _excluded); const buttonProps = omitDataValueReadWriteProps(restProps); const { absolutePath } = useItemPath(itemPath); const { path: relativePath } = usePath({ path, itemPath }); const arrayValue = useDataValue().getValueByPath(path || absolutePath); const { hasReachedLimit, setShowStatus } = useArrayLimit(path); if (arrayValue !== undefined && !Array.isArray(arrayValue)) { throw new Error('PushButton received a non-array value'); } const { setLastItemContainerMode } = useSwitchContainerMode(path); const handleClick = useCallback(async () => { if (hasReachedLimit) { setShowStatus(true); return; } const newValue = typeof pushValue === 'function' ? pushValue(arrayValue) : pushValue; if (handlePush && !absolutePath) { handlePush(newValue); } else { await (handlePathChange === null || handlePathChange === void 0 ? void 0 : handlePathChange(absolutePath || relativePath, [...(arrayValue !== null && arrayValue !== void 0 ? arrayValue : []), newValue])); } if (!absolutePath) { setTimeout(() => { setLastItemContainerMode('view'); }, 100); } }, [arrayValue, absolutePath, handlePathChange, handlePush, hasReachedLimit, relativePath, pushValue, setLastItemContainerMode, setShowStatus]); const content = useMemo(() => { if (children || text) { const str = convertJsxToString(children || text); if (str.includes('{nextItemNo}')) { const nextItemNo = ((arrayValue === null || arrayValue === void 0 ? void 0 : arrayValue.length) || 0) + 1; return str.replace('{nextItemNo}', String(nextItemNo)); } } return children || text; }, [arrayValue === null || arrayValue === void 0 ? void 0 : arrayValue.length, children, text]); return React.createElement(Button, _extends({ className: classnames('dnb-forms-iterate-push-button', className), variant: "secondary", icon: add, icon_position: "left", on_click: handleClick }, buttonProps), content); } PushButton._supportsSpacingProps = true; export default PushButton; //# sourceMappingURL=PushButton.js.map