Skip to content

Commit

Permalink
chore: update Autocomplete filter to be case insensitive (#3019)
Browse files Browse the repository at this point in the history
* chore: update filter to be case insensitive

* Create grumpy-apricots-sparkle.md
  • Loading branch information
zchenwei committed Nov 20, 2022
1 parent 33b2651 commit 0ff6cbc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-apricots-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aws-amplify/ui-react": patch
---

chore: update Autocomplete filter to be case insensitive
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,21 @@ describe('Autocomplete: ', () => {
);
});

it('should be case insensitive filtering', async () => {
render(<Autocomplete label={label} options={options} />);

const textInput = await screen.findByRole('combobox');
userEvent.type(textInput, 'ap');
let optionElements = await screen.findAllByRole('option');
expect(optionElements).toHaveLength(1);
expect(optionElements[0]).toHaveTextContent('apple');
userEvent.clear(textInput);
userEvent.type(textInput, 'AP');
optionElements = await screen.findAllByRole('option');
expect(optionElements).toHaveLength(1);
expect(optionElements[0]).toHaveTextContent('apple');
});

it('should be able to apply custom filtering', async () => {
const optionFilter = jest.fn();
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export const useAutocomplete = ({
const filteredOptions = React.useMemo(() => {
const defaultFilter = (option: ComboBoxOption) => {
const { label } = option;
return label?.includes(composedValue);
return label
?.toLocaleLowerCase()
.includes(composedValue?.toLocaleLowerCase());
};
const filter = isCustomFiltering
? (option: ComboBoxOption) => optionFilter(option, composedValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export const HighlightMatchPrimitive: Primitive<HighlightMatchProps, 'span'> = (
ref
) => {
const matchTestId = getTestId(testId, 'match');
const startIndex =
children?.toLocaleLowerCase().indexOf(query?.toLocaleLowerCase()) ?? -1;
const startIndex = children
?.toLocaleLowerCase()
.indexOf(query?.toLocaleLowerCase());

if (strHasLength(query) && startIndex !== -1) {
const match = children.substring(startIndex, startIndex + query.length);
Expand Down

0 comments on commit 0ff6cbc

Please sign in to comment.