Skip to content

Commit f01f474

Browse files
committed
fix: match searchable Select queries literally
1 parent ab1daff commit f01f474

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
});

‎src/@next/Select/components/Activator/ActivatorTextInput.tsx‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff 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 }) => {

0 commit comments

Comments
 (0)