useDisplayValues.js 2.2 KB

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