diff --git a/src/demo-app/autocomplete/autocomplete-demo.ts b/src/demo-app/autocomplete/autocomplete-demo.ts index 7f9ed2de234b..00fbeeb1a15c 100644 --- a/src/demo-app/autocomplete/autocomplete-demo.ts +++ b/src/demo-app/autocomplete/autocomplete-demo.ts @@ -88,7 +88,8 @@ export class AutocompleteDemo { } filterStates(val: string) { - return val ? this.states.filter((s) => s.name.match(new RegExp(val, 'gi'))) : this.states; + return val ? this.states.filter(s => new RegExp(`^${val}`, 'gi').test(s.name)) + : this.states; } } diff --git a/src/examples/autocomplete-overview/autocomplete-overview-example.ts b/src/examples/autocomplete-overview/autocomplete-overview-example.ts index 515e35e9e501..7b28b28b998b 100644 --- a/src/examples/autocomplete-overview/autocomplete-overview-example.ts +++ b/src/examples/autocomplete-overview/autocomplete-overview-example.ts @@ -71,7 +71,8 @@ export class AutocompleteOverviewExample { } filterStates(val: string) { - return val ? this.states.filter((s) => new RegExp(val, 'gi').test(s)) : this.states; + return val ? this.states.filter(s => new RegExp(`^${val}`, 'gi').test(s)) + : this.states; } } diff --git a/src/lib/autocomplete/autocomplete.md b/src/lib/autocomplete/autocomplete.md index 19f4acbededa..7d2884c48fa7 100644 --- a/src/lib/autocomplete/autocomplete.md +++ b/src/lib/autocomplete/autocomplete.md @@ -48,15 +48,19 @@ local template variable (here we called it "auto"), and binding that variable to At this point, the autocomplete panel should be toggleable on focus and options should be selectable. But if we want our options to filter when we type, we need to add a custom filter. -You can filter the options in any way you want based on the text input. Here we will do a simple string test on the -input value to see if it matches the option value. We already have access to the built-in `valueChanges` observable on -the `FormControl`, so we can simply map the text input's values to the suggested options by passing them through this -filter. The resulting observable (`filteredOptions`) can be added to the template in place of the `options` property -using the `async` pipe. +You can filter the options in any way you like based on the text input*. Here we will perform a simple string test on +the option value to see if it matches the input value, starting from the option's first letter. We already have access +to the built-in `valueChanges` observable on the `FormControl`, so we can simply map the text input's values to the +suggested options by passing them through this filter. The resulting observable (`filteredOptions`) can be added to the +template in place of the `options` property using the `async` pipe. Below we are also priming our value change stream with `null` so that the options are filtered by that value on init (before there are any value changes). +*For optimal accessibility, you may want to consider adding text guidance on the page to explain filter criteria. +This is especially helpful for screenreader users if you're using a non-standard filter that doesn't limit matches +to the beginning of the string. + *my-comp.ts* ```ts class MyComp { @@ -75,7 +79,7 @@ class MyComp { } filter(val: string): string[] { - return this.options.filter(option => new RegExp(val, 'gi').test(option)); + return this.options.filter(option => new RegExp(`^${val}`, 'gi').test(option)); } } ``` @@ -134,7 +138,7 @@ class MyComp { } filter(name: string): User[] { - return this.options.filter(option => new RegExp(name, 'gi').test(option)); + return this.options.filter(option => new RegExp(`^${name}`, 'gi').test(option)); } displayFn(user: User): string {