11import {
2- Component ,
2+ Directive ,
33 ElementRef ,
44 EventEmitter ,
55 Input ,
66 OnDestroy ,
7- OnInit ,
87 Output ,
98 Renderer2 ,
109} from '@angular/core' ;
@@ -24,34 +23,52 @@ export class MdChipBase {
2423export const _MdChipMixinBase = mixinColor ( MdChipBase , 'primary' ) ;
2524
2625
26+ /**
27+ * Dummy directive to add CSS class to basic chips.
28+ * @docs -private
29+ */
30+ @Directive ( {
31+ selector : `md-basic-chip, [md-basic-chip], mat-basic-chip, [mat-basic-chip]` ,
32+ host : { 'class' : 'mat-basic-chip' }
33+ } )
34+ export class MdBasicChip { }
35+
2736/**
2837 * Material design styled Chip component. Used inside the MdChipList component.
2938 */
30- @Component ( {
39+ @Directive ( {
3140 selector : `md-basic-chip, [md-basic-chip], md-chip, [md-chip],
3241 mat-basic-chip, [mat-basic-chip], mat-chip, [mat-chip]` ,
33- template : `<ng-content></ng-content>` ,
3442 inputs : [ 'color' ] ,
3543 host : {
36- '[ class.mat-chip] ' : 'true ' ,
44+ 'class' : 'mat-chip ' ,
3745 'tabindex' : '-1' ,
3846 'role' : 'option' ,
39-
4047 '[class.mat-chip-selected]' : 'selected' ,
41- '[attr.disabled]' : 'disabled' ,
42- '[attr.aria-disabled]' : '_isAriaDisabled' ,
43-
44- '(click)' : '_handleClick($event)'
48+ '[attr.disabled]' : 'disabled || null' ,
49+ '[attr.aria-disabled]' : '_isAriaDisabled()' ,
50+ '(click)' : '_handleClick($event)' ,
51+ '(focus)' : '_hasFocus = true' ,
52+ '(blur)' : '_hasFocus = false' ,
4553 }
4654} )
47- export class MdChip extends _MdChipMixinBase implements Focusable , OnInit , OnDestroy , CanColor {
48-
49- /** Whether or not the chip is disabled. Disabled chips cannot be focused. */
55+ export class MdChip extends _MdChipMixinBase implements Focusable , OnDestroy , CanColor {
56+ /** Whether or not the chip is disabled. */
57+ @Input ( ) get disabled ( ) : boolean { return this . _disabled ; }
58+ set disabled ( value : boolean ) { this . _disabled = coerceBooleanProperty ( value ) ; }
5059 protected _disabled : boolean = null ;
5160
52- /** Whether or not the chip is selected. */
61+ /** Whether the chip is selected. */
62+ @Input ( ) get selected ( ) : boolean { return this . _selected ; }
63+ set selected ( value : boolean ) {
64+ this . _selected = coerceBooleanProperty ( value ) ;
65+ ( this . selected ? this . select : this . deselect ) . emit ( { chip : this } ) ;
66+ }
5367 protected _selected : boolean = false ;
5468
69+ /** Whether the chip has focus. */
70+ _hasFocus : boolean = false ;
71+
5572 /** Emitted when the chip is focused. */
5673 onFocus = new EventEmitter < MdChipEvent > ( ) ;
5774
@@ -68,44 +85,10 @@ export class MdChip extends _MdChipMixinBase implements Focusable, OnInit, OnDes
6885 super ( renderer , elementRef ) ;
6986 }
7087
71- ngOnInit ( ) : void {
72- this . _addDefaultCSSClass ( ) ;
73- }
74-
7588 ngOnDestroy ( ) : void {
7689 this . destroy . emit ( { chip : this } ) ;
7790 }
7891
79- /** Whether or not the chip is disabled. */
80- @Input ( ) get disabled ( ) : boolean {
81- return this . _disabled ;
82- }
83-
84- /** Sets the disabled state of the chip. */
85- set disabled ( value : boolean ) {
86- this . _disabled = coerceBooleanProperty ( value ) ? true : null ;
87- }
88-
89- /** A String representation of the current disabled state. */
90- get _isAriaDisabled ( ) : string {
91- return String ( coerceBooleanProperty ( this . disabled ) ) ;
92- }
93-
94- /** Whether or not this chip is selected. */
95- @Input ( ) get selected ( ) : boolean {
96- return this . _selected ;
97- }
98-
99- set selected ( value : boolean ) {
100- this . _selected = coerceBooleanProperty ( value ) ;
101-
102- if ( this . _selected ) {
103- this . select . emit ( { chip : this } ) ;
104- } else {
105- this . deselect . emit ( { chip : this } ) ;
106- }
107- }
108-
10992 /**
11093 * Toggles the current selected state of this chip.
11194 * @return Whether the chip is selected.
@@ -121,6 +104,11 @@ export class MdChip extends _MdChipMixinBase implements Focusable, OnInit, OnDes
121104 this . onFocus . emit ( { chip : this } ) ;
122105 }
123106
107+ /** The aria-disabled state for the chip */
108+ _isAriaDisabled ( ) : string {
109+ return String ( this . disabled ) ;
110+ }
111+
124112 /** Ensures events fire properly upon click. */
125113 _handleClick ( event : Event ) {
126114 // Check disabled
@@ -131,18 +119,4 @@ export class MdChip extends _MdChipMixinBase implements Focusable, OnInit, OnDes
131119 this . focus ( ) ;
132120 }
133121 }
134-
135- /** Initializes the appropriate CSS classes based on the chip type (basic or standard). */
136- private _addDefaultCSSClass ( ) {
137- let el : HTMLElement = this . _elementRef . nativeElement ;
138-
139- // Always add the `mat-chip` class
140- this . _renderer . addClass ( el , 'mat-chip' ) ;
141-
142- // If we are a basic chip, also add the `mat-basic-chip` class for :not() targeting
143- if ( el . nodeName . toLowerCase ( ) == 'mat-basic-chip' || el . hasAttribute ( 'mat-basic-chip' ) ||
144- el . nodeName . toLowerCase ( ) == 'md-basic-chip' || el . hasAttribute ( 'md-basic-chip' ) ) {
145- this . _renderer . addClass ( el , 'mat-basic-chip' ) ;
146- }
147- }
148122}
0 commit comments