@@ -7,22 +7,26 @@ import {
77 NgModule ,
88 ModuleWithProviders ,
99 Renderer ,
10- ViewEncapsulation
10+ ViewEncapsulation ,
11+ Inject ,
12+ Optional ,
1113} from '@angular/core' ;
1214import { CommonModule } from '@angular/common' ;
1315import { ENTER , SPACE } from '../keyboard/keycodes' ;
1416import { coerceBooleanProperty } from '../coercion/boolean-property' ;
1517import { MdRippleModule } from '../ripple/index' ;
18+ import { MdSelectionModule } from '../selection/index' ;
19+ import { MATERIAL_COMPATIBILITY_MODE } from '../../core/compatibility/compatibility' ;
1620
1721/**
1822 * Option IDs need to be unique across components, so this counter exists outside of
1923 * the component definition.
2024 */
2125let _uniqueIdCounter = 0 ;
2226
23- /** Event object emitted by MdOption when selected. */
24- export class MdOptionSelectEvent {
25- constructor ( public source : MdOption , public isUserInput = false ) { }
27+ /** Event object emitted by MdOption when selected or deselected . */
28+ export class MdOptionSelectionChange {
29+ constructor ( public source : MdOption , public isUserInput = false ) { }
2630}
2731
2832
@@ -36,6 +40,7 @@ export class MdOptionSelectEvent {
3640 'role' : 'option' ,
3741 '[attr.tabindex]' : '_getTabIndex()' ,
3842 '[class.mat-selected]' : 'selected' ,
43+ '[class.mat-option-multiple]' : 'multiple' ,
3944 '[class.mat-active]' : 'active' ,
4045 '[id]' : 'id' ,
4146 '[attr.aria-selected]' : 'selected.toString()' ,
@@ -57,9 +62,15 @@ export class MdOption {
5762
5863 private _id : string = `md-option-${ _uniqueIdCounter ++ } ` ;
5964
65+ /** Whether the wrapping component is in multiple selection mode. */
66+ multiple : boolean = false ;
67+
6068 /** The unique ID of the option. */
6169 get id ( ) { return this . _id ; }
6270
71+ /** Whether or not the option is currently selected. */
72+ get selected ( ) : boolean { return this . _selected ; }
73+
6374 /** The form value of the option. */
6475 @Input ( ) value : any ;
6576
@@ -68,15 +79,13 @@ export class MdOption {
6879 get disabled ( ) { return this . _disabled ; }
6980 set disabled ( value : any ) { this . _disabled = coerceBooleanProperty ( value ) ; }
7081
71- /** Event emitted when the option is selected. */
72- @Output ( ) onSelect = new EventEmitter < MdOptionSelectEvent > ( ) ;
82+ /** Event emitted when the option is selected or deselected . */
83+ @Output ( ) onSelectionChange = new EventEmitter < MdOptionSelectionChange > ( ) ;
7384
74- constructor ( private _element : ElementRef , private _renderer : Renderer ) { }
75-
76- /** Whether or not the option is currently selected. */
77- get selected ( ) : boolean {
78- return this . _selected ;
79- }
85+ constructor (
86+ private _element : ElementRef ,
87+ private _renderer : Renderer ,
88+ @Optional ( ) @Inject ( MATERIAL_COMPATIBILITY_MODE ) public _isCompatibilityMode : boolean ) { }
8089
8190 /**
8291 * Whether or not the option is currently active and ready to be selected.
@@ -100,12 +109,13 @@ export class MdOption {
100109 /** Selects the option. */
101110 select ( ) : void {
102111 this . _selected = true ;
103- this . onSelect . emit ( new MdOptionSelectEvent ( this , false ) ) ;
112+ this . _emitSelectionChangeEvent ( ) ;
104113 }
105114
106115 /** Deselects the option. */
107116 deselect ( ) : void {
108117 this . _selected = false ;
118+ this . _emitSelectionChangeEvent ( ) ;
109119 }
110120
111121 /** Sets focus onto this option. */
@@ -118,7 +128,7 @@ export class MdOption {
118128 * active. This is used by the ActiveDescendantKeyManager so key
119129 * events will display the proper options as active on arrow key events.
120130 */
121- setActiveStyles ( ) {
131+ setActiveStyles ( ) : void {
122132 Promise . resolve ( null ) . then ( ( ) => this . _active = true ) ;
123133 }
124134
@@ -127,7 +137,7 @@ export class MdOption {
127137 * active. This is used by the ActiveDescendantKeyManager so key
128138 * events will display the proper options as active on arrow key events.
129139 */
130- setInactiveStyles ( ) {
140+ setInactiveStyles ( ) : void {
131141 Promise . resolve ( null ) . then ( ( ) => this . _active = false ) ;
132142 }
133143
@@ -142,26 +152,32 @@ export class MdOption {
142152 * Selects the option while indicating the selection came from the user. Used to
143153 * determine if the select's view -> model callback should be invoked.
144154 */
145- _selectViaInteraction ( ) {
155+ _selectViaInteraction ( ) : void {
146156 if ( ! this . disabled ) {
147- this . _selected = true ;
148- this . onSelect . emit ( new MdOptionSelectEvent ( this , true ) ) ;
157+ this . _selected = this . multiple ? ! this . _selected : true ;
158+ this . _emitSelectionChangeEvent ( true ) ;
149159 }
150160 }
151161
152162 /** Returns the correct tabindex for the option depending on disabled state. */
153- _getTabIndex ( ) {
163+ _getTabIndex ( ) : string {
154164 return this . disabled ? '-1' : '0' ;
155165 }
156166
167+ /** Fetches the host DOM element. */
157168 _getHostElement ( ) : HTMLElement {
158169 return this . _element . nativeElement ;
159170 }
160171
172+ /** Emits the selection change event. */
173+ private _emitSelectionChangeEvent ( isUserInput = false ) : void {
174+ this . onSelectionChange . emit ( new MdOptionSelectionChange ( this , isUserInput ) ) ;
175+ } ;
176+
161177}
162178
163179@NgModule ( {
164- imports : [ MdRippleModule , CommonModule ] ,
180+ imports : [ MdRippleModule , CommonModule , MdSelectionModule ] ,
165181 exports : [ MdOption ] ,
166182 declarations : [ MdOption ]
167183} )
0 commit comments