useOptions.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = useOptions;
  6. var _vue = require("vue");
  7. var _legacyUtil = require("../utils/legacyUtil");
  8. /**
  9. * Parse `children` to `options` if `options` is not provided.
  10. * Then flatten the `options`.
  11. */
  12. function useOptions(options, children, fieldNames) {
  13. const mergedOptions = (0, _vue.shallowRef)();
  14. const valueOptions = (0, _vue.shallowRef)();
  15. const labelOptions = (0, _vue.shallowRef)();
  16. const tempMergedOptions = (0, _vue.shallowRef)([]);
  17. (0, _vue.watch)([options, children], () => {
  18. if (options.value) {
  19. tempMergedOptions.value = (0, _vue.toRaw)(options.value).slice();
  20. } else {
  21. tempMergedOptions.value = (0, _legacyUtil.convertChildrenToData)(children.value);
  22. }
  23. }, {
  24. immediate: true,
  25. deep: true
  26. });
  27. (0, _vue.watchEffect)(() => {
  28. const newOptions = tempMergedOptions.value;
  29. const newValueOptions = new Map();
  30. const newLabelOptions = new Map();
  31. const fieldNamesValue = fieldNames.value;
  32. function dig(optionList) {
  33. let isChildren = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  34. // for loop to speed up collection speed
  35. for (let i = 0; i < optionList.length; i += 1) {
  36. const option = optionList[i];
  37. if (!option[fieldNamesValue.options] || isChildren) {
  38. newValueOptions.set(option[fieldNamesValue.value], option);
  39. newLabelOptions.set(option[fieldNamesValue.label], option);
  40. } else {
  41. dig(option[fieldNamesValue.options], true);
  42. }
  43. }
  44. }
  45. dig(newOptions);
  46. mergedOptions.value = newOptions;
  47. valueOptions.value = newValueOptions;
  48. labelOptions.value = newLabelOptions;
  49. });
  50. return {
  51. options: mergedOptions,
  52. valueOptions,
  53. labelOptions
  54. };
  55. }