Skip to content

fix(chips): default click action on chip being prevented #12856

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

Merged
merged 1 commit into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,11 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
* Implemented as part of MatFormFieldControl.
* @docs-private
*/
onContainerClick() { this.focus(); }
onContainerClick(event: MouseEvent) {
if (!this._originatesFromChip(event)) {
this.focus();
}
}

/**
* Focuses the the first non-disabled chip in this chip list, or the associated input when there
Expand Down Expand Up @@ -739,4 +743,19 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
}
});
}

/** Checks whether an event comes from inside a chip element. */
private _originatesFromChip(event: Event): boolean {
let currentElement = event.target as HTMLElement | null;

while (currentElement && currentElement !== this._elementRef.nativeElement) {
if (currentElement.classList.contains('mat-chip')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we also check if it originates from the chip input?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's not necessary, because the focus method checks whether the input is focused.

return true;
}

currentElement = currentElement.parentElement;
}

return false;
}
}
20 changes: 19 additions & 1 deletion src/lib/chips/chip.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Directionality} from '@angular/cdk/bidi';
import {BACKSPACE, DELETE, SPACE} from '@angular/cdk/keycodes';
import {createKeyboardEvent} from '@angular/cdk/testing';
import {createKeyboardEvent, dispatchFakeEvent} from '@angular/cdk/testing';
import {Component, DebugElement} from '@angular/core';
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
Expand Down Expand Up @@ -132,6 +132,24 @@ describe('Chips', () => {

expect(testComponent.chipRemove).toHaveBeenCalledWith({chip: chipInstance});
});

it('should not prevent the default click action', () => {
const event = dispatchFakeEvent(chipNativeElement, 'click');
fixture.detectChanges();

expect(event.defaultPrevented).toBe(false);
});

it('should prevent the default click action when the chip is disabled', () => {
chipInstance.disabled = true;
fixture.detectChanges();

const event = dispatchFakeEvent(chipNativeElement, 'click');
fixture.detectChanges();

expect(event.defaultPrevented).toBe(true);
});

});

describe('keyboard behavior', () => {
Expand Down
10 changes: 4 additions & 6 deletions src/lib/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,13 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
}
}

/** Ensures events fire properly upon click. */
/** Handles click events on the chip. */
_handleClick(event: Event) {
// Check disabled
if (this.disabled) {
return;
event.preventDefault();
} else {
event.stopPropagation();
}

event.preventDefault();
event.stopPropagation();
}

/** Handle custom key presses. */
Expand Down