useDisplayValues.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 { toPathKey } from '../utils/commonUtil';
  5. import { computed } from 'vue';
  6. import { isValidElement } from '../../_util/props-util';
  7. import { cloneElement } from '../../_util/vnode';
  8. export default (function (rawValues, options, fieldNames, multiple, displayRender) {
  9. return computed(function () {
  10. var mergedDisplayRender = displayRender.value ||
  11. // Default displayRender
  12. function (_ref) {
  13. var labels = _ref.labels;
  14. var mergedLabels = multiple.value ? labels.slice(-1) : labels;
  15. var SPLIT = ' / ';
  16. if (mergedLabels.every(function (label) {
  17. return ['string', 'number'].includes(_typeof(label));
  18. })) {
  19. return mergedLabels.join(SPLIT);
  20. }
  21. // If exist non-string value, use VueNode instead
  22. return mergedLabels.reduce(function (list, label, index) {
  23. var keyedLabel = isValidElement(label) ? cloneElement(label, {
  24. key: index
  25. }) : label;
  26. if (index === 0) {
  27. return [keyedLabel];
  28. }
  29. return [].concat(_toConsumableArray(list), [SPLIT, keyedLabel]);
  30. }, []);
  31. };
  32. return rawValues.value.map(function (valueCells) {
  33. var valueOptions = toPathOptions(valueCells, options.value, fieldNames.value);
  34. var label = mergedDisplayRender({
  35. labels: valueOptions.map(function (_ref2) {
  36. var _option$fieldNames$va;
  37. var option = _ref2.option,
  38. value = _ref2.value;
  39. return (_option$fieldNames$va = option === null || option === void 0 ? void 0 : option[fieldNames.value.label]) !== null && _option$fieldNames$va !== void 0 ? _option$fieldNames$va : value;
  40. }),
  41. selectedOptions: valueOptions.map(function (_ref3) {
  42. var option = _ref3.option;
  43. return option;
  44. })
  45. });
  46. var value = toPathKey(valueCells);
  47. return {
  48. label: label,
  49. value: value,
  50. key: value,
  51. valueCells: valueCells
  52. };
  53. });
  54. });
  55. });