File tree 3 files changed +81
-2
lines changed
packages/kit/src/input-search
3 files changed +81
-2
lines changed Original file line number Diff line number Diff line change @@ -208,3 +208,38 @@ describe("InputSearchInput", () => {
208
208
expect ( inputElement ) . toHaveValue ( "Test item" ) ;
209
209
} ) ;
210
210
} ) ;
211
+
212
+ describe ( "InputSearchInput - Controlled" , ( ) => {
213
+ test ( "updates the value when an item is highlighted" , async ( ) => {
214
+ const user = userEvent . setup ( ) ;
215
+ const changeSpy = jest . fn ( ) ;
216
+ render (
217
+ < InputSearchRoot aria-label = "Example-Search" openOnFocus >
218
+ < InputSearchInput
219
+ name = "fruit"
220
+ id = "fruit"
221
+ value = { "" }
222
+ onChange = { ( event ) => {
223
+ changeSpy ( event . target . value ) ;
224
+ } }
225
+ />
226
+ < InputSearchPopover >
227
+ < InputSearchList >
228
+ < InputSearchListItem value = "Apple" />
229
+ < InputSearchListItem value = "Banana" />
230
+ < InputSearchListItem value = "Orange" />
231
+ < InputSearchListItem value = "Kiwi" />
232
+ < InputSearchListItem value = "Pineapple" />
233
+ </ InputSearchList >
234
+ </ InputSearchPopover >
235
+ </ InputSearchRoot >
236
+ ) ;
237
+
238
+ const inputElement = screen . getByRole ( "combobox" ) ;
239
+ await user . click ( inputElement ) ;
240
+ await user . keyboard ( "T" ) ;
241
+ await user . keyboard ( "[ArrowDown]" ) ;
242
+
243
+ expect ( inputElement ) . toHaveValue ( "Apple" ) ;
244
+ } ) ;
245
+ } ) ;
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ export const InputSearchInput = React.forwardRef<
25
25
label = "Search" ,
26
26
autocomplete = true ,
27
27
id,
28
+ value,
28
29
...rest
29
30
} : InputSearchInputProps ,
30
31
ref
@@ -61,8 +62,8 @@ export const InputSearchInput = React.forwardRef<
61
62
}
62
63
} , [ state . selectionManager . focusedKey , setTempText ] ) ;
63
64
64
- if ( rest . value ) {
65
- inputProps . value = rest . value ;
65
+ if ( value ) {
66
+ inputProps . value = value ;
66
67
}
67
68
if ( autocomplete && tempText !== undefined ) {
68
69
inputProps . value = tempText ;
Original file line number Diff line number Diff line change @@ -220,6 +220,49 @@ export const Scroll = {
220
220
} ,
221
221
} ;
222
222
223
+ const ControlledTemplate : StoryFn < typeof InputSearch . Root > = ( args ) => {
224
+ const [ term , setTerm ] = useState ( "" ) ;
225
+ return (
226
+ < Box css = { { width : "275px" } } >
227
+ < InputSearch . Root
228
+ { ...args }
229
+ aria-label = "Example-Search"
230
+ openOnFocus
231
+ onSelect = { ( value ) => {
232
+ //setTerm(value);
233
+ } }
234
+ >
235
+ < InputSearch . Input
236
+ name = "fruit"
237
+ id = "fruit"
238
+ value = { term }
239
+ onChange = { ( event ) => {
240
+ setTerm ( event . target . value ) ;
241
+ } }
242
+ />
243
+ < InputSearch . Popover >
244
+ < InputSearch . List >
245
+ < InputSearch . ListItem value = "Apple" />
246
+ < InputSearch . ListItem value = "Banana" />
247
+ < InputSearch . ListItem value = "Orange" />
248
+ < InputSearch . ListItem value = "Kiwi" />
249
+ < InputSearch . ListItem value = "Pineapple" />
250
+ </ InputSearch . List >
251
+ </ InputSearch . Popover >
252
+ </ InputSearch . Root >
253
+ </ Box >
254
+ ) ;
255
+ } ;
256
+
257
+ export const Controlled = {
258
+ render : ControlledTemplate ,
259
+ args : { } ,
260
+
261
+ parameters : {
262
+ chromatic : { disableSnapshot : true } ,
263
+ } ,
264
+ } ;
265
+
223
266
const InteractionsTemplate : StoryFn < typeof InputSearch . Root > = ( ) => (
224
267
< Box css = { { width : "275px" , height : "340px" } } >
225
268
< InputSearch . Root aria-label = "Example-Search" openOnFocus >
You can’t perform that action at this time.
0 commit comments