@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
120 lines (119 loc) • 3.83 kB
JavaScript
"use client";
import React, { Fragment, useContext, useMemo } from 'react';
import { LOCALE } from "../../shared/defaults.js";
import { extendPropsWithContext } from "../../shared/component-helper.js";
import SharedContext from "../../shared/Context.js";
import { Li, Ol, Ul } from "../../elements/index.js";
import clsx from 'clsx';
import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
function ListFormat(localProps) {
const {
locale,
ListFormat
} = useContext(SharedContext);
const allProps = extendPropsWithContext(localProps, {}, ListFormat);
const {
value,
format,
variant = 'text',
listType,
children,
className,
...props
} = allProps;
const list = useMemo(() => {
const isListVariant = variant !== 'text';
const valueToUse = replaceRootFragment(children) || value;
if (!Array.isArray(valueToUse)) {
return [valueToUse];
}
return isListVariant ? React.Children.map(valueToUse, (child, index) => {
return _jsx(Li, {
children: child
}, index);
}) : valueToUse;
}, [value, children, variant, listType]);
const result = useMemo(() => {
if (variant === 'text') {
return listFormat(list, {
locale,
format
});
}
const ListElement = variant.startsWith('ol') ? Ol : Ul;
return _jsx(ListElement, {
type: listType !== 'unstyled' ? listType : null,
className: clsx('dnb-list-format', className, listType === 'unstyled' && 'dnb-unstyled-list'),
...props,
children: list
});
}, [format, list, locale]);
return result;
}
export function listFormat(list, {
locale = 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 : _jsx(React.Fragment, {
children: element
}, i);
}
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) === Fragment) {
var _children$props;
return React.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.Children.count(children) === 1 && (firstChild === null || firstChild === void 0 ? void 0 : firstChild.type) === Fragment) {
var _firstChild$props;
return React.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;
}
withComponentMarkers(ListFormat, {
_supportsSpacingProps: true
});
export default ListFormat;
//# sourceMappingURL=ListFormat.js.map