Skip to content

Commit

Permalink
Add escapeRegex string utility (#11818)
Browse files Browse the repository at this point in the history
### WHY are these changes introduced?

The picker was exploding when special characters were getting typed into
the search field.

### WHAT is this pull request doing?

Adds a string util to escape special characters. Internal use only so
skipping changeset

### How to 🎩

Open the Picker and enter open bracket, square bracket, parenthesis
etc.. in the search field:
[Storybook](https://5d559397bae39100201eedc1-qccucmxydk.chromatic.com/?path=/story/playground--details-page)
  • Loading branch information
kyledurand authored Apr 2, 2024
1 parent 2ff9427 commit 8ffddd6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion polaris-react/src/components/Picker/Picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {ListboxProps, OptionProps} from '../Listbox';
import {Listbox} from '../Listbox';
import type {IconProps} from '../Icon';
import {Icon} from '../Icon';
import {escapeRegex} from '../../utilities/string';

import {Activator, SearchField} from './components';
import type {ActivatorProps} from './components';
Expand All @@ -44,7 +45,8 @@ export interface PickerProps extends Omit<ListboxProps, 'children'> {
}

const FILTER_REGEX = (value: string) => new RegExp(value, 'i');
const QUERY_REGEX = (value: string) => new RegExp(`^${value}$`, 'i');
const QUERY_REGEX = (value: string) =>
new RegExp(`^${escapeRegex(value)}$`, 'i');

export function Picker({
activator,
Expand Down
3 changes: 3 additions & 0 deletions polaris-react/src/utilities/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function escapeRegex(str: string) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

0 comments on commit 8ffddd6

Please sign in to comment.