@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
104 lines (103 loc) • 3.73 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _includes = _interopRequireDefault(require("core-js-pure/stable/instance/includes.js"));
var _react = require("react");
var _clsx = 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 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 {
value: arrayValue
} = (0, _useDataValue.default)(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?.(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 ((0, _includes.default)(str).call(str, '{nextItemNo}')) {
const nextItemNo = (arrayValue?.length || 0) + 1;
return str.replace('{nextItemNo}', String(nextItemNo));
}
}
return children || text;
}, [arrayValue?.length, children, text]);
return (0, _jsxRuntime.jsx)(_index.Button, {
className: (0, _clsx.clsx)('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