UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

129 lines (128 loc) 6.06 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; exports.listFormat = listFormat; require("core-js/modules/web.dom-collections.iterator.js"); var _react = _interopRequireWildcard(require("react")); var _defaults = require("../../shared/defaults"); var _componentHelper = require("../../shared/component-helper"); var _Context = _interopRequireDefault(require("../../shared/Context")); var _elements = require("../../elements"); var _classnames = _interopRequireDefault(require("classnames")); const _excluded = ["value", "format", "variant", "listType", "children", "className"]; 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 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 } = allProps, props = _objectWithoutProperties(allProps, _excluded); 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(_elements.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') ? _elements.Ol : _elements.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) { let { locale = _defaults.LOCALE, format = { style: 'long', type: 'conjunction' } } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; 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 ? 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 ? 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