24bb77d72918d907fbd17506cb4b99f6df2b0d1038ef146bf8dc8afd922898b3bb2f46d5810448c9e1db841a418f70c4cec97de72a3825efea735d29d3f746 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var useProps = require('./useProps.js');
  5. function useAllowCreate(props, states) {
  6. const { aliasProps, getLabel, getValue } = useProps.useProps(props);
  7. const createOptionCount = vue.ref(0);
  8. const cachedSelectedOption = vue.ref();
  9. const enableAllowCreateMode = vue.computed(() => {
  10. return props.allowCreate && props.filterable;
  11. });
  12. vue.watch(() => props.options, (options) => {
  13. const optionLabelsSet = new Set(options.map((option) => getLabel(option)));
  14. states.createdOptions = states.createdOptions.filter((createdOption) => !optionLabelsSet.has(getLabel(createdOption)));
  15. });
  16. function hasExistingOption(query) {
  17. const hasOption = (option) => getLabel(option) === query;
  18. return props.options && props.options.some(hasOption) || states.createdOptions.some(hasOption);
  19. }
  20. function selectNewOption(option) {
  21. if (!enableAllowCreateMode.value) {
  22. return;
  23. }
  24. if (props.multiple && option.created) {
  25. createOptionCount.value++;
  26. } else {
  27. cachedSelectedOption.value = option;
  28. }
  29. }
  30. function createNewOption(query) {
  31. if (enableAllowCreateMode.value) {
  32. if (query && query.length > 0) {
  33. if (hasExistingOption(query)) {
  34. states.createdOptions = states.createdOptions.filter((createdOption) => getLabel(createdOption) !== states.previousQuery);
  35. return;
  36. }
  37. const newOption = {
  38. [aliasProps.value.value]: query,
  39. [aliasProps.value.label]: query,
  40. created: true,
  41. [aliasProps.value.disabled]: false
  42. };
  43. if (states.createdOptions.length >= createOptionCount.value) {
  44. states.createdOptions[createOptionCount.value] = newOption;
  45. } else {
  46. states.createdOptions.push(newOption);
  47. }
  48. } else {
  49. if (props.multiple) {
  50. states.createdOptions.length = createOptionCount.value;
  51. } else {
  52. const selectedOption = cachedSelectedOption.value;
  53. states.createdOptions.length = 0;
  54. if (selectedOption && selectedOption.created) {
  55. states.createdOptions.push(selectedOption);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. function removeNewOption(option) {
  62. if (!enableAllowCreateMode.value || !option || !option.created || option.created && props.reserveKeyword && states.inputValue === getLabel(option)) {
  63. return;
  64. }
  65. const idx = states.createdOptions.findIndex((it) => getValue(it) === getValue(option));
  66. if (~idx) {
  67. states.createdOptions.splice(idx, 1);
  68. createOptionCount.value--;
  69. }
  70. }
  71. function clearAllNewOption() {
  72. if (enableAllowCreateMode.value) {
  73. states.createdOptions.length = 0;
  74. createOptionCount.value = 0;
  75. }
  76. }
  77. return {
  78. createNewOption,
  79. removeNewOption,
  80. selectNewOption,
  81. clearAllNewOption
  82. };
  83. }
  84. exports.useAllowCreate = useAllowCreate;
  85. //# sourceMappingURL=useAllowCreate.js.map