Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(autocomplete): add autocomplete docs #2840

Merged
merged 1 commit into from
Jan 28, 2017

Conversation

kara
Copy link
Contributor

@kara kara commented Jan 27, 2017

Docs site example to come once examples live here.

r: @jelbourn @andrewseguin

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jan 27, 2017
```ts
class MyComp {
myControl = new FormControl();
options = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a real type here and get rid of any throughout? Maybe something like new User('Mary', 'Shelly')

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.

We already have access to the built-in `valueChanges` observable on the `FormControl`, so we can simply map the text
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit like you're saying "This is the One True Way to do filtering". What do you think of approaching more as "You can filter the options in any way you want based on the text input. Here's one example."

the option's string properties.

To make this work, create a function on your component class that maps the control value to the desired display value.
Then bind it to the autocomplete's `displayWith` property. You'll probably also want to add a step to your
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably also want to => You can

}

displayFn(value: any): string {
return value && typeof value === 'object' ? value.name : value;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is typeof needed here?

}

filter(val: string): any[] {
return this.options.filter(option => option.match(new RegExp(val, 'gi')));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You typically don't want to use match when you are looking for a yes/no result.
http://stackoverflow.com/a/10940138/477102

@kara
Copy link
Contributor Author

kara commented Jan 28, 2017

@jelbourn Comments addressed

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Jan 28, 2017
@jelbourn
Copy link
Member

LGTM

@kara kara merged commit 88b54ae into angular:autocomplete Jan 28, 2017
kara added a commit to kara/material2 that referenced this pull request Feb 3, 2017
kara added a commit that referenced this pull request Feb 3, 2017
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants