@@ -13,16 +13,17 @@ import {CommonModule} from '@angular/common';
1313import { ENTER , SPACE } from '../keyboard/keycodes' ;
1414import { coerceBooleanProperty } from '../coercion/boolean-property' ;
1515import { MdRippleModule } from '../ripple/index' ;
16+ import { MdSelectionModule } from '../selection/index' ;
1617
1718/**
1819 * Option IDs need to be unique across components, so this counter exists outside of
1920 * the component definition.
2021 */
2122let _uniqueIdCounter = 0 ;
2223
23- /** Event object emitted by MdOption when selected. */
24- export class MdOptionSelectEvent {
25- constructor ( public source : MdOption , public isUserInput = false ) { }
24+ /** Event object emitted by MdOption when selected or deselected . */
25+ export class MdOptionSelectionChange {
26+ constructor ( public source : MdOption , public isUserInput = false ) { }
2627}
2728
2829
@@ -36,6 +37,7 @@ export class MdOptionSelectEvent {
3637 'role' : 'option' ,
3738 '[attr.tabindex]' : '_getTabIndex()' ,
3839 '[class.mat-selected]' : 'selected' ,
40+ '[class.mat-option-multiple]' : 'multiple' ,
3941 '[class.mat-active]' : 'active' ,
4042 '[id]' : 'id' ,
4143 '[attr.aria-selected]' : 'selected.toString()' ,
@@ -57,9 +59,15 @@ export class MdOption {
5759
5860 private _id : string = `md-option-${ _uniqueIdCounter ++ } ` ;
5961
62+ /** Whether the wrapping component is in multiple selection mode. */
63+ multiple : boolean = false ;
64+
6065 /** The unique ID of the option. */
6166 get id ( ) { return this . _id ; }
6267
68+ /** Whether or not the option is currently selected. */
69+ get selected ( ) : boolean { return this . _selected ; }
70+
6371 /** The form value of the option. */
6472 @Input ( ) value : any ;
6573
@@ -68,16 +76,11 @@ export class MdOption {
6876 get disabled ( ) { return this . _disabled ; }
6977 set disabled ( value : any ) { this . _disabled = coerceBooleanProperty ( value ) ; }
7078
71- /** Event emitted when the option is selected. */
72- @Output ( ) onSelect = new EventEmitter < MdOptionSelectEvent > ( ) ;
79+ /** Event emitted when the option is selected or deselected . */
80+ @Output ( ) onSelectionChange = new EventEmitter < MdOptionSelectionChange > ( ) ;
7381
7482 constructor ( private _element : ElementRef , private _renderer : Renderer ) { }
7583
76- /** Whether or not the option is currently selected. */
77- get selected ( ) : boolean {
78- return this . _selected ;
79- }
80-
8184 /**
8285 * Whether or not the option is currently active and ready to be selected.
8386 * An active option displays styles as if it is focused, but the
@@ -100,12 +103,13 @@ export class MdOption {
100103 /** Selects the option. */
101104 select ( ) : void {
102105 this . _selected = true ;
103- this . onSelect . emit ( new MdOptionSelectEvent ( this , false ) ) ;
106+ this . _emitSelectionChangeEvent ( ) ;
104107 }
105108
106109 /** Deselects the option. */
107110 deselect ( ) : void {
108111 this . _selected = false ;
112+ this . _emitSelectionChangeEvent ( ) ;
109113 }
110114
111115 /** Sets focus onto this option. */
@@ -118,7 +122,7 @@ export class MdOption {
118122 * active. This is used by the ActiveDescendantKeyManager so key
119123 * events will display the proper options as active on arrow key events.
120124 */
121- setActiveStyles ( ) {
125+ setActiveStyles ( ) : void {
122126 Promise . resolve ( null ) . then ( ( ) => this . _active = true ) ;
123127 }
124128
@@ -127,7 +131,7 @@ export class MdOption {
127131 * active. This is used by the ActiveDescendantKeyManager so key
128132 * events will display the proper options as active on arrow key events.
129133 */
130- setInactiveStyles ( ) {
134+ setInactiveStyles ( ) : void {
131135 Promise . resolve ( null ) . then ( ( ) => this . _active = false ) ;
132136 }
133137
@@ -142,26 +146,32 @@ export class MdOption {
142146 * Selects the option while indicating the selection came from the user. Used to
143147 * determine if the select's view -> model callback should be invoked.
144148 */
145- _selectViaInteraction ( ) {
149+ _selectViaInteraction ( ) : void {
146150 if ( ! this . disabled ) {
147- this . _selected = true ;
148- this . onSelect . emit ( new MdOptionSelectEvent ( this , true ) ) ;
151+ this . _selected = this . multiple ? ! this . _selected : true ;
152+ this . _emitSelectionChangeEvent ( true ) ;
149153 }
150154 }
151155
152156 /** Returns the correct tabindex for the option depending on disabled state. */
153- _getTabIndex ( ) {
157+ _getTabIndex ( ) : string {
154158 return this . disabled ? '-1' : '0' ;
155159 }
156160
161+ /** Fetches the host DOM element. */
157162 _getHostElement ( ) : HTMLElement {
158163 return this . _element . nativeElement ;
159164 }
160165
166+ /** Emits the selection change event. */
167+ private _emitSelectionChangeEvent ( isUserInput = false ) : void {
168+ this . onSelectionChange . emit ( new MdOptionSelectionChange ( this , isUserInput ) ) ;
169+ } ;
170+
161171}
162172
163173@NgModule ( {
164- imports : [ MdRippleModule , CommonModule ] ,
174+ imports : [ MdRippleModule , CommonModule , MdSelectionModule ] ,
165175 exports : [ MdOption ] ,
166176 declarations : [ MdOption ]
167177} )
0 commit comments