66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import {
10- AfterViewInit ,
11- ChangeDetectorRef ,
12- Directive ,
13- ElementRef ,
14- Inject ,
15- Input ,
16- OnChanges ,
17- OnDestroy ,
18- SimpleChanges ,
19- } from '@angular/core' ;
20- import { DOCUMENT } from '@angular/common' ;
21- import {
22- MDCChipActionAdapter ,
23- MDCChipActionFoundation ,
24- MDCChipActionType ,
25- MDCChipPrimaryActionFoundation ,
26- } from '@material/chips' ;
27- import { emitCustomEvent } from './emit-event' ;
9+ import { Directive , ElementRef , Input } from '@angular/core' ;
2810import {
2911 CanDisable ,
3012 HasTabIndex ,
@@ -35,118 +17,40 @@ import {
3517const _MatChipActionMixinBase = mixinTabIndex ( mixinDisabled ( class { } ) , - 1 ) ;
3618
3719/**
38- * Interactive element within a chip.
20+ * Section within a chip.
3921 * @docs -private
4022 */
4123@Directive ( {
4224 selector : '[matChipAction]' ,
4325 inputs : [ 'disabled' , 'tabIndex' ] ,
4426 host : {
4527 'class' : 'mdc-evolution-chip__action mat-mdc-chip-action' ,
46- '[class.mdc-evolution-chip__action--primary]' : `_getFoundation().actionType() === ${ MDCChipActionType . PRIMARY } ` ,
28+ '[class.mdc-evolution-chip__action--primary]' : '_isPrimary' ,
4729 // Note that while our actions are interactive, we have to add the `--presentational` class,
4830 // in order to avoid some super-specific `:hover` styles from MDC.
49- '[class.mdc-evolution-chip__action--presentational]' : `_getFoundation().actionType() === ${ MDCChipActionType . PRIMARY } ` ,
50- '[class.mdc-evolution-chip__action--trailing]' : `_getFoundation().actionType() === ${ MDCChipActionType . TRAILING } ` ,
31+ '[class.mdc-evolution-chip__action--presentational]' : '_isPrimary' ,
32+ '[class.mdc-evolution-chip__action--trailing]' : '!_isPrimary' ,
5133 '[attr.tabindex]' : '(disabled || !isInteractive) ? null : tabIndex' ,
5234 '[attr.disabled]' : "disabled ? '' : null" ,
5335 '[attr.aria-disabled]' : 'disabled' ,
54- '(click)' : '_handleClick($event)' ,
55- '(keydown)' : '_handleKeydown($event)' ,
5636 } ,
5737} )
58- export class MatChipAction
59- extends _MatChipActionMixinBase
60- implements AfterViewInit , OnDestroy , CanDisable , HasTabIndex , OnChanges
61- {
62- private _document : Document ;
63- private _foundation : MDCChipActionFoundation ;
64- private _adapter : MDCChipActionAdapter = {
65- focus : ( ) => this . focus ( ) ,
66- getAttribute : ( name : string ) => this . _elementRef . nativeElement . getAttribute ( name ) ,
67- setAttribute : ( name : string , value : string ) => {
68- // MDC tries to update the tabindex directly in the DOM when navigating using the keyboard
69- // which overrides our own handling. If we detect such a case, assign it to the same property
70- // as the Angular binding in order to maintain consistency.
71- if ( name === 'tabindex' ) {
72- this . _updateTabindex ( parseInt ( value ) ) ;
73- } else {
74- this . _elementRef . nativeElement . setAttribute ( name , value ) ;
75- }
76- } ,
77- removeAttribute : ( name : string ) => {
78- if ( name !== 'tabindex' ) {
79- this . _elementRef . nativeElement . removeAttribute ( name ) ;
80- }
81- } ,
82- getElementID : ( ) => this . _elementRef . nativeElement . id ,
83- emitEvent : < T > ( eventName : string , data : T ) => {
84- emitCustomEvent < T > ( this . _elementRef . nativeElement , this . _document , eventName , data , true ) ;
85- } ,
86- } ;
87-
38+ export class MatChipAction extends _MatChipActionMixinBase implements CanDisable , HasTabIndex {
8839 /** Whether the action is interactive. */
8940 @Input ( ) isInteractive = true ;
9041
91- _handleClick ( event : MouseEvent ) {
92- // Usually these events can't happen while the chip is disabled since the browser won't
93- // allow them which is what MDC seems to rely on, however the event can be faked in tests.
94- if ( ! this . disabled && this . isInteractive ) {
95- this . _foundation . handleClick ( ) ;
96- event . preventDefault ( ) ;
97- }
98- }
42+ /** Whether this is the primary action in the chip. */
43+ _isPrimary = true ;
9944
100- _handleKeydown ( event : KeyboardEvent ) {
101- // Usually these events can't happen while the chip is disabled since the browser won't
102- // allow them which is what MDC seems to rely on, however the event can be faked in tests.
103- if ( ! this . disabled && this . isInteractive ) {
104- this . _foundation . handleKeydown ( event ) ;
105- }
106- }
107-
108- protected _createFoundation ( adapter : MDCChipActionAdapter ) : MDCChipActionFoundation {
109- return new MDCChipPrimaryActionFoundation ( adapter ) ;
110- }
111-
112- constructor (
113- public _elementRef : ElementRef ,
114- @Inject ( DOCUMENT ) _document : any ,
115- private _changeDetectorRef : ChangeDetectorRef ,
116- ) {
45+ constructor ( public _elementRef : ElementRef < HTMLElement > ) {
11746 super ( ) ;
118- this . _foundation = this . _createFoundation ( this . _adapter ) ;
11947
12048 if ( _elementRef . nativeElement . nodeName === 'BUTTON' ) {
12149 _elementRef . nativeElement . setAttribute ( 'type' , 'button' ) ;
12250 }
12351 }
12452
125- ngAfterViewInit ( ) {
126- this . _foundation . init ( ) ;
127- this . _foundation . setDisabled ( this . disabled ) ;
128- }
129-
130- ngOnChanges ( changes : SimpleChanges ) {
131- if ( changes [ 'disabled' ] ) {
132- this . _foundation . setDisabled ( this . disabled ) ;
133- }
134- }
135-
136- ngOnDestroy ( ) {
137- this . _foundation . destroy ( ) ;
138- }
139-
14053 focus ( ) {
14154 this . _elementRef . nativeElement . focus ( ) ;
14255 }
143-
144- _getFoundation ( ) {
145- return this . _foundation ;
146- }
147-
148- _updateTabindex ( value : number ) {
149- this . tabIndex = value ;
150- this . _changeDetectorRef . markForCheck ( ) ;
151- }
15256}
0 commit comments