File tree 3 files changed +30
-4
lines changed
packages/kit/src/input-search
3 files changed +30
-4
lines changed Original file line number Diff line number Diff line change @@ -65,7 +65,7 @@ export const InputSearchInput = React.forwardRef<
65
65
if ( value ) {
66
66
inputProps . value = value ;
67
67
}
68
- if ( autocomplete && tempText !== undefined ) {
68
+ if ( autocomplete && withKeyboard . current ) {
69
69
inputProps . value = tempText ;
70
70
}
71
71
Original file line number Diff line number Diff line change @@ -100,11 +100,17 @@ export const InputSearchRoot = ({
100
100
state
101
101
) ;
102
102
103
+ const prevSelectedKey = React . useRef ( state . selectedKey ) ;
103
104
React . useEffect ( ( ) => {
104
- if ( state . selectedItem && onSelect ) {
105
+ if (
106
+ state . selectedItem &&
107
+ onSelect &&
108
+ prevSelectedKey . current !== state . selectedKey
109
+ ) {
105
110
onSelect (
106
111
state . selectedItem . textValue || ( state . selectedItem . rendered as string )
107
112
) ;
113
+ prevSelectedKey . current = state . selectedKey ;
108
114
}
109
115
} , [ state . selectedItem , onSelect ] ) ;
110
116
Original file line number Diff line number Diff line change @@ -28,8 +28,8 @@ const useCityMatch = (term: string) => {
28
28
term . trim ( ) === ""
29
29
? null
30
30
: matchSorter ( cities , term , {
31
- keys : [ ( item ) => `${ item . city } , ${ item . state } ` ] ,
32
- } ) ,
31
+ keys : [ ( item ) => `${ item . city } , ${ item . state } ` ] ,
32
+ } ) ,
33
33
[ term ]
34
34
) ;
35
35
} ;
@@ -292,3 +292,23 @@ export const Interactions = {
292
292
await expect ( input ) . toHaveDisplayValue ( "Apple" ) ;
293
293
} ,
294
294
} ;
295
+
296
+ export const ControlledKeyboardInteractions = {
297
+ render : ControlledTemplate ,
298
+
299
+ play : async ( ) => {
300
+ const input = await screen . findByLabelText ( "Search" ) ;
301
+ await userEvent . type ( input , "app" , {
302
+ delay : 100 ,
303
+ } ) ;
304
+ await userEvent . keyboard ( "[ArrowDown]" ) ;
305
+ await userEvent . keyboard ( "[ArrowDown]" ) ;
306
+ await userEvent . keyboard ( "[ArrowDown]" ) ;
307
+ await expect ( input ) . toHaveDisplayValue ( "Orange" ) ;
308
+ await userEvent . keyboard ( "[Backspace]" ) ;
309
+ await expect ( input ) . toHaveDisplayValue ( "Orang" ) ;
310
+ const clearButton = await screen . findByText ( "Clear" ) ;
311
+ await userEvent . click ( clearButton ) ;
312
+ await expect ( input ) . toHaveDisplayValue ( "" ) ;
313
+ } ,
314
+ } ;
You can’t perform that action at this time.
0 commit comments