useAllowCreate.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
  2. require("../../../_virtual/_rolldown/runtime.js");
  3. const require_useProps = require("./useProps.js");
  4. let vue = require("vue");
  5. //#region ../../packages/components/select-v2/src/useAllowCreate.ts
  6. function useAllowCreate(props, states) {
  7. const { aliasProps, getLabel, getValue } = require_useProps.useProps(props);
  8. const createOptionCount = (0, vue.ref)(0);
  9. const cachedSelectedOption = (0, vue.ref)();
  10. const enableAllowCreateMode = (0, vue.computed)(() => {
  11. return props.allowCreate && props.filterable;
  12. });
  13. (0, vue.watch)(() => props.options, (options) => {
  14. const optionLabelsSet = new Set(options.map((option) => getLabel(option)));
  15. states.createdOptions = states.createdOptions.filter((createdOption) => !optionLabelsSet.has(getLabel(createdOption)));
  16. });
  17. function hasExistingOption(query) {
  18. const hasOption = (option) => getLabel(option) === query;
  19. return props.options && props.options.some(hasOption) || states.createdOptions.some(hasOption);
  20. }
  21. function selectNewOption(option) {
  22. if (!enableAllowCreateMode.value) return;
  23. if (props.multiple && option.created) createOptionCount.value++;
  24. else cachedSelectedOption.value = option;
  25. }
  26. function createNewOption(query) {
  27. if (enableAllowCreateMode.value) if (query && query.length > 0) {
  28. if (hasExistingOption(query)) {
  29. states.createdOptions = states.createdOptions.filter((createdOption) => getLabel(createdOption) !== states.previousQuery);
  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) states.createdOptions[createOptionCount.value] = newOption;
  39. else states.createdOptions.push(newOption);
  40. } else if (props.multiple) states.createdOptions.length = createOptionCount.value;
  41. else {
  42. const selectedOption = cachedSelectedOption.value;
  43. states.createdOptions.length = 0;
  44. if (selectedOption && selectedOption.created) states.createdOptions.push(selectedOption);
  45. }
  46. }
  47. function removeNewOption(option) {
  48. if (!enableAllowCreateMode.value || !option || !option.created || option.created && props.reserveKeyword && states.inputValue === getLabel(option)) return;
  49. const idx = states.createdOptions.findIndex((it) => getValue(it) === getValue(option));
  50. if (~idx) {
  51. states.createdOptions.splice(idx, 1);
  52. createOptionCount.value--;
  53. }
  54. }
  55. function clearAllNewOption() {
  56. if (enableAllowCreateMode.value) {
  57. states.createdOptions.length = 0;
  58. createOptionCount.value = 0;
  59. }
  60. }
  61. return {
  62. createNewOption,
  63. removeNewOption,
  64. selectNewOption,
  65. clearAllNewOption
  66. };
  67. }
  68. //#endregion
  69. exports.useAllowCreate = useAllowCreate;
  70. //# sourceMappingURL=useAllowCreate.js.map