UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

102 lines (101 loc) 4.46 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _clsx = _interopRequireDefault(require("clsx")); var _index = require("../../../../components/index.js"); var _IterateItemContext = _interopRequireDefault(require("../IterateItemContext.js")); var _index2 = require("../hooks/index.js"); var _types = require("../../types.js"); var _index3 = require("../../../../icons/index.js"); var _Context = _interopRequireDefault(require("../../DataContext/Context.js")); var _useDataValue = _interopRequireDefault(require("../../hooks/useDataValue.js")); var _index4 = require("../../hooks/index.js"); var _componentHelper = require("../../../../shared/component-helper.js"); var _withComponentMarkers = _interopRequireDefault(require("../../../../shared/helpers/withComponentMarkers.js")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function PushButton(props) { const { handlePathChange } = (0, _react.useContext)(_Context.default) || {}; const iterateItemContext = (0, _react.useContext)(_IterateItemContext.default); const { handlePush } = iterateItemContext !== null && iterateItemContext !== void 0 ? iterateItemContext : {}; const { pushValue, className, path, itemPath, text, children, ...restProps } = props; const buttonProps = (0, _types.omitDataValueReadWriteProps)(restProps); const { absolutePath } = (0, _index2.useItemPath)(itemPath); const { path: relativePath } = (0, _index4.usePath)({ path, itemPath }); const arrayValue = (0, _useDataValue.default)().getValueByPath(path || absolutePath); const { hasReachedLimit, setShowStatus } = (0, _index2.useArrayLimit)(path); if (arrayValue !== undefined && !Array.isArray(arrayValue)) { throw new Error('PushButton received a non-array value'); } const { setLastItemContainerMode } = (0, _index2.useSwitchContainerMode)(path); const handleClick = (0, _react.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 = (0, _react.useMemo)(() => { if (children || text) { const str = (0, _componentHelper.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 (0, _jsxRuntime.jsx)(_index.Button, { className: (0, _clsx.default)('dnb-forms-iterate-push-button', className), variant: "secondary", icon: _index3.add, iconPosition: "left", onClick: handleClick, ...buttonProps, children: content }); } (0, _withComponentMarkers.default)(PushButton, { _supportsSpacingProps: true }); var _default = exports.default = PushButton; //# sourceMappingURL=PushButton.js.map