@@ -13,16 +13,17 @@ import {CommonModule} from '@angular/common';
13
13
import { ENTER , SPACE } from '../keyboard/keycodes' ;
14
14
import { coerceBooleanProperty } from '../coercion/boolean-property' ;
15
15
import { MdRippleModule } from '../ripple/index' ;
16
+ import { MdSelectionModule } from '../selection/index' ;
16
17
17
18
/**
18
19
* Option IDs need to be unique across components, so this counter exists outside of
19
20
* the component definition.
20
21
*/
21
22
let _uniqueIdCounter = 0 ;
22
23
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 ) { }
26
27
}
27
28
28
29
@@ -36,6 +37,7 @@ export class MdOptionSelectEvent {
36
37
'role' : 'option' ,
37
38
'[attr.tabindex]' : '_getTabIndex()' ,
38
39
'[class.mat-selected]' : 'selected' ,
40
+ '[class.mat-option-multiple]' : 'multiple' ,
39
41
'[class.mat-active]' : 'active' ,
40
42
'[id]' : 'id' ,
41
43
'[attr.aria-selected]' : 'selected.toString()' ,
@@ -57,9 +59,15 @@ export class MdOption {
57
59
58
60
private _id : string = `md-option-${ _uniqueIdCounter ++ } ` ;
59
61
62
+ /** Whether the wrapping component is in multiple selection mode. */
63
+ multiple : boolean = false ;
64
+
60
65
/** The unique ID of the option. */
61
66
get id ( ) { return this . _id ; }
62
67
68
+ /** Whether or not the option is currently selected. */
69
+ get selected ( ) : boolean { return this . _selected ; }
70
+
63
71
/** The form value of the option. */
64
72
@Input ( ) value : any ;
65
73
@@ -68,16 +76,11 @@ export class MdOption {
68
76
get disabled ( ) { return this . _disabled ; }
69
77
set disabled ( value : any ) { this . _disabled = coerceBooleanProperty ( value ) ; }
70
78
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 > ( ) ;
73
81
74
82
constructor ( private _element : ElementRef , private _renderer : Renderer ) { }
75
83
76
- /** Whether or not the option is currently selected. */
77
- get selected ( ) : boolean {
78
- return this . _selected ;
79
- }
80
-
81
84
/**
82
85
* Whether or not the option is currently active and ready to be selected.
83
86
* An active option displays styles as if it is focused, but the
@@ -100,12 +103,13 @@ export class MdOption {
100
103
/** Selects the option. */
101
104
select ( ) : void {
102
105
this . _selected = true ;
103
- this . onSelect . emit ( new MdOptionSelectEvent ( this , false ) ) ;
106
+ this . _emitSelectionChangeEvent ( ) ;
104
107
}
105
108
106
109
/** Deselects the option. */
107
110
deselect ( ) : void {
108
111
this . _selected = false ;
112
+ this . _emitSelectionChangeEvent ( ) ;
109
113
}
110
114
111
115
/** Sets focus onto this option. */
@@ -118,7 +122,7 @@ export class MdOption {
118
122
* active. This is used by the ActiveDescendantKeyManager so key
119
123
* events will display the proper options as active on arrow key events.
120
124
*/
121
- setActiveStyles ( ) {
125
+ setActiveStyles ( ) : void {
122
126
Promise . resolve ( null ) . then ( ( ) => this . _active = true ) ;
123
127
}
124
128
@@ -127,7 +131,7 @@ export class MdOption {
127
131
* active. This is used by the ActiveDescendantKeyManager so key
128
132
* events will display the proper options as active on arrow key events.
129
133
*/
130
- setInactiveStyles ( ) {
134
+ setInactiveStyles ( ) : void {
131
135
Promise . resolve ( null ) . then ( ( ) => this . _active = false ) ;
132
136
}
133
137
@@ -142,26 +146,32 @@ export class MdOption {
142
146
* Selects the option while indicating the selection came from the user. Used to
143
147
* determine if the select's view -> model callback should be invoked.
144
148
*/
145
- _selectViaInteraction ( ) {
149
+ _selectViaInteraction ( ) : void {
146
150
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 ) ;
149
153
}
150
154
}
151
155
152
156
/** Returns the correct tabindex for the option depending on disabled state. */
153
- _getTabIndex ( ) {
157
+ _getTabIndex ( ) : string {
154
158
return this . disabled ? '-1' : '0' ;
155
159
}
156
160
161
+ /** Fetches the host DOM element. */
157
162
_getHostElement ( ) : HTMLElement {
158
163
return this . _element . nativeElement ;
159
164
}
160
165
166
+ /** Emits the selection change event. */
167
+ private _emitSelectionChangeEvent ( isUserInput = false ) : void {
168
+ this . onSelectionChange . emit ( new MdOptionSelectionChange ( this , isUserInput ) ) ;
169
+ } ;
170
+
161
171
}
162
172
163
173
@NgModule ( {
164
- imports : [ MdRippleModule , CommonModule ] ,
174
+ imports : [ MdRippleModule , CommonModule , MdSelectionModule ] ,
165
175
exports : [ MdOption ] ,
166
176
declarations : [ MdOption ]
167
177
} )
0 commit comments