useOption.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. return props.label || (shared.isObject(props.value) ? "" : props.value);
  30. });
  31. const currentValue = vue.computed(() => {
  32. return props.value || props.label || "";
  33. });
  34. const isDisabled = vue.computed(() => {
  35. return props.disabled || states.groupDisabled || limitReached.value;
  36. });
  37. const instance = vue.getCurrentInstance();
  38. const contains = (arr = [], target) => {
  39. if (!shared.isObject(props.value)) {
  40. return arr && arr.includes(target);
  41. } else {
  42. const valueKey = select.props.valueKey;
  43. return arr && arr.some((item) => {
  44. return vue.toRaw(lodashUnified.get(item, valueKey)) === lodashUnified.get(target, valueKey);
  45. });
  46. }
  47. };
  48. const hoverItem = () => {
  49. if (!props.disabled && !selectGroup.disabled) {
  50. select.states.hoveringIndex = select.optionsArray.indexOf(instance.proxy);
  51. }
  52. };
  53. const updateOption = (query) => {
  54. const regexp = new RegExp(strings.escapeStringRegexp(query), "i");
  55. states.visible = regexp.test(String(currentLabel.value)) || props.created;
  56. };
  57. vue.watch(() => currentLabel.value, () => {
  58. if (!props.created && !select.props.remote)
  59. select.setSelected();
  60. });
  61. vue.watch(() => props.value, (val, oldVal) => {
  62. const { remote, valueKey } = select.props;
  63. const shouldUpdate = remote ? val !== oldVal : !lodashUnified.isEqual(val, oldVal);
  64. if (shouldUpdate) {
  65. select.onOptionDestroy(oldVal, instance.proxy);
  66. select.onOptionCreate(instance.proxy);
  67. }
  68. if (!props.created && !remote) {
  69. if (valueKey && shared.isObject(val) && shared.isObject(oldVal) && val[valueKey] === oldVal[valueKey]) {
  70. return;
  71. }
  72. select.setSelected();
  73. }
  74. });
  75. vue.watch(() => selectGroup.disabled, () => {
  76. states.groupDisabled = selectGroup.disabled;
  77. }, { immediate: true });
  78. return {
  79. select,
  80. currentLabel,
  81. currentValue,
  82. itemSelected,
  83. isDisabled,
  84. hoverItem,
  85. updateOption
  86. };
  87. }
  88. exports.useOption = useOption;
  89. //# sourceMappingURL=useOption.js.map