useOption.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 (!isDisabled.value) {
  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(
  59. () => currentLabel.value,
  60. () => {
  61. if (!props.created && !select.props.remote)
  62. select.setSelected();
  63. }
  64. );
  65. vue.watch(
  66. () => props.value,
  67. (val, oldVal) => {
  68. const { remote, valueKey } = select.props;
  69. const shouldUpdate = remote ? val !== oldVal : !lodashUnified.isEqual(val, oldVal);
  70. if (shouldUpdate) {
  71. select.onOptionDestroy(oldVal, instance.proxy);
  72. select.onOptionCreate(instance.proxy);
  73. }
  74. if (!props.created && !remote) {
  75. if (valueKey && shared.isObject(val) && shared.isObject(oldVal) && val[valueKey] === oldVal[valueKey]) {
  76. return;
  77. }
  78. select.setSelected();
  79. }
  80. }
  81. );
  82. vue.watch(
  83. () => selectGroup.disabled,
  84. () => {
  85. states.groupDisabled = selectGroup.disabled;
  86. },
  87. { immediate: true }
  88. );
  89. return {
  90. select,
  91. currentLabel,
  92. currentValue,
  93. itemSelected,
  94. isDisabled,
  95. hoverItem,
  96. updateOption
  97. };
  98. }
  99. exports.useOption = useOption;
  100. //# sourceMappingURL=useOption.js.map