useDisplayValues.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. "use strict";
  2. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  9. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  10. var _treeUtil = require("../utils/treeUtil");
  11. var React = _interopRequireWildcard(require("react"));
  12. var _commonUtil = require("../utils/commonUtil");
  13. var _default = exports.default = function _default(rawValues, options, fieldNames, multiple, displayRender) {
  14. return React.useMemo(function () {
  15. var mergedDisplayRender = displayRender ||
  16. // Default displayRender
  17. function (labels) {
  18. var mergedLabels = multiple ? labels.slice(-1) : labels;
  19. var SPLIT = ' / ';
  20. if (mergedLabels.every(function (label) {
  21. return ['string', 'number'].includes((0, _typeof2.default)(label));
  22. })) {
  23. return mergedLabels.join(SPLIT);
  24. }
  25. // If exist non-string value, use ReactNode instead
  26. return mergedLabels.reduce(function (list, label, index) {
  27. var keyedLabel = /*#__PURE__*/React.isValidElement(label) ? /*#__PURE__*/React.cloneElement(label, {
  28. key: index
  29. }) : label;
  30. if (index === 0) {
  31. return [keyedLabel];
  32. }
  33. return [].concat((0, _toConsumableArray2.default)(list), [SPLIT, keyedLabel]);
  34. }, []);
  35. };
  36. return rawValues.map(function (valueCells) {
  37. var _valueOptions;
  38. var valueOptions = (0, _treeUtil.toPathOptions)(valueCells, options, fieldNames);
  39. var label = mergedDisplayRender(valueOptions.map(function (_ref) {
  40. var _option$fieldNames$la;
  41. var option = _ref.option,
  42. value = _ref.value;
  43. return (_option$fieldNames$la = option === null || option === void 0 ? void 0 : option[fieldNames.label]) !== null && _option$fieldNames$la !== void 0 ? _option$fieldNames$la : value;
  44. }), valueOptions.map(function (_ref2) {
  45. var option = _ref2.option;
  46. return option;
  47. }));
  48. var value = (0, _commonUtil.toPathKey)(valueCells);
  49. return {
  50. label: label,
  51. value: value,
  52. key: value,
  53. valueCells: valueCells,
  54. disabled: (_valueOptions = valueOptions[valueOptions.length - 1]) === null || _valueOptions === void 0 || (_valueOptions = _valueOptions.option) === null || _valueOptions === void 0 ? void 0 : _valueOptions.disabled
  55. };
  56. });
  57. }, [rawValues, options, fieldNames, displayRender, multiple]);
  58. };