Skip to content

Commit

Permalink
fix(react/autocomplete): escape all special characters for search text,
Browse files Browse the repository at this point in the history
closed #217
  • Loading branch information
travor20814 committed Jan 10, 2024
1 parent d39e593 commit 8285356
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/react/src/Form/useAutoCompleteValueControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ function useAutoCompleteBaseValueControl(props: UseAutoCompleteValueControl) {
const [searchText, setSearchText] = useState<string>('');
const [focused, setFocused] = useState<boolean>(false);

const searchTextReg = new RegExp(searchText.replace(/\\/g, '\\\\'), 'i' );
/** escape all special characters */
const searchTextReg = new RegExp(searchText
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d'));

const onFocus = useCallback((focus: boolean) => {
setFocused(focus);
Expand Down
3 changes: 3 additions & 0 deletions packages/react/src/Select/AutoComplete.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const originOptions: SelectValue[] = [{
}, {
id: 'very very very long',
name: 'very very very long',
}, {
id: '?><!@#$^$&^&',
name: '?><!@#$^$&^&',
}];

export const Basic = () => (
Expand Down

0 comments on commit 8285356

Please sign in to comment.