@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
123 lines (122 loc) • 5.06 kB
JavaScript
;
"use client";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.listFormat = listFormat;
var _react = _interopRequireWildcard(require("react"));
var _defaults = require("../../shared/defaults.js");
var _componentHelper = require("../../shared/component-helper.js");
var _Context = _interopRequireDefault(require("../../shared/Context.js"));
var _index = require("../../elements/index.js");
var _classnames = _interopRequireDefault(require("classnames"));
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 ListFormat(localProps) {
const {
locale,
ListFormat
} = (0, _react.useContext)(_Context.default);
const allProps = (0, _componentHelper.extendPropsWithContext)(localProps, {}, ListFormat);
const {
value,
format,
variant = 'text',
listType,
children,
className,
...props
} = allProps;
const list = (0, _react.useMemo)(() => {
const isListVariant = variant !== 'text';
const valueToUse = replaceRootFragment(children) || value;
if (!Array.isArray(valueToUse)) {
return [valueToUse];
}
return isListVariant ? _react.default.Children.map(valueToUse, (child, index) => {
return _react.default.createElement(_index.Li, {
key: index
}, child);
}) : valueToUse;
}, [value, children, variant, listType]);
const result = (0, _react.useMemo)(() => {
if (variant === 'text') {
return listFormat(list, {
locale,
format
});
}
const ListElement = variant.startsWith('ol') ? _index.Ol : _index.Ul;
return _react.default.createElement(ListElement, _extends({
type: listType !== 'unstyled' ? listType : null,
className: (0, _classnames.default)('dnb-list-format', className, listType === 'unstyled' && 'dnb-unstyled-list')
}, props), list);
}, [format, list, locale]);
return result;
}
function listFormat(list, {
locale = _defaults.LOCALE,
format = {
style: 'long',
type: 'conjunction'
}
} = {}) {
if (!Array.isArray(list)) {
return list;
}
list = replaceRootFragment(list).filter(function (item) {
const isNan = typeof item === 'number' && isNaN(item);
return item !== undefined && item !== false && item !== null && !isNan;
});
const buffer = new Map();
const hasJSX = list.some(v => typeof v === 'object');
const shadow = list.map((v, i) => {
if (hasJSX) {
const id = `id-${i}`;
buffer.set(id, v);
return `{${id}}`;
}
return String(v);
});
try {
const formatter = new Intl.ListFormat(locale, format);
const formattedList = formatter.format(shadow);
if (hasJSX) {
return formattedList.split(/\{(id-[0-9]+)\}/).map((v, i) => {
if (v.startsWith('id-')) {
const element = buffer.get(v);
return element.key ? element : _react.default.createElement(_react.default.Fragment, {
key: i
}, element);
}
return v;
});
}
return formattedList;
} catch (error) {
if (hasJSX) {
return list;
}
return list.join(', ');
}
}
function replaceRootFragment(children) {
if ((children === null || children === void 0 ? void 0 : children.type) === _react.Fragment) {
var _children$props;
return _react.default.Children.toArray(children === null || children === void 0 || (_children$props = children.props) === null || _children$props === void 0 ? void 0 : _children$props.children);
}
if (Array.isArray(children)) {
const firstChild = children[0];
if (_react.default.Children.count(children) === 1 && (firstChild === null || firstChild === void 0 ? void 0 : firstChild.type) === _react.Fragment) {
var _firstChild$props;
return _react.default.Children.toArray(firstChild === null || firstChild === void 0 || (_firstChild$props = firstChild.props) === null || _firstChild$props === void 0 ? void 0 : _firstChild$props.children);
}
return children;
}
return children;
}
ListFormat._supportsSpacingProps = true;
var _default = exports.default = ListFormat;
//# sourceMappingURL=ListFormat.js.map