UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

97 lines (96 loc) 4.55 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _classnames = _interopRequireDefault(require("classnames")); 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"); 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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } 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 _react.default.createElement(_index.Button, _extends({ className: (0, _classnames.default)('dnb-forms-iterate-push-button', className), variant: "secondary", icon: _index3.add, icon_position: "left", on_click: handleClick }, buttonProps), content); } PushButton._supportsSpacingProps = true; var _default = exports.default = PushButton; //# sourceMappingURL=PushButton.js.map