list-items.mjs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Utilities
  2. import { computed } from 'vue';
  3. import { deepEqual, getPropertyFromItem, omit, propsFactory } from "../util/index.mjs"; // Types
  4. // Composables
  5. export const makeItemsProps = propsFactory({
  6. items: {
  7. type: Array,
  8. default: () => []
  9. },
  10. itemTitle: {
  11. type: [String, Array, Function],
  12. default: 'title'
  13. },
  14. itemValue: {
  15. type: [String, Array, Function],
  16. default: 'value'
  17. },
  18. itemChildren: {
  19. type: [Boolean, String, Array, Function],
  20. default: 'children'
  21. },
  22. itemProps: {
  23. type: [Boolean, String, Array, Function],
  24. default: 'props'
  25. },
  26. returnObject: Boolean,
  27. valueComparator: {
  28. type: Function,
  29. default: deepEqual
  30. }
  31. }, 'list-items');
  32. export function transformItem(props, item) {
  33. const title = getPropertyFromItem(item, props.itemTitle, item);
  34. const value = getPropertyFromItem(item, props.itemValue, title);
  35. const children = getPropertyFromItem(item, props.itemChildren);
  36. const itemProps = props.itemProps === true ? typeof item === 'object' && item != null && !Array.isArray(item) ? 'children' in item ? omit(item, ['children']) : item : undefined : getPropertyFromItem(item, props.itemProps);
  37. const _props = {
  38. title,
  39. value,
  40. ...itemProps
  41. };
  42. return {
  43. title: String(_props.title ?? ''),
  44. value: _props.value,
  45. props: _props,
  46. children: Array.isArray(children) ? transformItems(props, children) : undefined,
  47. raw: item
  48. };
  49. }
  50. export function transformItems(props, items) {
  51. const array = [];
  52. for (const item of items) {
  53. array.push(transformItem(props, item));
  54. }
  55. return array;
  56. }
  57. export function useItems(props) {
  58. const items = computed(() => transformItems(props, props.items));
  59. const hasNullItem = computed(() => items.value.some(item => item.value === null));
  60. function transformIn(value) {
  61. if (!hasNullItem.value) {
  62. // When the model value is null, return an InternalItem
  63. // based on null only if null is one of the items
  64. value = value.filter(v => v !== null);
  65. }
  66. return value.map(v => {
  67. if (props.returnObject && typeof v === 'string') {
  68. // String model value means value is a custom input value from combobox
  69. // Don't look up existing items if the model value is a string
  70. return transformItem(props, v);
  71. }
  72. return items.value.find(item => props.valueComparator(v, item.value)) || transformItem(props, v);
  73. });
  74. }
  75. function transformOut(value) {
  76. return props.returnObject ? value.map(_ref => {
  77. let {
  78. raw
  79. } = _ref;
  80. return raw;
  81. }) : value.map(_ref2 => {
  82. let {
  83. value
  84. } = _ref2;
  85. return value;
  86. });
  87. }
  88. return {
  89. items,
  90. transformIn,
  91. transformOut
  92. };
  93. }
  94. //# sourceMappingURL=list-items.mjs.map