@@ -61,6 +61,21 @@ type CommandRootStateProps = WithRefProps<
6161 } >
6262> ;
6363
64+ const defaultState = {
65+ /** Value of the search query */
66+ search : "" ,
67+ /** Currently selected item value */
68+ value : "" ,
69+ filtered : {
70+ /** The count of all visible items. */
71+ count : 0 ,
72+ /** Map from visible item id to its search store. */
73+ items : new Map < string , number > ( ) ,
74+ /** Set of groups with at least one visible item. */
75+ groups : new Set < string > ( ) ,
76+ } ,
77+ } ;
78+
6479class CommandRootState {
6580 #updateScheduled = false ;
6681 sortAfterTick = false ;
@@ -74,9 +89,9 @@ class CommandRootState {
7489 inputNode = $state < HTMLElement | null > ( null ) ;
7590 labelNode = $state < HTMLElement | null > ( null ) ;
7691 // published state that the components and other things can react to
77- commandState = $state . raw < CommandState > ( null ! ) ;
92+ commandState = $state . raw < CommandState > ( defaultState ) ;
7893 // internal state that we mutate in batches and publish to the `state` at once
79- _commandState = $state < CommandState > ( null ! ) ;
94+ _commandState = $state < CommandState > ( defaultState ) ;
8095 // whether the search has had a value other than ""
8196 searchHasHadValue = $state ( false ) ;
8297
@@ -126,23 +141,10 @@ class CommandRootState {
126141 }
127142
128143 constructor ( readonly opts : CommandRootStateProps ) {
129- const defaultState = {
130- /** Value of the search query */
131- search : "" ,
132- /** Currently selected item value */
133- value : this . opts . value . current ?? "" ,
134- filtered : {
135- /** The count of all visible items. */
136- count : 0 ,
137- /** Map from visible item id to its search store. */
138- items : new Map < string , number > ( ) ,
139- /** Set of groups with at least one visible item. */
140- groups : new Set < string > ( ) ,
141- } ,
142- } ;
144+ const defaults = { ...this . _commandState , value : this . opts . value . current ?? "" } ;
143145
144- this . _commandState = defaultState ;
145- this . commandState = defaultState ;
146+ this . _commandState = defaults ;
147+ this . commandState = defaults ;
146148
147149 useRefById ( opts ) ;
148150
0 commit comments