Skip to content

fix(chips): Fix chip and chip list selectable #9955

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
Feb 18, 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
12 changes: 11 additions & 1 deletion src/lib/chips/chip-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ describe('MatChipList', () => {
it('should add the `mat-chip-list` class', () => {
expect(chipListNativeElement.classList).toContain('mat-chip-list');
});

it('should not have the aria-selected attribute when is not selectable', () => {
testComponent.selectable = false;
fixture.detectChanges();

const chipsValid = chips.toArray().every(chip =>
!chip.selectable && !chip._elementRef.nativeElement.hasAttribute('aria-selected'));

expect(chipsValid).toBe(true);
});
});

describe('with selected chips', () => {
Expand Down Expand Up @@ -1027,7 +1037,7 @@ describe('MatChipList', () => {

@Component({
template: `
<mat-chip-list [tabIndex]="tabIndex">
<mat-chip-list [tabIndex]="tabIndex" [selectable]="selectable">
<div *ngFor="let i of [0,1,2,3,4]">
<div *ngIf="remove != i">
<mat-chip (select)="chipSelect(i)" (deselect)="chipDeselect(i)">
Expand Down
11 changes: 8 additions & 3 deletions src/lib/chips/chip-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,17 @@ export class MatChipList extends _MatChipListMixinBase implements MatFormFieldCo
@Input('aria-orientation') ariaOrientation: 'horizontal' | 'vertical' = 'horizontal';

/**
* Whether or not this chip is selectable. When a chip is not selectable,
* its selected state is always ignored.
* Whether or not this chip list is selectable. When a chip list is not selectable,
* the selected states for all the chips inside the chip list are always ignored.
*/
@Input()
get selectable(): boolean { return this._selectable; }
set selectable(value: boolean) { this._selectable = coerceBooleanProperty(value); }
set selectable(value: boolean) {
this._selectable = coerceBooleanProperty(value);
if (this.chips) {
this.chips.forEach(chip => chip.chipListSelectable = this._selectable);
}
}
protected _selectable: boolean = true;

@Input()
Expand Down
11 changes: 8 additions & 3 deletions src/lib/chips/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
/** Whether the chip has focus. */
_hasFocus: boolean = false;

/** Whether the chip list is selectable */
chipListSelectable: boolean = true;

/** The chip avatar */
@ContentChild(MatChipAvatar) avatar: MatChipAvatar;

Expand Down Expand Up @@ -167,11 +170,13 @@ export class MatChip extends _MatChipMixinBase implements FocusableOption, OnDes
protected _value: any;

/**
* Whether or not the chips are selectable. When a chip is not selectable,
* changes to it's selected state are always ignored.
* Whether or not the chip is selectable. When a chip is not selectable,
* changes to it's selected state are always ignored. By default a chip is
* selectable, and it becomes non-selectable if it's parent chip list is
* not selectable.
*/
@Input()
get selectable(): boolean { return this._selectable; }
get selectable(): boolean { return this._selectable && this.chipListSelectable; }
Copy link
Member

Choose a reason for hiding this comment

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

Does this work with the OnPush change detection? I think the chip may need a markForCheck somewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The selectable on chip list is an @Input().

set selectable(value: boolean) {
this._selectable = coerceBooleanProperty(value);
}
Expand Down