useDisplayValues.js 1.8 KB

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