@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
74 lines (73 loc) • 2.22 kB
JavaScript
"use client";
import _includesInstanceProperty from "core-js-pure/stable/instance/includes.js";
import { useContext, useMemo } from 'react';
import { clsx } from 'clsx';
import { useValueProps } from "../../hooks/index.js";
import useDataValue from "../../hooks/useDataValue.js";
import ValueBlock from "../../ValueBlock/index.js";
import { convertJsxToString } from "../../../../shared/component-helper.js";
import Context from "../../DataContext/Context.js";
import ListFormat from "../../../../components/list-format/index.js";
import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
function MultiSelection(props) {
const {
fieldInternalsRef
} = useContext(Context) || {};
const {
path,
data,
dataPath,
value,
format,
className,
variant = 'text',
listType,
...rest
} = useValueProps(props);
const {
value: dataPathValue
} = useDataValue(dataPath, undefined, {
pathType: 'absolute'
});
const list = useMemo(() => {
let valueToUse = value;
if (path || dataPath || data) {
let items;
if (path) {
items = fieldInternalsRef?.current?.[path + '/multiSelectionData']?.props;
}
if (!items && dataPath) {
items = dataPathValue;
}
if (!items && data) {
items = data;
}
if (items && value) {
const selectedItems = items.filter(item => _includesInstanceProperty(value).call(value, item.value));
valueToUse = selectedItems.map(({
title
}) => convertJsxToString(title));
}
}
if (typeof valueToUse === 'undefined') {
return undefined;
}
return _jsx(ListFormat, {
value: valueToUse,
format: format,
variant: variant,
listType: listType
});
}, [value, path, data, dataPath, format, variant, listType, fieldInternalsRef, dataPathValue]);
return _jsx(ValueBlock, {
className: clsx('dnb-forms-value-multi-selection', className),
...rest,
children: list
});
}
withComponentMarkers(MultiSelection, {
_supportsSpacingProps: true
});
export default MultiSelection;
//# sourceMappingURL=MultiSelection.js.map