File tree Expand file tree Collapse file tree
src/@next/Select/components/Activator Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import React from 'react' ;
2+ import { fireEvent , render , screen } from '@testing-library/react' ;
3+
4+ import { ActivatorTextInput } from './ActivatorTextInput' ;
5+
6+ const options = [
7+ { label : 'Singapore (Central)' , value : 'central' } ,
8+ { label : 'St. John' , value : 'st-john' } ,
9+ { label : 'Seoul' , value : 'seoul' } ,
10+ ] ;
11+
12+ test . each ( [
13+ [ '(' , 'central' ] ,
14+ [ '.' , 'st-john' ] ,
15+ [ 'SINGAPORE' , 'central' ] ,
16+ ] ) ( 'filters search text %s literally and ignores case' , ( query , expected ) => {
17+ const updateMenuOptions = jest . fn ( ) ;
18+ render (
19+ < ActivatorTextInput
20+ placeholder = "Search"
21+ inputValue = ""
22+ selectedValues = { [ ] }
23+ options = { options }
24+ onFocus = { jest . fn ( ) }
25+ updateInputValue = { jest . fn ( ) }
26+ searchableSelectState = { {
27+ showSelected : false ,
28+ showInput : true ,
29+ showPlaceholder : true ,
30+ } }
31+ updateSearchableSelectState = { jest . fn ( ) }
32+ updateMenuOptions = { updateMenuOptions }
33+ />
34+ ) ;
35+
36+ fireEvent . change ( screen . getByRole ( 'textbox' ) , { target : { value : query } } ) ;
37+
38+ expect ( updateMenuOptions ) . toHaveBeenLastCalledWith (
39+ options . filter ( option => option . value === expected )
40+ ) ;
41+ } ) ;
Original file line number Diff line number Diff line change @@ -56,18 +56,18 @@ export const ActivatorTextInput = ({
5656 const activatorRef = useRef ( null ) ;
5757 const [ hasSelectedValues , setHasSelectedValues ] = useState ( false ) ;
5858
59- const filterOptions = ( str : string ) => {
60- if ( str === '' ) {
59+ const filterOptions = ( query : string ) => {
60+ if ( query === '' ) {
6161 updateMenuOptions ( options ) ;
6262 return options ;
6363 }
6464
65- const filterRegex = new RegExp ( str , 'i' ) ;
66- const filterOptions = options . filter ( ( option : Option ) =>
67- ( option . label as string ) . match ( filterRegex )
65+ const normalizedQuery = query . toLowerCase ( ) ;
66+ return options . filter (
67+ ( { label } ) =>
68+ typeof label === 'string' &&
69+ label . toLowerCase ( ) . includes ( normalizedQuery )
6870 ) ;
69-
70- return filterOptions ;
7171 } ;
7272
7373 const handleChange = ( { value } : { value : string } ) => {
You can’t perform that action at this time.
0 commit comments