Skip to content

Commit 277fb47

Browse files
committed
fixup! refactor(material-experimental/mdc-chips): remove usage of MDC adapter
1 parent d8465f5 commit 277fb47

File tree

11 files changed

+122
-92
lines changed

11 files changed

+122
-92
lines changed

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

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

9-
import {Directive, ElementRef, Input} from '@angular/core';
9+
import {ENTER, SPACE} from '@angular/cdk/keycodes';
10+
import {Directive, ElementRef, Inject, Input} from '@angular/core';
1011
import {
1112
CanDisable,
1213
HasTabIndex,
1314
mixinDisabled,
1415
mixinTabIndex,
1516
} from '@angular/material-experimental/mdc-core';
17+
import {MAT_CHIP} from './tokens';
1618

1719
const _MatChipActionMixinBase = mixinTabIndex(mixinDisabled(class {}), -1);
1820

@@ -33,6 +35,8 @@ 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
})
3842
export class MatChipAction extends _MatChipActionMixinBase implements CanDisable, HasTabIndex {
@@ -42,7 +46,11 @@ export class MatChipAction extends _MatChipActionMixinBase implements CanDisable
4246
/** Whether this is the primary action in the chip. */
4347
_isPrimary = true;
4448

45-
constructor(public _elementRef: ElementRef<HTMLElement>) {
49+
constructor(
50+
public _elementRef: ElementRef<HTMLElement>,
51+
@Inject(MAT_CHIP)
52+
protected _parentChip: {_handlePrimaryActionInteraction(): void; remove(): void},
53+
) {
4654
super();
4755

4856
if (_elementRef.nativeElement.nodeName === 'BUTTON') {
@@ -53,4 +61,23 @@ export class MatChipAction extends _MatChipActionMixinBase implements CanDisable
5361
focus() {
5462
this._elementRef.nativeElement.focus();
5563
}
64+
65+
_handleClick(event: MouseEvent) {
66+
if (!this.disabled && this.isInteractive && this._isPrimary) {
67+
event.preventDefault();
68+
this._parentChip._handlePrimaryActionInteraction();
69+
}
70+
}
71+
72+
_handleKeydown(event: KeyboardEvent) {
73+
if (
74+
(event.keyCode === ENTER || event.keyCode === SPACE) &&
75+
!this.disabled &&
76+
this.isInteractive &&
77+
this._isPrimary
78+
) {
79+
event.preventDefault();
80+
this._parentChip._handlePrimaryActionInteraction();
81+
}
82+
}
5683
}

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-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) {
199+
this._chips.forEach(chip => {
200+
if (chip !== event.source && chip.selected) {
201+
chip.deselect();
202+
}
203+
});
204+
}
205+
198206
this._propagateChanges();
199207
}
200208
});

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

Lines changed: 6 additions & 2 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 {
@@ -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
})
@@ -154,7 +158,7 @@ export class MatChipOption extends MatChip implements OnInit {
154158
return this.selected;
155159
}
156160

157-
protected override _handlePrimaryActionInteraction() {
161+
override _handlePrimaryActionInteraction() {
158162
if (this.selectable && !this.disabled) {
159163
this._setSelectedState(!this.selected, true);
160164
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {FocusMonitor} from '@angular/cdk/a11y';
3434
import {MatChip, MatChipEvent} from './chip';
3535
import {MatChipEditInput} from './chip-edit-input';
3636
import {takeUntil} from 'rxjs/operators';
37+
import {MAT_CHIP} from './tokens';
3738

3839
/** Represents an event fired on an individual `mat-chip` when it is edited. */
3940
export interface MatChipEditedEvent extends MatChipEvent {
@@ -70,7 +71,10 @@ export interface MatChipEditedEvent extends MatChipEvent {
7071
'(mousedown)': '_mousedown($event)',
7172
'(dblclick)': '_doubleclick($event)',
7273
},
73-
providers: [{provide: MatChip, useExisting: MatChipRow}],
74+
providers: [
75+
{provide: MatChip, useExisting: MatChipRow},
76+
{provide: MAT_CHIP, useExisting: MatChipRow},
77+
],
7478
encapsulation: ViewEncapsulation.None,
7579
changeDetection: ChangeDetectionStrategy.OnPush,
7680
})

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

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,10 @@ import {
4242
import {FocusMonitor} from '@angular/cdk/a11y';
4343
import {Subject} from 'rxjs';
4444
import {take} from 'rxjs/operators';
45-
import {
46-
MatChipAvatar,
47-
MatChipTrailingIcon,
48-
MatChipRemove,
49-
MAT_CHIP_AVATAR,
50-
MAT_CHIP_TRAILING_ICON,
51-
MAT_CHIP_REMOVE,
52-
} from './chip-icons';
45+
import {MatChipAvatar, MatChipTrailingIcon, MatChipRemove} from './chip-icons';
5346
import {MatChipAction} from './chip-action';
54-
import {BACKSPACE, DELETE, ENTER, SPACE} from '@angular/cdk/keycodes';
47+
import {BACKSPACE, DELETE} from '@angular/cdk/keycodes';
48+
import {MAT_CHIP, MAT_CHIP_AVATAR, MAT_CHIP_REMOVE, MAT_CHIP_TRAILING_ICON} from './tokens';
5549

5650
let uid = 0;
5751

@@ -102,11 +96,11 @@ const _MatChipMixinBase = mixinTabIndex(mixinColor(mixinDisableRipple(MatChipBas
10296
'[attr.role]': 'role',
10397
'[attr.tabindex]': 'role ? tabIndex : null',
10498
'[attr.aria-label]': 'ariaLabel',
105-
'(click)': '_handleClick($event)',
10699
'(keydown)': '_handleKeydown($event)',
107100
},
108101
encapsulation: ViewEncapsulation.None,
109102
changeDetection: ChangeDetectionStrategy.OnPush,
103+
providers: [{provide: MAT_CHIP, useExisting: MatChip}],
110104
})
111105
export class MatChip
112106
extends _MatChipMixinBase
@@ -289,23 +283,9 @@ export class MatChip
289283
return !!(this.trailingIcon || this.removeIcon);
290284
}
291285

292-
/** Handles click events on the chip. */
293-
_handleClick(event: MouseEvent) {
294-
const action = this._getSourceAction(event.target as Node);
295-
296-
if (action) {
297-
this._handleClickLikeInteraction(event, action);
298-
}
299-
}
300-
301286
/** Handles keyboard events on the chip. */
302287
_handleKeydown(event: KeyboardEvent) {
303-
const action = this._getSourceAction(event.target as Node);
304-
const keyCode = event.keyCode;
305-
306-
if (action && (keyCode === ENTER || keyCode === SPACE)) {
307-
this._handleClickLikeInteraction(event, action);
308-
} else if (keyCode === BACKSPACE || keyCode === DELETE) {
288+
if (event.keyCode === BACKSPACE || event.keyCode === DELETE) {
309289
event.preventDefault();
310290
this.remove();
311291
}
@@ -353,24 +333,10 @@ export class MatChip
353333
}
354334

355335
/** Handles interactions with the primary action of the chip. */
356-
protected _handlePrimaryActionInteraction() {
336+
_handlePrimaryActionInteraction() {
357337
// Empty here, but is overwritten in child classes.
358338
}
359339

360-
/** Handles clicks or enter/space presses on a specific action. */
361-
private _handleClickLikeInteraction(event: Event, action: MatChipAction) {
362-
if (action.isInteractive && !action.disabled) {
363-
if (action === this.removeIcon) {
364-
event.preventDefault();
365-
event.stopPropagation();
366-
this.remove();
367-
} else if (action === this.primaryAction) {
368-
event.preventDefault();
369-
this._handlePrimaryActionInteraction();
370-
}
371-
}
372-
}
373-
374340
/** Starts the focus monitoring process on the chip. */
375341
private _monitorFocus() {
376342
this._focusMonitor.monitor(this._elementRef, true).subscribe(origin => {

src/material-experimental/mdc-chips/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
MatRippleModule,
1616
} from '@angular/material-experimental/mdc-core';
1717
import {MatChip} from './chip';
18-
import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './chip-default-options';
18+
import {MAT_CHIPS_DEFAULT_OPTIONS, MatChipsDefaultOptions} from './tokens';
1919
import {MatChipEditInput} from './chip-edit-input';
2020
import {MatChipGrid} from './chip-grid';
2121
import {MatChipAvatar, MatChipRemove, MatChipTrailingIcon} from './chip-icons';

src/material-experimental/mdc-chips/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export * from './chip-listbox';
1414
export * from './chip-grid';
1515
export * from './module';
1616
export * from './chip-input';
17-
export * from './chip-default-options';
17+
export * from './tokens';
1818
export * from './chip-icons';
1919
export * from './chip-text-control';
2020
export * from './chip-edit-input';

0 commit comments

Comments
 (0)