@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
113 lines (112 loc) • 3.34 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
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 classNames from 'classnames';
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 React.createElement(Li, {
key: index
}, child);
}) : valueToUse;
}, [value, children, variant, listType]);
const result = useMemo(() => {
if (variant === 'text') {
return listFormat(list, {
locale,
format
});
}
const ListElement = variant.startsWith('ol') ? Ol : Ul;
return React.createElement(ListElement, _extends({
type: listType !== 'unstyled' ? listType : null,
className: classNames('dnb-list-format', className, listType === 'unstyled' && 'dnb-unstyled-list')
}, props), 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 : React.createElement(React.Fragment, {
key: i
}, element);
}
return v;
});
}
return formattedList;
} catch (error) {
if (hasJSX) {
return list;
}
return list.join(', ');
}
}
function replaceRootFragment(children) {
if (children?.type === Fragment) {
return React.Children.toArray(children?.props?.children);
}
if (Array.isArray(children)) {
const firstChild = children[0];
if (React.Children.count(children) === 1 && firstChild?.type === Fragment) {
return React.Children.toArray(firstChild?.props?.children);
}
return children;
}
return children;
}
ListFormat._supportsSpacingProps = true;
export default ListFormat;
//# sourceMappingURL=ListFormat.js.map