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

feat(autocomplete): emit event when an option is selected #4187

Merged
merged 2 commits into from
Aug 23, 2017

Conversation

crisbeto
Copy link
Member

Emits the select event when an option in the autocomplete is selected.

Note: I went with passing the selected option from the trigger to the panel, instead of listening to the onSelectionChange inside the panel, because it involves keeping track of less subscriptions and not having to re-construct them when the list of options changes.

Fixes #4094.
Fixes #3645.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Apr 20, 2017
@@ -50,6 +58,9 @@ export class MdAutocomplete implements AfterContentInit {
/** Function that maps an option's control value to its display value in the trigger. */
@Input() displayWith: (value: any) => string;

/** Event that is emitted whenever an option from the list is selected. */
@Output() select: EventEmitter<MdAutocompleteSelect> = new EventEmitter<MdAutocompleteSelect>();
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we just expose the Observable instead of the EventEmitter?

Also as a side-question: What about a change event?

Copy link
Member Author

Choose a reason for hiding this comment

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

I wanted to avoid change, because typing in the select also changes the value. Regarding the event, using the EventEmitter is the way to handle @Output.

Copy link
Member

@devversion devversion Apr 20, 2017

Choose a reason for hiding this comment

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

@Output can be basically any Observable. EventEmitter is just like an RxJS Subscription. We originally changed all @Outputs to just expose the Observable.

Actually it looks like a lot of components don't do that. @jelbourn any reason why we didn't continue just exposing the observable? I can go through them and fix the remaining ones.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is pretty much the only use for the EventEmitter: https://angular.io/docs/ts/latest/api/core/index/EventEmitter-class.html

Copy link
Contributor

Choose a reason for hiding this comment

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

@crisbeto @devversion is this resolved? I don't want this PR to be forgotten!

Choose a reason for hiding this comment

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

+1 to have fix this problem :). I would to know when this PR will be accepted. Thank you

@willshowell
Copy link
Contributor

@crisbeto think you could rebase this in anticipation of @kara's review? Very much looking forward to a solution better than,

    this.trigger.autocomplete.options.changes
      .startWith(null)
      .switchMap(() => this.trigger.optionSelections)
      .map(s => s.source)
      .filter(s => s.selected)

Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

Naming quibbles, but otherwise looks good. Can you rebase as well?

@@ -1146,6 +1147,29 @@ describe('MdAutocomplete', () => {
expect(panel.classList).toContain(visibleClass, `Expected panel to be visible.`);
});
}));

it('should call emit an event when an option is selected', fakeAsync(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: 'should call emit an event' -> 'should emit an event'

/**
* Autocomplete IDs need to be unique across components, so this counter exists outside of
* the component definition.
*/
let _uniqueAutocompleteIdCounter = 0;

/** Event object that is emitted when an autocomplete option is selected */
export class MdAutocompleteSelect {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about MdAutocompleteSelectEvent ? Without "Event", it almost sounds like a hybrid md-autocomplete / md-select directive.

@@ -50,6 +58,9 @@ export class MdAutocomplete implements AfterContentInit {
/** Function that maps an option's control value to its display value in the trigger. */
@Input() displayWith: (value: any) => string;

/** Event that is emitted whenever an option from the list is selected. */
@Output() select: EventEmitter<MdAutocompleteSelect> = new EventEmitter<MdAutocompleteSelect>();
Copy link
Contributor

Choose a reason for hiding this comment

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

@jelbourn and I discussed and we like (optionSelected). It's a bit more explicit, and otherwise it sounds a bit too much like md-select.

let event = spy.calls.mostRecent().args[0] as MdAutocompleteSelect;

expect(event.source).toBe(fixture.componentInstance.autocomplete);
expect(event.option.value).toBe('Washington');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test for when the option list changes? e.g. emitting an event from a new option.

@crisbeto
Copy link
Member Author

Thanks for the feedback @kara. It's addressed now, can you take another look?

Copy link
Contributor

@kara kara left a comment

Choose a reason for hiding this comment

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

LGTM. @crisbeto Apply merge label when ready.

constructor(public source: MdAutocomplete, public option: MdOption) { }
}

export type AutocompletePositionY = 'above' | 'below';
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is this being used? Seems unrelated to this PR.

@kara kara assigned crisbeto and unassigned kara Aug 22, 2017
Emits the `select` event when an option in the autocomplete is selected.

**Note:** I went with passing the selected option from the trigger to the panel, instead of listening to the `onSelectionChange` inside the panel, because it involves keeping track of less subscriptions and not having to re-construct them when the list of options changes.

Fixes angular#4094.
Fixes angular#3645.
@crisbeto crisbeto force-pushed the 4094/autocomplete-select-event branch from 0adbc33 to e60439f Compare August 23, 2017 06:36
@crisbeto crisbeto added the action: merge The PR is ready for merge by the caretaker label Aug 23, 2017
@kara kara merged commit 2dd5c7c into angular:master Aug 23, 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
6 participants