@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
46 lines (45 loc) • 2.23 kB
JavaScript
"use client";
import _extends from "@babel/runtime-corejs3/helpers/esm/extends";
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";
function Selection(props) {
const {
fieldInternalsRef
} = useContext(Context) || {};
const {
path,
dataPath,
value,
...rest
} = useValueProps(props);
const {
getValueByPath
} = useDataValue();
const valueToDisplay = useMemo(() => {
var _fieldInternalsRef$cu;
const fieldProp = fieldInternalsRef === null || fieldInternalsRef === void 0 || (_fieldInternalsRef$cu = fieldInternalsRef.current) === null || _fieldInternalsRef$cu === void 0 || (_fieldInternalsRef$cu = _fieldInternalsRef$cu[path]) === null || _fieldInternalsRef$cu === void 0 ? void 0 : _fieldInternalsRef$cu.props;
if (path || dataPath) {
var _getValueByPath, _getValueByPath$map, _list, _list$find;
let list = (_getValueByPath = getValueByPath(dataPath)) === null || _getValueByPath === void 0 || (_getValueByPath$map = _getValueByPath.map) === null || _getValueByPath$map === void 0 ? void 0 : _getValueByPath$map.call(_getValueByPath, props => ({
props
}));
if (!list) {
list = fieldProp === null || fieldProp === void 0 ? void 0 : fieldProp.children;
}
const title = (_list = list) === null || _list === void 0 || (_list$find = _list.find) === null || _list$find === void 0 || (_list$find = _list$find.call(_list, child => child.props.value === value)) === null || _list$find === void 0 || (_list$find = _list$find.props) === null || _list$find === void 0 ? void 0 : _list$find.title;
return title ? convertJsxToString(title) : value;
}
return value;
}, [dataPath, fieldInternalsRef, getValueByPath, path, value]);
return React.createElement(StringValue, _extends({
value: valueToDisplay,
path: path
}, rest));
}
Selection._supportsSpacingProps = true;
export default Selection;
//# sourceMappingURL=Selection.js.map