@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
48 lines (47 loc) • 1.46 kB
JavaScript
"use client";
import React, { useContext, useMemo } from 'react';
import StringValue from "../String/index.js";
import { useValueProps } from "../../hooks/index.js";
import useDataValue from "../../hooks/useDataValue.js";
import Context from "../../DataContext/Context.js";
import { convertJsxToString } from "../../../../shared/component-helper.js";
import withComponentMarkers from "../../../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
function Selection(props) {
const {
fieldInternalsRef
} = useContext(Context) || {};
const {
path,
dataPath,
value,
...rest
} = useValueProps(props);
const {
getValueByPath
} = useDataValue();
const valueToDisplay = useMemo(() => {
const fieldProp = fieldInternalsRef?.current?.[path]?.props;
if (path || dataPath) {
let list = getValueByPath(dataPath)?.map?.(props => ({
props
}));
if (!list) {
list = fieldProp?.children;
}
const title = list?.find?.(child => child.props.value === value)?.props?.title;
return title ? convertJsxToString(title) : value;
}
return value;
}, [dataPath, fieldInternalsRef, getValueByPath, path, value]);
return _jsx(StringValue, {
value: valueToDisplay,
path: path,
...rest
});
}
withComponentMarkers(Selection, {
_supportsSpacingProps: true
});
export default Selection;
//# sourceMappingURL=Selection.js.map