7d0df0559b70522e484ac4879adc05e0cee9b955f2785d8226499f25328c2423a8978a4739f2963d036c21dac1262395f62696d29ab9a2114b393300de49d6 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var lodashUnified = require('lodash-unified');
  5. var token = require('./token.js');
  6. var option = require('./option.js');
  7. var strings = require('../../../utils/strings.js');
  8. var error = require('../../../utils/error.js');
  9. var shared = require('@vue/shared');
  10. function useOption(props, states) {
  11. const select = vue.inject(token.selectKey);
  12. if (!select) {
  13. error.throwError(option.COMPONENT_NAME, "usage: <el-select><el-option /></el-select/>");
  14. }
  15. const selectGroup = vue.inject(token.selectGroupKey, { disabled: false });
  16. const itemSelected = vue.computed(() => {
  17. return contains(lodashUnified.castArray(select.props.modelValue), props.value);
  18. });
  19. const limitReached = vue.computed(() => {
  20. var _a;
  21. if (select.props.multiple) {
  22. const modelValue = lodashUnified.castArray((_a = select.props.modelValue) != null ? _a : []);
  23. return !itemSelected.value && modelValue.length >= select.props.multipleLimit && select.props.multipleLimit > 0;
  24. } else {
  25. return false;
  26. }
  27. });
  28. const currentLabel = vue.computed(() => {
  29. var _a;
  30. return (_a = props.label) != null ? _a : shared.isObject(props.value) ? "" : props.value;
  31. });
  32. const currentValue = vue.computed(() => {
  33. return props.value || props.label || "";
  34. });
  35. const isDisabled = vue.computed(() => {
  36. return props.disabled || states.groupDisabled || limitReached.value;
  37. });
  38. const instance = vue.getCurrentInstance();
  39. const contains = (arr = [], target) => {
  40. if (!shared.isObject(props.value)) {
  41. return arr && arr.includes(target);
  42. } else {
  43. const valueKey = select.props.valueKey;
  44. return arr && arr.some((item) => {
  45. return vue.toRaw(lodashUnified.get(item, valueKey)) === lodashUnified.get(target, valueKey);
  46. });
  47. }
  48. };
  49. const hoverItem = () => {
  50. if (!props.disabled && !selectGroup.disabled) {
  51. select.states.hoveringIndex = select.optionsArray.indexOf(instance.proxy);
  52. }
  53. };
  54. const updateOption = (query) => {
  55. const regexp = new RegExp(strings.escapeStringRegexp(query), "i");
  56. states.visible = regexp.test(String(currentLabel.value)) || props.created;
  57. };
  58. vue.watch(() => currentLabel.value, () => {
  59. if (!props.created && !select.props.remote)
  60. select.setSelected();
  61. });
  62. vue.watch(() => props.value, (val, oldVal) => {
  63. const { remote, valueKey } = select.props;
  64. const shouldUpdate = remote ? val !== oldVal : !lodashUnified.isEqual(val, oldVal);
  65. if (shouldUpdate) {
  66. select.onOptionDestroy(oldVal, instance.proxy);
  67. select.onOptionCreate(instance.proxy);
  68. }
  69. if (!props.created && !remote) {
  70. if (valueKey && shared.isObject(val) && shared.isObject(oldVal) && val[valueKey] === oldVal[valueKey]) {
  71. return;
  72. }
  73. select.setSelected();
  74. }
  75. });
  76. vue.watch(() => selectGroup.disabled, () => {
  77. states.groupDisabled = selectGroup.disabled;
  78. }, { immediate: true });
  79. return {
  80. select,
  81. currentLabel,
  82. currentValue,
  83. itemSelected,
  84. isDisabled,
  85. hoverItem,
  86. updateOption
  87. };
  88. }
  89. exports.useOption = useOption;
  90. //# sourceMappingURL=useOption.js.map