@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
96 lines (95 loc) • 5.3 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.string.replace.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _components = require("../../../../components");
var _IterateItemContext = _interopRequireDefault(require("../IterateItemContext"));
var _hooks = require("../hooks");
var _types = require("../../types");
var _icons = require("../../../../icons");
var _Context = _interopRequireDefault(require("../../DataContext/Context"));
var _useDataValue = _interopRequireDefault(require("../../hooks/useDataValue"));
var _componentHelper = require("../../../../shared/component-helper");
const _excluded = ["pushValue", "className", "path", "itemPath", "text", "children"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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 _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return 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
} = props,
restProps = _objectWithoutProperties(props, _excluded);
const buttonProps = (0, _types.omitDataValueReadWriteProps)(restProps);
const {
absolutePath
} = (0, _hooks.useItemPath)(itemPath);
const arrayValue = (0, _useDataValue.default)().getValueByPath(path || absolutePath);
const {
hasReachedLimit,
setShowStatus
} = (0, _hooks.useArrayLimit)(path);
if (arrayValue !== undefined && !Array.isArray(arrayValue)) {
throw new Error('PushButton received a non-array value');
}
const {
setLastItemContainerMode
} = (0, _hooks.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(path || absolutePath, [...(arrayValue !== null && arrayValue !== void 0 ? arrayValue : []), newValue]));
}
if (!absolutePath) {
setTimeout(() => {
setLastItemContainerMode('view');
}, 100);
}
}, [arrayValue, absolutePath, handlePathChange, handlePush, hasReachedLimit, path, 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(_components.Button, _extends({
className: (0, _classnames.default)('dnb-forms-iterate-push-button', className),
variant: "secondary",
icon: _icons.add,
icon_position: "left",
on_click: handleClick
}, buttonProps), content);
}
PushButton._supportsSpacingProps = true;
var _default = exports.default = PushButton;
//# sourceMappingURL=PushButton.js.map