You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.6 KiB

1 month ago
  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. function hasExistingOption(query) {
  13. const hasOption = (option) => getLabel(option) === query;
  14. return props.options && props.options.some(hasOption) || states.createdOptions.some(hasOption);
  15. }
  16. function selectNewOption(option) {
  17. if (!enableAllowCreateMode.value) {
  18. return;
  19. }
  20. if (props.multiple && option.created) {
  21. createOptionCount.value++;
  22. } else {
  23. cachedSelectedOption.value = option;
  24. }
  25. }
  26. function createNewOption(query) {
  27. if (enableAllowCreateMode.value) {
  28. if (query && query.length > 0) {
  29. if (hasExistingOption(query)) {
  30. return;
  31. }
  32. const newOption = {
  33. [aliasProps.value.value]: query,
  34. [aliasProps.value.label]: query,
  35. created: true,
  36. [aliasProps.value.disabled]: false
  37. };
  38. if (states.createdOptions.length >= createOptionCount.value) {
  39. states.createdOptions[createOptionCount.value] = newOption;
  40. } else {
  41. states.createdOptions.push(newOption);
  42. }
  43. } else {
  44. if (props.multiple) {
  45. states.createdOptions.length = createOptionCount.value;
  46. } else {
  47. const selectedOption = cachedSelectedOption.value;
  48. states.createdOptions.length = 0;
  49. if (selectedOption && selectedOption.created) {
  50. states.createdOptions.push(selectedOption);
  51. }
  52. }
  53. }
  54. }
  55. }
  56. function removeNewOption(option) {
  57. if (!enableAllowCreateMode.value || !option || !option.created || option.created && props.reserveKeyword && states.inputValue === getLabel(option)) {
  58. return;
  59. }
  60. const idx = states.createdOptions.findIndex((it) => getValue(it) === getValue(option));
  61. if (~idx) {
  62. states.createdOptions.splice(idx, 1);
  63. createOptionCount.value--;
  64. }
  65. }
  66. function clearAllNewOption() {
  67. if (enableAllowCreateMode.value) {
  68. states.createdOptions.length = 0;
  69. createOptionCount.value = 0;
  70. }
  71. }
  72. return {
  73. createNewOption,
  74. removeNewOption,
  75. selectNewOption,
  76. clearAllNewOption
  77. };
  78. }
  79. exports.useAllowCreate = useAllowCreate;
  80. //# sourceMappingURL=useAllowCreate.js.map