@astryx-vue/core 0.16.5 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/dist/AvatarGroup/index.d.ts +1 -1
  2. package/dist/Banner/index.d.ts +1 -1
  3. package/dist/Button/index.d.ts +1 -1
  4. package/dist/ButtonGroup/index.d.ts +1 -1
  5. package/dist/Calendar/index.d.ts +1 -1
  6. package/dist/Card/index.d.ts +1 -1
  7. package/dist/Carousel/index.d.ts +1 -1
  8. package/dist/Chat/index.d.ts +1 -1
  9. package/dist/Chat/index.js +1 -1
  10. package/dist/CheckboxInput/index.d.ts +1 -1
  11. package/dist/CheckboxList/index.d.ts +1 -1
  12. package/dist/ClickableCard/index.d.ts +1 -1
  13. package/dist/Code/index.js +1 -1
  14. package/dist/Collapsible/index.d.ts +1 -1
  15. package/dist/CommandPalette/index.d.ts +1 -1
  16. package/dist/CommandPalette/index.js +1 -1
  17. package/dist/{CommandPalette-mNR6dV0B.js → CommandPalette-CECjNvGB.js} +1 -1
  18. package/dist/ComplexSelector/index.d.ts +1 -1
  19. package/dist/ComplexSelector/index.js +1 -1
  20. package/dist/{ComplexSelector-CR9XbpTa.js → ComplexSelector-Dmaag1wm.js} +5 -1
  21. package/dist/ContextMenu/index.d.ts +1 -1
  22. package/dist/DateInput/index.d.ts +1 -1
  23. package/dist/DateInput/index.js +1 -1
  24. package/dist/DateRangeInput/index.d.ts +1 -1
  25. package/dist/DateRangeInput/index.js +1 -1
  26. package/dist/DateTimeInput/index.d.ts +1 -1
  27. package/dist/DateTimeInput/index.js +1 -1
  28. package/dist/{DateTimeInput-ChGin5zS.js → DateTimeInput-Cfkuec_h.js} +1 -1
  29. package/dist/Dialog/index.d.ts +1 -1
  30. package/dist/Drawer/index.d.ts +1 -1
  31. package/dist/Field/index.d.ts +1 -1
  32. package/dist/FileInput/index.d.ts +1 -1
  33. package/dist/Heading/index.d.ts +1 -1
  34. package/dist/HoverCard/index.d.ts +1 -1
  35. package/dist/Indicator/index.d.ts +2 -2
  36. package/dist/InfoTip/index.d.ts +1 -1
  37. package/dist/InputGroup/index.d.ts +1 -1
  38. package/dist/InternationalizationProvider/index.d.ts +1 -1
  39. package/dist/InternationalizationProvider/index.js +1 -1
  40. package/dist/{InternationalizationProvider-7rA65NO7.js → InternationalizationProvider-Dr5GE6f0.js} +1 -1
  41. package/dist/Link/index.d.ts +1 -1
  42. package/dist/List/index.d.ts +1 -1
  43. package/dist/Markdown/index.js +1 -1
  44. package/dist/{Markdown-BNVd70jg.js → Markdown-DnnWjJGX.js} +1 -1
  45. package/dist/MoreMenu/index.d.ts +1 -1
  46. package/dist/MultiSelector/index.d.ts +1 -1
  47. package/dist/MultiSelector/index.js +1 -1
  48. package/dist/{MultiSelector-CXX1Hsty.js → MultiSelector-BK6nZweN.js} +26 -14
  49. package/dist/NavItem/index.d.ts +1 -1
  50. package/dist/NumberInput/index.d.ts +1 -1
  51. package/dist/OverflowList/index.d.ts +1 -1
  52. package/dist/Pagination/index.d.ts +1 -1
  53. package/dist/Pagination/index.js +1 -1
  54. package/dist/{Pagination-BvZFvwso.js → Pagination-DFQNX_Mb.js} +1 -1
  55. package/dist/Popover/index.d.ts +1 -1
  56. package/dist/PowerSearch/index.d.ts +1 -1
  57. package/dist/PowerSearch/index.js +1 -1
  58. package/dist/{PowerSearch-BiupstfT.js → PowerSearch-BAOqpQag.js} +10 -10
  59. package/dist/ProgressBar/index.d.ts +1 -1
  60. package/dist/Radio/index.d.ts +1 -1
  61. package/dist/RadioList/index.d.ts +1 -1
  62. package/dist/Resizable/index.d.ts +1 -1
  63. package/dist/ScrollableArea/index.d.ts +1 -1
  64. package/dist/SegmentedControl/index.d.ts +1 -1
  65. package/dist/SelectableCard/index.d.ts +1 -1
  66. package/dist/Selector/index.d.ts +1 -1
  67. package/dist/Selector/index.js +1 -1
  68. package/dist/{Selector-DiSLNzl8.js → Selector-Bo7qkt1P.js} +157 -62
  69. package/dist/SideNav/index.d.ts +1 -1
  70. package/dist/Stepper/index.d.ts +1 -1
  71. package/dist/Switch/index.d.ts +1 -1
  72. package/dist/TabList/index.d.ts +1 -1
  73. package/dist/Table/index.d.ts +1 -1
  74. package/dist/Table/index.js +1 -1
  75. package/dist/{Table-BD8MugZo.js → Table-CRKljXg4.js} +6 -6
  76. package/dist/Text/index.d.ts +1 -1
  77. package/dist/TextArea/index.d.ts +1 -1
  78. package/dist/TextInput/index.d.ts +1 -1
  79. package/dist/Theme/index.d.ts +3 -3
  80. package/dist/Theme/index.js +1 -1
  81. package/dist/{Theme-8bDq2nud.js → Theme-iqi6AvU_.js} +363 -39
  82. package/dist/TimeInput/index.d.ts +1 -1
  83. package/dist/Timestamp/index.d.ts +1 -1
  84. package/dist/ToggleButton/index.d.ts +1 -1
  85. package/dist/Token/index.d.ts +1 -1
  86. package/dist/Tokenizer/index.d.ts +1 -1
  87. package/dist/Tokenizer/index.js +1 -1
  88. package/dist/{Tokenizer-mu0Sak58.js → Tokenizer-CsFiYUJ_.js} +171 -103
  89. package/dist/Toolbar/index.d.ts +1 -1
  90. package/dist/Tooltip/index.d.ts +1 -1
  91. package/dist/TopNav/index.d.ts +1 -1
  92. package/dist/Typeahead/index.d.ts +2 -1
  93. package/dist/Typeahead/index.js +2 -2
  94. package/dist/{Typeahead-Db8Tu27Z.js → Typeahead--kEp21ut.js} +159 -71
  95. package/dist/{useKeyboardHighlight-CGCf1O57.js → assertOptions-CK_gJ0d3.js} +12 -1
  96. package/dist/composables/index.d.ts +3 -3
  97. package/dist/composables/index.js +5 -5
  98. package/dist/composables-BKHcPgN0.js +5730 -0
  99. package/dist/{index-CE74Qsbn.d.ts → index-85RFPeV0.d.ts} +10 -10
  100. package/dist/{index-BFZxzCYu.d.ts → index-9tZJgo0R.d.ts} +2 -2
  101. package/dist/{index-B-Jla56c.d.ts → index-B0iRA00v.d.ts} +3 -3
  102. package/dist/{index-LHvLg1Pv.d.ts → index-B95VIgRl.d.ts} +84 -84
  103. package/dist/{index-DMN_JVEt.d.ts → index-BL4mH8aK.d.ts} +1 -1
  104. package/dist/{index-Bfm6mF-r.d.ts → index-BfoazhEu.d.ts} +1 -1
  105. package/dist/{index-CQ1bsxLC.d.ts → index-C1qJlC65.d.ts} +1 -1
  106. package/dist/{index-ygmRS6Np.d.ts → index-C1zwvxPH.d.ts} +2 -2
  107. package/dist/{index-gmY3A_Iw.d.ts → index-C93nwgTW.d.ts} +1 -1
  108. package/dist/{index-BvWBeoa8.d.ts → index-CEjvYAUZ.d.ts} +1 -1
  109. package/dist/{index-B0yaHi__.d.ts → index-CH-xSiJz.d.ts} +12 -5
  110. package/dist/{index-sXu15K4j.d.ts → index-CO9GW-ik.d.ts} +1 -1
  111. package/dist/{index-Xc3q9f84.d.ts → index-COgrQZIL.d.ts} +3 -3
  112. package/dist/{index-Bgk1Rj2A.d.ts → index-CTjkJP4x.d.ts} +1 -1
  113. package/dist/{index-BQ3cm7mP.d.ts → index-CbA8rR_b.d.ts} +1 -1
  114. package/dist/{index-B0R5CLPF.d.ts → index-CsYDrQ9R.d.ts} +1 -1
  115. package/dist/{index-BWzEt0gT.d.ts → index-CuW4AQ7M.d.ts} +2 -2
  116. package/dist/{index-BPf_jmME.d.ts → index-Cy1rEWC-.d.ts} +2 -2
  117. package/dist/{index-D_xBhUV5.d.ts → index-CyMStZq4.d.ts} +10 -10
  118. package/dist/{index-C2cH8OLC.d.ts → index-D31bgBoy.d.ts} +1 -1
  119. package/dist/index-DGXCnzOL.d.ts +69 -0
  120. package/dist/{index-DBwEz4M9.d.ts → index-DK0Wdo9N.d.ts} +13 -13
  121. package/dist/{index-SWw-WMTO.d.ts → index-DYabo8TI.d.ts} +2 -2
  122. package/dist/{index-CJdOt5Cc.d.ts → index-DofC9Rll.d.ts} +2 -2
  123. package/dist/{index-Byan7Dad.d.ts → index-GmoeIuhl.d.ts} +1 -1
  124. package/dist/{index-unAZVFGz.d.ts → index-Qdbssopn.d.ts} +2 -2
  125. package/dist/{index-DE1genab.d.ts → index-QnlWyUxN.d.ts} +2 -2
  126. package/dist/{index-C5e2rZML.d.ts → index-dWlCpnua.d.ts} +2 -2
  127. package/dist/{index-BPHqOCfp.d.ts → index-lmd9LZKB.d.ts} +4 -4
  128. package/dist/{index-CtYlR9Cu.d.ts → index-rdwI-mcm.d.ts} +1 -1
  129. package/dist/{index-BXDqyk2_.d.ts → index-tuNsmJxw.d.ts} +169 -2
  130. package/dist/{index-BEZ4ezJk.d.ts → index-wY6dKz6c.d.ts} +2 -2
  131. package/dist/{index-C89cIh4E.d.ts → index-waTkqxTz.d.ts} +1 -1
  132. package/dist/index.d.ts +39 -38
  133. package/dist/index.js +26 -26
  134. package/dist/{loadCatalog-DFyDz_4e.d.ts → loadCatalog-BJwcyX8i.d.ts} +2 -0
  135. package/dist/{loadCatalog-C3MSknfx.js → loadCatalog-C1kms5sO.js} +5 -1
  136. package/dist/locale/index.d.ts +2 -2
  137. package/dist/locale/index.js +3 -3
  138. package/dist/{locale-OCGw4bi6.js → locale-CHhsd9Mq.js} +1 -1
  139. package/dist/{typeahead-RkZw0Ktn.js → typeahead-BgfLP7vR.js} +2 -0
  140. package/dist/{index-DbFIQDoa.d.ts → types-C1ydeFiu.d.ts} +43 -65
  141. package/dist/{types-uWxXh1Jm.d.ts → types-fRasDCe4.d.ts} +47 -6
  142. package/dist/{useAsyncSource-Gk0VtE7_.js → useAsyncSource-7GTcK8ep.js} +31 -1
  143. package/dist/{useAsyncSource-aGzrvRkI.d.ts → useAsyncSource-DbG2FSuI.d.ts} +19 -2
  144. package/dist/{useCombobox-D374_pT8.js → useCombobox-DIR9yoEu.js} +150 -3
  145. package/dist/{useTypeaheadAnnouncement-CSikDlpK.js → useTypeaheadMenuConfig--4f7jR6G.js} +264 -20
  146. package/package.json +1 -1
  147. package/styles/motion.css +10 -0
  148. package/styles/selector.css +10 -0
  149. package/dist/composables-CCsHfpCS.js +0 -1256
  150. package/dist/{Chat-PEkZTrQE.js → Chat-Wq7e98zS.js} +1 -1
  151. package/dist/{CodeBlock-9Lfec8C8.js → CodeBlock-CLVXVlB7.js} +1 -1
  152. package/dist/{DateInput-kXAc_r5m.js → DateInput-BitZfGEY.js} +1 -1
  153. package/dist/{DateRangeInput-DW7IzFQM.js → DateRangeInput-kxXBsUTU.js} +1 -1
  154. package/dist/{index-BqhgeAT4.d.ts → index-D8lv2BiD.d.ts} +1 -1
@@ -1,3 +1,4 @@
1
+ import { t as devWarn } from "./useDevWarning-B2fsk2B-.js";
1
2
  import { onScopeDispose, ref, shallowRef, toValue, watch } from "vue";
2
3
  //#region src/composables/useAsyncSource.ts
3
4
  /**
@@ -25,6 +26,19 @@ function createStaticSource(items, options) {
25
26
  function isPromise(value) {
26
27
  return typeof value?.then === "function";
27
28
  }
29
+ /**
30
+ * 同一个查询的后一份列表接在前一份已显示的行后面:已显示的行留在最前面、顺序不变,
31
+ * 新行只加在后面。后一份列表移动或去掉了已显示的行时按它给的显示,并在开发期警告。
32
+ */
33
+ function appendAfterShown(shown, next) {
34
+ const nextById = new Map(next.map((item) => [item.id, item]));
35
+ if (!(shown.filter((item) => nextById.has(item.id)).length === shown.length && next.filter((item) => shown.some((row) => row.id === item.id)).every((item, index) => item.id === shown[index]?.id))) {
36
+ devWarn("SearchSource", "a later list for the same query moved or removed rows that were already shown, so it is shown as given. Keep the shown rows first and in order, and add new rows after them.");
37
+ return [...next];
38
+ }
39
+ const shownIds = new Set(shown.map((item) => item.id));
40
+ return [...shown.map((item) => nextById.get(item.id)), ...next.filter((item) => !shownIds.has(item.id))];
41
+ }
28
42
  function isAbortError(error) {
29
43
  return error instanceof Error && error.name === "AbortError";
30
44
  }
@@ -36,6 +50,7 @@ function useAsyncSource(options) {
36
50
  const items = shallowRef([]);
37
51
  const status = ref("idle");
38
52
  const error = shallowRef(null);
53
+ const searchedQuery = ref(null);
39
54
  let requestId = 0;
40
55
  let timer;
41
56
  let controller;
@@ -54,18 +69,32 @@ function useAsyncSource(options) {
54
69
  const ac = new AbortController();
55
70
  controller = ac;
56
71
  const query = options.query.value;
57
- const pending = query.trim() === "" && source.bootstrap ? source.bootstrap(ac.signal) : source.search(query, ac.signal);
72
+ searchedQuery.value = query;
73
+ const isBootstrap = query.trim() === "" && source.bootstrap != null;
74
+ let shown = null;
75
+ let isSettled = false;
76
+ const pending = isBootstrap ? source.bootstrap(ac.signal) : source.search(query, ac.signal, { update: (list) => {
77
+ if (isSettled || captured !== requestId || ac.signal.aborted) return;
78
+ if (list.length === 0 && shown != null) return;
79
+ shown = shown == null ? [...list] : appendAfterShown(shown, list);
80
+ items.value = shown;
81
+ error.value = null;
82
+ } });
58
83
  try {
59
84
  let resolved;
60
85
  if (isPromise(pending)) {
61
86
  status.value = "loading";
62
87
  resolved = await pending;
63
88
  } else resolved = pending;
89
+ isSettled = true;
64
90
  if (captured !== requestId || ac.signal.aborted) return;
91
+ if (!isBootstrap && shown != null && resolved.length > 0) resolved = appendAfterShown(shown, resolved);
92
+ else if (!isBootstrap && shown != null) resolved = shown;
65
93
  items.value = resolved;
66
94
  error.value = null;
67
95
  status.value = resolved.length === 0 ? "empty" : "ready";
68
96
  } catch (caught) {
97
+ isSettled = true;
69
98
  if (captured !== requestId || ac.signal.aborted || isAbortError(caught)) return;
70
99
  items.value = [];
71
100
  error.value = caught;
@@ -101,6 +130,7 @@ function useAsyncSource(options) {
101
130
  items,
102
131
  status,
103
132
  error,
133
+ searchedQuery,
104
134
  reload: () => schedule(true),
105
135
  cancel
106
136
  };
@@ -13,12 +13,27 @@ interface SearchableItem<TAux = unknown> {
13
13
  action?: OptionActionNode | null;
14
14
  auxiliaryData?: TAux;
15
15
  }
16
+ /**
17
+ * 一次搜索的上下文,作为 `search` 的第三个参数。
18
+ */
19
+ interface SearchContext<T extends SearchableItem = SearchableItem> {
20
+ /**
21
+ * 在 `search()` 落定之前先显示一份列表,比如页面上已经有的匹配项,服务端的答案随后追加。
22
+ *
23
+ * 每份列表都是数据源当时的完整答案。同一个查询的第一份列表可以换掉已有的行;
24
+ * 之后的每一份(包括 `search()` 的返回值)把已经显示的行留在最前面、顺序不变,
25
+ * 新行只加在它们后面,所以高亮的行不会被挤走。空列表不动已显示的行。
26
+ * 查询已经不是当前查询、或 `search()` 已经落定之后的调用会被忽略。
27
+ */
28
+ update: (items: T[]) => void;
29
+ }
16
30
  /**
17
31
  * 搜索数据源。同步数组和 Promise 都可以。
18
32
  * 新一次搜索开始时会调用 `cancel`,并传入 AbortSignal,用来丢掉过期请求。
33
+ * 搜索菜单还会传入 `context`,数据源可以用它先显示已有的匹配项。
19
34
  */
20
35
  interface SearchSource<T extends SearchableItem = SearchableItem> {
21
- search: (query: string, signal?: AbortSignal) => Promise<T[]> | T[];
36
+ search: (query: string, signal?: AbortSignal, context?: SearchContext<T>) => Promise<T[]> | T[];
22
37
  bootstrap?: (signal?: AbortSignal) => Promise<T[]> | T[];
23
38
  cancel?: () => void;
24
39
  }
@@ -44,6 +59,8 @@ interface AsyncSource<T extends SearchableItem = SearchableItem> {
44
59
  items: ShallowRef<T[]>;
45
60
  status: Ref<AsyncSourceStatus>;
46
61
  error: ShallowRef<unknown>;
62
+ /** 当前的 items 和 status 是为哪个查询发出的请求得到的;请求还在防抖时它落后于 `query`。 */
63
+ searchedQuery: Ref<string | null>;
47
64
  /** 忽略防抖,立刻再搜一次。 */
48
65
  reload: () => void;
49
66
  cancel: () => void;
@@ -54,4 +71,4 @@ interface AsyncSource<T extends SearchableItem = SearchableItem> {
54
71
  */
55
72
  declare function useAsyncSource<T extends SearchableItem = SearchableItem>(options: UseAsyncSourceOptions<T>): AsyncSource<T>;
56
73
  //#endregion
57
- export { SearchableItem as a, useAsyncSource as c, SearchSource as i, AsyncSourceStatus as n, UseAsyncSourceOptions as o, CreateStaticSourceOptions as r, createStaticSource as s, AsyncSource as t };
74
+ export { SearchSource as a, createStaticSource as c, SearchContext as i, useAsyncSource as l, AsyncSourceStatus as n, SearchableItem as o, CreateStaticSourceOptions as r, UseAsyncSourceOptions as s, AsyncSource as t };
@@ -1,4 +1,100 @@
1
- import { computed, onScopeDispose, ref, watch } from "vue";
1
+ import { p as isImeKeyEvent } from "./interactionModality-B5dR7nnb.js";
2
+ import { computed, nextTick, onScopeDispose, ref, useId, watch } from "vue";
3
+ //#region src/composables/internal/comboboxFieldProps.ts
4
+ function hasModifier(event) {
5
+ return event.altKey || event.ctrlKey || event.metaKey || event.shiftKey;
6
+ }
7
+ /**
8
+ * 调用方自己渲染输入框、列表和选项时,把组合框语义、按键和指针模型接上去的属性生成器。
9
+ * 焦点始终留在输入框里,高亮的选项由 `aria-activedescendant` 指出;输入框保留列表用不到的所有按键。
10
+ */
11
+ function createComboboxFieldProps(host) {
12
+ const getItemId = (index) => `${host.listboxId}-option-${index}`;
13
+ /** 列表只在有选项时才渲染:打开了但一个选项也没有时显示的是调用方的空状态,它不是 listbox。 */
14
+ const isShown = () => host.isOpen.value && host.options.value.length > 0;
15
+ function enabledIndices() {
16
+ return host.options.value.flatMap((option, index) => option.disabled ? [] : [index]);
17
+ }
18
+ function onFieldKeyDown(event) {
19
+ if (!host.isEnabled.value || isImeKeyEvent(event) || hasModifier(event) && event.key !== "Escape") return;
20
+ const enabled = enabledIndices();
21
+ if (!host.isOpen.value) {
22
+ if (event.key === "ArrowDown" && host.options.value.length > 0) {
23
+ event.preventDefault();
24
+ host.open();
25
+ host.activeIndex.value = enabled[0] ?? -1;
26
+ }
27
+ return;
28
+ }
29
+ switch (event.key) {
30
+ case "ArrowDown":
31
+ case "ArrowUp": {
32
+ event.preventDefault();
33
+ if (enabled.length === 0) break;
34
+ const position = enabled.indexOf(host.activeIndex.value);
35
+ const next = event.key === "ArrowDown" ? (position + 1) % enabled.length : position < 0 ? enabled.length - 1 : (position - 1 + enabled.length) % enabled.length;
36
+ host.activeIndex.value = enabled[next];
37
+ break;
38
+ }
39
+ case "Enter": {
40
+ const option = host.activeIndex.value >= 0 ? host.options.value[host.activeIndex.value] : void 0;
41
+ if (option && !option.disabled) {
42
+ event.preventDefault();
43
+ host.select(option.value);
44
+ }
45
+ break;
46
+ }
47
+ case "Escape":
48
+ event.preventDefault();
49
+ host.close();
50
+ }
51
+ }
52
+ function getFieldProps(handlers = {}) {
53
+ const shown = isShown();
54
+ const active = host.activeIndex.value;
55
+ return {
56
+ "role": "combobox",
57
+ "aria-autocomplete": "list",
58
+ "aria-expanded": shown,
59
+ "aria-controls": shown ? host.listboxId : void 0,
60
+ "aria-activedescendant": shown && active >= 0 && active < host.options.value.length ? getItemId(active) : void 0,
61
+ "onKeydown": (event) => {
62
+ onFieldKeyDown(event);
63
+ if (!event.defaultPrevented) handlers.onKeydown?.(event);
64
+ }
65
+ };
66
+ }
67
+ function getListboxProps() {
68
+ return {
69
+ id: host.listboxId,
70
+ role: "listbox",
71
+ onMousedown: (event) => event.preventDefault()
72
+ };
73
+ }
74
+ function getOptionProps(index) {
75
+ const option = host.options.value[index];
76
+ const isDisabled = option?.disabled === true;
77
+ return {
78
+ "id": getItemId(index),
79
+ "role": "option",
80
+ "aria-selected": index === host.activeIndex.value,
81
+ ...isDisabled ? { "aria-disabled": true } : {},
82
+ "onMousemove": () => {
83
+ if (option && !isDisabled && index !== host.activeIndex.value) host.activeIndex.value = index;
84
+ },
85
+ "onClick": () => {
86
+ if (option && !isDisabled) host.select(option.value);
87
+ }
88
+ };
89
+ }
90
+ return {
91
+ getItemId,
92
+ getFieldProps,
93
+ getListboxProps,
94
+ getOptionProps
95
+ };
96
+ }
97
+ //#endregion
2
98
  //#region src/composables/internal/typeaheadText.ts
3
99
  /**
4
100
  * 类型跳转(type-to-select)用的文字折叠:忽略大小写、组合符号(U+0300–U+036F)、
@@ -21,6 +117,15 @@ function foldTypeaheadText(text) {
21
117
  function startsWithTypeahead(label, prefix) {
22
118
  return foldTypeaheadText(label).startsWith(prefix);
23
119
  }
120
+ /**
121
+ * 按键是不是 Control 或 Command 快捷键,而不是键入的字符。
122
+ * Windows 上 AltGr 也会报 Control,同时带 AltGraph 修饰;它给出的是用户键入的字母,比如 ł。
123
+ * 单独的 Alt 不算:macOS 的 Option 加字母会组出字符(Option+a 得到 å)。
124
+ */
125
+ function isShortcutChord(event) {
126
+ if (event.metaKey) return true;
127
+ return event.ctrlKey && event.getModifierState?.("AltGraph") !== true;
128
+ }
24
129
  //#endregion
25
130
  //#region src/composables/useCombobox.ts
26
131
  const TYPEAHEAD_RESET_MS = 750;
@@ -29,9 +134,11 @@ const TYPEAHEAD_RESET_MS = 750;
29
134
  * 供 Selector、MultiSelector、Typeahead、CommandPalette 等组件复用。
30
135
  */
31
136
  function useCombobox(options) {
32
- const { options: optionsList, modelValue, isEnabled = ref(true), isSearchable = ref(false), onChange, onOpenChange } = options;
137
+ const { options: optionsList, modelValue, isEnabled = ref(true), isSearchable = ref(false), onChange, onOpenChange, listboxId = `astryx-combobox-${useId()}` } = options;
33
138
  const isOpen = ref(false);
34
139
  const activeIndex = ref(-1);
140
+ /** 查询变化带来的列表变化由查询自己处理高亮,不走「跟着选项走」。 */
141
+ let queryChanging = false;
35
142
  const searchQuery = ref("");
36
143
  let typeaheadBuffer = "";
37
144
  let typeaheadTimer;
@@ -65,6 +172,7 @@ function useCombobox(options) {
65
172
  else open();
66
173
  }
67
174
  function select(value) {
175
+ if (!isEnabled.value) return;
68
176
  onChange(value);
69
177
  close();
70
178
  }
@@ -106,6 +214,11 @@ function useCombobox(options) {
106
214
  }
107
215
  function setSearchQuery(query) {
108
216
  searchQuery.value = query;
217
+ queryChanging = true;
218
+ nextTick(() => {
219
+ queryChanging = false;
220
+ });
221
+ activeIndex.value = -1;
109
222
  if (query && filteredOptions.value.length > 0) highlightFirst();
110
223
  }
111
224
  function jumpToMatch(char) {
@@ -177,7 +290,7 @@ function useCombobox(options) {
177
290
  selectHighlighted();
178
291
  }
179
292
  break;
180
- default: if (!isSearchInput && event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
293
+ default: if (!isSearchInput && event.key.length === 1 && !isShortcutChord(event)) {
181
294
  event.preventDefault();
182
295
  if (!isOpen.value) open();
183
296
  jumpToMatch(event.key);
@@ -190,6 +303,30 @@ function useCombobox(options) {
190
303
  if (index >= 0) activeIndex.value = index;
191
304
  }
192
305
  });
306
+ watch(filteredOptions, (next, previous) => {
307
+ if (queryChanging || !isOpen.value || activeIndex.value < 0 || next === previous) return;
308
+ const enabledIndexOf = (value) => value === void 0 ? -1 : next.findIndex((option) => option.value === value && !option.disabled);
309
+ const stayed = enabledIndexOf(previous[activeIndex.value]?.value);
310
+ if (stayed >= 0) {
311
+ activeIndex.value = stayed;
312
+ return;
313
+ }
314
+ for (let after = activeIndex.value + 1; after < previous.length; after++) {
315
+ const found = enabledIndexOf(previous[after]?.value);
316
+ if (found >= 0) {
317
+ activeIndex.value = found;
318
+ return;
319
+ }
320
+ }
321
+ for (let before = Math.min(activeIndex.value, previous.length) - 1; before >= 0; before--) {
322
+ const found = enabledIndexOf(previous[before]?.value);
323
+ if (found >= 0) {
324
+ activeIndex.value = found;
325
+ return;
326
+ }
327
+ }
328
+ activeIndex.value = -1;
329
+ });
193
330
  onScopeDispose(() => {
194
331
  if (typeaheadTimer !== void 0) clearTimeout(typeaheadTimer);
195
332
  });
@@ -198,6 +335,16 @@ function useCombobox(options) {
198
335
  activeIndex,
199
336
  searchQuery,
200
337
  filteredOptions,
338
+ ...createComboboxFieldProps({
339
+ listboxId,
340
+ isOpen,
341
+ isEnabled,
342
+ activeIndex,
343
+ options: filteredOptions,
344
+ open,
345
+ close,
346
+ select: (value) => select(value)
347
+ }),
201
348
  open,
202
349
  close,
203
350
  toggle,