@@ -60,7 +60,7 @@ const emit = defineEmits<{
6060}>()
6161
6262provide (' theme' , props .theme || ' default' )
63- const { selectedNode, search, dataValue, filtered } = useCommandState ()
63+ const { selectedNode, search, filtered } = useCommandState ()
6464const { emitter } = useCommandEvent ()
6565
6666const commandRef = ref <HTMLDivElement >()
@@ -110,6 +110,20 @@ const getSelectedItem = () => {
110110 return commandRef .value ?.querySelector (SELECTED_ITEM_SELECTOR )
111111}
112112
113+ const getAllGroups = () => {
114+ const allGroupEl = commandRef .value ?.querySelectorAll (
115+ GROUP_SELECTOR
116+ ) as NodeListOf <HTMLElement >
117+ return allGroupEl ? Array .from (allGroupEl ) : []
118+ }
119+
120+ const getAllItems = (rootNode : HTMLElement | undefined = commandRef .value ) => {
121+ const allItemEl = rootNode ?.querySelectorAll (
122+ ITEM_SELECTOR
123+ ) as NodeListOf <HTMLElement >
124+ return allItemEl ? Array .from (allItemEl ) : []
125+ }
126+
113127const getValidItems = (
114128 rootNode : HTMLElement | undefined = commandRef .value
115129) => {
@@ -119,13 +133,6 @@ const getValidItems = (
119133 return allItemEl ? Array .from (allItemEl ) : []
120134}
121135
122- const getValidGroups = () => {
123- const allGroupEl = commandRef .value ?.querySelectorAll (
124- GROUP_SELECTOR
125- ) as NodeListOf <HTMLElement >
126- return allGroupEl ? Array .from (allGroupEl ) : []
127- }
128-
129136const selectedFirstItem = () => {
130137 const [firstItem] = getValidItems ()
131138 if (firstItem && firstItem .getAttribute (ITEM_KEY_SELECTOR )) {
@@ -279,7 +286,7 @@ const filterItems = () => {
279286 }
280287
281288 // Reset the groups
282- filtered .value .groups = new Set (' ' )
289+ filtered .value .groups = new Set ()
283290
284291 const items = new Map ()
285292
@@ -299,15 +306,13 @@ const filterItems = () => {
299306 }
300307 }
301308
302- nextTick (() => {
303- filtered .value .count = items .size
304- filtered .value .items = items
305- })
309+ filtered .value .count = items .size
310+ filtered .value .items = items
306311}
307312
308313const initStore = () => {
309- const items = getValidItems ()
310- const groups = getValidGroups ()
314+ const groups = getAllGroups ()
315+ const items = getAllItems ()
311316
312317 for (const item of items ) {
313318 const itemKey = item .getAttribute (ITEM_KEY_SELECTOR ) || ' '
@@ -319,7 +324,7 @@ const initStore = () => {
319324
320325 // map the items in group
321326 for (const group of groups ) {
322- const itemsInGroup = getValidItems (group )
327+ const itemsInGroup = getAllItems (group )
323328 const groupId = group .getAttribute (GROUP_KEY_SELECTOR ) || ' '
324329 const itemIds = new Set (' ' )
325330
@@ -343,12 +348,13 @@ watch(
343348 },
344349 { deep: true }
345350)
351+
346352/**
347353 * when search's value is changed, trigger filter action
348354 */
349355watch (
350356 () => search .value ,
351- (newVal ) => {
357+ () => {
352358 filterItems ()
353359 nextTick (selectedFirstItem )
354360 }
@@ -361,14 +367,17 @@ emitter.on('selectItem', (item) => {
361367const debouncedEmit = useDebounceFn ((isRerender : Boolean ) => {
362368 if (isRerender ) {
363369 initStore ()
364- nextTick (selectedFirstItem )
370+ nextTick (() => {
371+ filterItems ()
372+ selectedFirstItem ()
373+ })
365374 }
366375}, 100 )
367376
368377emitter .on (' rerenderList' , debouncedEmit )
369378
370379onMounted (() => {
371380 initStore ()
372- selectedFirstItem ( )
381+ nextTick ( selectedFirstItem )
373382})
374383 </script >
0 commit comments