Skip to content

Commit 965edae

Browse files
committed
fixup! refactor(material-experimental/mdc-chips): remove usage of MDC adapter
1 parent 09c9d09 commit 965edae

File tree

13 files changed

+187
-170
lines changed

13 files changed

+187
-170
lines changed

src/material-experimental/mdc-chips/chip-action.ts

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, ElementRef, Input} from '@angular/core';
10-
import {
11-
CanDisable,
12-
HasTabIndex,
13-
mixinDisabled,
14-
mixinTabIndex,
15-
} from '@angular/material-experimental/mdc-core';
9+
import {BooleanInput, coerceBooleanProperty} from '@angular/cdk/coercion';
10+
import {ENTER, SPACE} from '@angular/cdk/keycodes';
11+
import {Directive, ElementRef, Inject, Input} from '@angular/core';
12+
import {HasTabIndex, mixinTabIndex} from '@angular/material-experimental/mdc-core';
13+
import {MAT_CHIP} from './tokens';
1614

17-
const _MatChipActionMixinBase = mixinTabIndex(mixinDisabled(class {}), -1);
15+
abstract class _MatChipActionBase {
16+
abstract disabled: boolean;
17+
}
18+
19+
const _MatChipActionMixinBase = mixinTabIndex(_MatChipActionBase, -1);
1820

1921
/**
2022
* Section within a chip.
@@ -33,16 +35,36 @@ const _MatChipActionMixinBase = mixinTabIndex(mixinDisabled(class {}), -1);
3335
'[attr.tabindex]': '(disabled || !isInteractive) ? null : tabIndex',
3436
'[attr.disabled]': "disabled ? '' : null",
3537
'[attr.aria-disabled]': 'disabled',
38+
'(click)': '_handleClick($event)',
39+
'(keydown)': '_handleKeydown($event)',
3640
},
3741
})
38-
export class MatChipAction extends _MatChipActionMixinBase implements CanDisable, HasTabIndex {
42+
export class MatChipAction extends _MatChipActionMixinBase implements HasTabIndex {
3943
/** Whether the action is interactive. */
4044
@Input() isInteractive = true;
4145

4246
/** Whether this is the primary action in the chip. */
4347
_isPrimary = true;
4448

45-
constructor(public _elementRef: ElementRef<HTMLElement>) {
49+
/** Whether the action is disabled. */
50+
@Input()
51+
get disabled(): boolean {
52+
return this._disabled || this._parentChip.disabled;
53+
}
54+
set disabled(value: BooleanInput) {
55+
this._disabled = coerceBooleanProperty(value);
56+
}
57+
private _disabled = false;
58+
59+
constructor(
60+
public _elementRef: ElementRef<HTMLElement>,
61+
@Inject(MAT_CHIP)
62+
protected _parentChip: {
63+
_handlePrimaryActionInteraction(): void;
64+
remove(): void;
65+
disabled: boolean;
66+
},
67+
) {
4668
super();
4769

4870
if (_elementRef.nativeElement.nodeName === 'BUTTON') {
@@ -53,4 +75,23 @@ export class MatChipAction extends _MatChipActionMixinBase implements CanDisable
5375
focus() {
5476
this._elementRef.nativeElement.focus();
5577
}
78+
79+
_handleClick(event: MouseEvent) {
80+
if (!this.disabled && this.isInteractive && this._isPrimary) {
81+
event.preventDefault();
82+
this._parentChip._handlePrimaryActionInteraction();
83+
}
84+
}
85+
86+
_handleKeydown(event: KeyboardEvent) {
87+
if (
88+
(event.keyCode === ENTER || event.keyCode === SPACE) &&
89+
!this.disabled &&
90+
this.isInteractive &&
91+
this._isPrimary
92+
) {
93+
event.preventDefault();
94+
this._parentChip._handlePrimaryActionInteraction();
95+
}
96+
}
5697
}

src/material-experimental/mdc-chips/chip-default-options.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/material-experimental/mdc-chips/chip-grid.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import {
4040
} from '@angular/material-experimental/mdc-core';
4141
import {MatFormFieldControl} from '@angular/material-experimental/mdc-form-field';
4242
import {MatChipTextControl} from './chip-text-control';
43-
import {Observable, Subject} from 'rxjs';
43+
import {Observable, Subject, merge} from 'rxjs';
4444
import {takeUntil} from 'rxjs/operators';
4545
import {MatChipEvent} from './chip';
4646
import {MatChipRow} from './chip-row';
@@ -296,7 +296,7 @@ export class MatChipGrid
296296
this.stateChanges.next();
297297
});
298298

299-
this.chipFocusChanges
299+
merge(this.chipFocusChanges, this._chips.changes)
300300
.pipe(takeUntil(this._destroyed))
301301
.subscribe(() => this.stateChanges.next());
302302
}
@@ -449,10 +449,16 @@ export class MatChipGrid
449449
!this._chips.last.disabled
450450
) {
451451
event.preventDefault();
452-
this._focusLastChip();
452+
453+
if (this._keyManager.activeItem) {
454+
this._keyManager.setActiveItem(this._keyManager.activeItem);
455+
} else {
456+
this._focusLastChip();
457+
}
453458
} else {
454-
// Use the super method here since it doesn't check for the input focused state.
455-
// This allows focus to escape if there's only one disabled chip left in the list.
459+
// Use the super method here since it doesn't check for the input
460+
// focused state. This allows focus to escape if there's only one
461+
// disabled chip left in the list.
456462
super._allowFocusEscape();
457463
}
458464
} else if (!this._chipInput.focused) {

src/material-experimental/mdc-chips/chip-icons.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Directive, InjectionToken} from '@angular/core';
9+
import {ENTER, SPACE} from '@angular/cdk/keycodes';
10+
import {Directive} from '@angular/core';
1011
import {MatChipAction} from './chip-action';
11-
12-
/**
13-
* Injection token that can be used to reference instances of `MatChipAvatar`. It serves as
14-
* alternative token to the actual `MatChipAvatar` class which could cause unnecessary
15-
* retention of the class and its directive metadata.
16-
*/
17-
export const MAT_CHIP_AVATAR = new InjectionToken<MatChipAvatar>('MatChipAvatar');
12+
import {MAT_CHIP_AVATAR, MAT_CHIP_REMOVE, MAT_CHIP_TRAILING_ICON} from './tokens';
1813

1914
/**
2015
* Directive to add CSS classes to chip leading icon.
@@ -30,15 +25,6 @@ export const MAT_CHIP_AVATAR = new InjectionToken<MatChipAvatar>('MatChipAvatar'
3025
})
3126
export class MatChipAvatar {}
3227

33-
/**
34-
* Injection token that can be used to reference instances of `MatChipTrailingIcon`. It serves as
35-
* alternative token to the actual `MatChipTrailingIcon` class which could cause unnecessary
36-
* retention of the class and its directive metadata.
37-
*/
38-
export const MAT_CHIP_TRAILING_ICON = new InjectionToken<MatChipTrailingIcon>(
39-
'MatChipTrailingIcon',
40-
);
41-
4228
/**
4329
* Directive to add CSS classes to and configure attributes for chip trailing icon.
4430
* @docs-private
@@ -62,13 +48,6 @@ export class MatChipTrailingIcon extends MatChipAction {
6248
override _isPrimary = false;
6349
}
6450

65-
/**
66-
* Injection token that can be used to reference instances of `MatChipRemove`. It serves as
67-
* alternative token to the actual `MatChipRemove` class which could cause unnecessary
68-
* retention of the class and its directive metadata.
69-
*/
70-
export const MAT_CHIP_REMOVE = new InjectionToken<MatChipRemove>('MatChipRemove');
71-
7251
/**
7352
* Directive to remove the parent chip when the trailing icon is clicked or
7453
* when the ENTER key is pressed on it.
@@ -98,4 +77,20 @@ export const MAT_CHIP_REMOVE = new InjectionToken<MatChipRemove>('MatChipRemove'
9877
})
9978
export class MatChipRemove extends MatChipAction {
10079
override _isPrimary = false;
80+
81+
override _handleClick(event: MouseEvent): void {
82+
if (!this.disabled) {
83+
event.stopPropagation();
84+
event.preventDefault();
85+
this._parentChip.remove();
86+
}
87+
}
88+
89+
override _handleKeydown(event: KeyboardEvent) {
90+
if ((event.keyCode === ENTER || event.keyCode === SPACE) && !this.disabled) {
91+
event.stopPropagation();
92+
event.preventDefault();
93+
this._parentChip.remove();
94+
}
95+
}
10196
}

src/material-experimental/mdc-chips/chip-input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
Output,
2222
} from '@angular/core';
2323
import {MatFormField, MAT_FORM_FIELD} from '@angular/material-experimental/mdc-form-field';
24-
import {MatChipsDefaultOptions, MAT_CHIPS_DEFAULT_OPTIONS} from './chip-default-options';
24+
import {MatChipsDefaultOptions, MAT_CHIPS_DEFAULT_OPTIONS} from './tokens';
2525
import {MatChipGrid} from './chip-grid';
2626
import {MatChipTextControl} from './chip-text-control';
2727

src/material-experimental/mdc-chips/chip-listbox.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ export class MatChipListbox
195195
this.chipBlurChanges.pipe(takeUntil(this._destroyed)).subscribe(() => this._blur());
196196
this.chipSelectionChanges.pipe(takeUntil(this._destroyed)).subscribe(event => {
197197
if (event.isUserInput) {
198+
if (!this.multiple && event.selected) {
199+
this._chips.forEach(chip => {
200+
if (chip !== event.source) {
201+
chip.deselect();
202+
}
203+
});
204+
}
205+
198206
this._propagateChanges();
199207
}
200208
});

src/material-experimental/mdc-chips/chip-option.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
OnInit,
1818
} from '@angular/core';
1919
import {MatChip} from './chip';
20+
import {MAT_CHIP} from './tokens';
2021

2122
/** Event object emitted by MatChipOption when selected or deselected. */
2223
export class MatChipSelectionChange {
@@ -38,7 +39,7 @@ export class MatChipSelectionChange {
3839
selector: 'mat-basic-chip-option, mat-chip-option',
3940
templateUrl: 'chip-option.html',
4041
styleUrls: ['chip.css'],
41-
inputs: ['color', 'disableRipple', 'tabIndex'],
42+
inputs: ['color', 'disabled', 'disableRipple', 'tabIndex'],
4243
host: {
4344
'class': 'mat-mdc-chip mat-mdc-chip-option mdc-evolution-chip mdc-evolution-chip--filter',
4445
'[class.mat-mdc-chip-selected]': 'selected',
@@ -64,7 +65,10 @@ export class MatChipSelectionChange {
6465
'[attr.role]': 'role',
6566
'[id]': 'id',
6667
},
67-
providers: [{provide: MatChip, useExisting: MatChipOption}],
68+
providers: [
69+
{provide: MatChip, useExisting: MatChipOption},
70+
{provide: MAT_CHIP, useExisting: MatChipOption},
71+
],
6872
encapsulation: ViewEncapsulation.None,
6973
changeDetection: ChangeDetectionStrategy.OnPush,
7074
})
@@ -97,10 +101,7 @@ export class MatChipOption extends MatChip implements OnInit {
97101
return this._selected;
98102
}
99103
set selected(value: BooleanInput) {
100-
if (this.selectable) {
101-
const coercedValue = coerceBooleanProperty(value);
102-
this._setSelectedState(coercedValue, false);
103-
}
104+
this._setSelectedState(coerceBooleanProperty(value), false);
104105
}
105106
private _selected = false;
106107

@@ -126,35 +127,26 @@ export class MatChipOption extends MatChip implements OnInit {
126127

127128
/** Selects the chip. */
128129
select(): void {
129-
if (this.selectable) {
130-
this._setSelectedState(true, false);
131-
}
130+
this._setSelectedState(true, false);
132131
}
133132

134133
/** Deselects the chip. */
135134
deselect(): void {
136-
if (this.selectable) {
137-
this._setSelectedState(false, false);
138-
}
135+
this._setSelectedState(false, false);
139136
}
140137

141138
/** Selects this chip and emits userInputSelection event */
142139
selectViaInteraction(): void {
143-
if (this.selectable) {
144-
this._setSelectedState(true, true);
145-
}
140+
this._setSelectedState(true, true);
146141
}
147142

148143
/** Toggles the current selected state of this chip. */
149144
toggleSelected(isUserInput: boolean = false): boolean {
150-
if (this.selectable) {
151-
this._setSelectedState(!this.selected, isUserInput);
152-
}
153-
145+
this._setSelectedState(!this.selected, isUserInput);
154146
return this.selected;
155147
}
156148

157-
protected override _handlePrimaryActionInteraction() {
149+
override _handlePrimaryActionInteraction() {
158150
if (this.selectable && !this.disabled) {
159151
this._setSelectedState(!this.selected, true);
160152
}

src/material-experimental/mdc-chips/chip-row.spec.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,16 +313,6 @@ describe('MDC-based Row Chips', () => {
313313
expect(document.activeElement).toBe(primaryAction);
314314
}));
315315

316-
it('should focus the chip content if the body has focus on completion', fakeAsync(() => {
317-
const chipValue = 'chip value';
318-
editInputInstance.setValue(chipValue);
319-
(document.activeElement as HTMLElement).blur();
320-
dispatchKeyboardEvent(getEditInput(), 'keydown', ENTER);
321-
fixture.detectChanges();
322-
flush();
323-
expect(document.activeElement).toBe(primaryAction);
324-
}));
325-
326316
it('should not change focus if another element has focus on completion', fakeAsync(() => {
327317
const chipValue = 'chip value';
328318
editInputInstance.setValue(chipValue);

0 commit comments

Comments
 (0)