@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
49 lines (48 loc) • 2.6 kB
JavaScript
"use client";
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
const _excluded = ["path", "dataPath", "value"];
import React, { useContext, useMemo } from 'react';
import StringValue from '../String';
import { useValueProps } from '../../hooks';
import useDataValue from '../../hooks/useDataValue';
import Context from '../../DataContext/Context';
import { convertJsxToString } from '../../../../shared/component-helper';
function Selection(props) {
const {
fieldInternalsRef
} = useContext(Context) || {};
const _useValueProps = useValueProps(props),
{
path,
dataPath,
value
} = _useValueProps,
rest = _objectWithoutProperties(_useValueProps, _excluded);
const {
getValueByPath
} = useDataValue();
const valueToDisplay = useMemo(() => {
var _fieldInternalsRef$cu, _fieldInternalsRef$cu2;
const fieldProp = fieldInternalsRef === null || fieldInternalsRef === void 0 ? void 0 : (_fieldInternalsRef$cu = fieldInternalsRef.current) === null || _fieldInternalsRef$cu === void 0 ? void 0 : (_fieldInternalsRef$cu2 = _fieldInternalsRef$cu[path]) === null || _fieldInternalsRef$cu2 === void 0 ? void 0 : _fieldInternalsRef$cu2.props;
if (path || dataPath) {
var _getValueByPath, _getValueByPath$map, _list, _list$find, _list$find$call, _list$find$call$props;
let list = (_getValueByPath = getValueByPath(dataPath)) === null || _getValueByPath === void 0 ? 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 ? void 0 : (_list$find = _list.find) === null || _list$find === void 0 ? void 0 : (_list$find$call = _list$find.call(_list, child => child.props.value === value)) === null || _list$find$call === void 0 ? void 0 : (_list$find$call$props = _list$find$call.props) === null || _list$find$call$props === void 0 ? void 0 : _list$find$call$props.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