Skip to content

Commit

Permalink
fix(select): do not emit change event when same value selected twice
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 320435732
  • Loading branch information
allan-chen authored and copybara-github committed Jul 9, 2020
1 parent d850de5 commit e07a708
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/mdc-select/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class MDCSelectFoundation extends MDCFoundation<MDCSelectAdapter> {
// By default, select is invalid if it is required but no value is selected.
private useDefaultValidation = true;
private customValidity = true;
private lastSelectedIndex = numbers.UNSET_INDEX;

/* istanbul ignore next: optional argument is not a branch statement */
/**
Expand Down Expand Up @@ -138,9 +139,10 @@ export class MDCSelectFoundation extends MDCFoundation<MDCSelectAdapter> {
this.adapter.closeMenu();
}

if (!skipNotify) {
if (!skipNotify && this.lastSelectedIndex !== index) {
this.handleChange();
}
this.lastSelectedIndex = index;
}

setValue(value: string) {
Expand Down
3 changes: 3 additions & 0 deletions packages/mdc-select/test/foundation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,9 @@ describe('MDCSelectFoundation', () => {
foundation.setSelectedIndex(1);
expect(mockAdapter.setSelectedIndex).toHaveBeenCalledWith(1);

foundation.setSelectedIndex(0);
// We intentionally call this twice, expecting notifyChange to be called
// only once for these two calls.
foundation.setSelectedIndex(0);
expect(mockAdapter.setSelectedIndex).toHaveBeenCalledWith(0);

Expand Down

0 comments on commit e07a708

Please sign in to comment.