UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

59 lines (58 loc) 4.47 kB
"use strict"; "use client"; require("core-js/modules/web.dom-collections.iterator.js"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _react = _interopRequireWildcard(require("react")); var _String = _interopRequireDefault(require("../String")); var _hooks = require("../../hooks"); var _useDataValue = _interopRequireDefault(require("../../hooks/useDataValue")); var _Context = _interopRequireDefault(require("../../DataContext/Context")); var _componentHelper = require("../../../../shared/component-helper"); const _excluded = ["path", "dataPath", "value"]; function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } function Selection(props) { const { fieldInternalsRef } = (0, _react.useContext)(_Context.default) || {}; const _useValueProps = (0, _hooks.useValueProps)(props), { path, dataPath, value } = _useValueProps, rest = _objectWithoutProperties(_useValueProps, _excluded); const { getValueByPath } = (0, _useDataValue.default)(); const valueToDisplay = (0, _react.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 ? (0, _componentHelper.convertJsxToString)(title) : value; } return value; }, [dataPath, fieldInternalsRef, getValueByPath, path, value]); return _react.default.createElement(_String.default, _extends({ value: valueToDisplay, path: path }, rest)); } Selection._supportsSpacingProps = true; var _default = exports.default = Selection; //# sourceMappingURL=Selection.js.map