-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Conversation
src/lib/autocomplete/autocomplete.ts
Outdated
@@ -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>(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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 @Output
s 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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
There was a problem hiding this 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(() => { |
There was a problem hiding this comment.
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'
src/lib/autocomplete/autocomplete.ts
Outdated
/** | ||
* 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 { |
There was a problem hiding this comment.
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.
src/lib/autocomplete/autocomplete.ts
Outdated
@@ -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>(); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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.
21e4ae2
to
0adbc33
Compare
Thanks for the feedback @kara. It's addressed now, can you take another look? |
There was a problem hiding this 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.
src/lib/autocomplete/autocomplete.ts
Outdated
constructor(public source: MdAutocomplete, public option: MdOption) { } | ||
} | ||
|
||
export type AutocompletePositionY = 'above' | 'below'; |
There was a problem hiding this comment.
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.
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.
0adbc33
to
e60439f
Compare
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
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.