Skip to content

Commit dfe01f2

Browse files
devversionmmalerba
authored andcommitted
fix(selection-list): do not coerece option value to boolean (#6983)
Currently the value of a `MdListOption` is coerced to a boolean. This is not the correct behavior because it would mean that there could be only two values (true/false). Needed for #6896
1 parent dc9679d commit dfe01f2

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/lib/list/selection-list.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ describe('MdSelectionList', () => {
4848
expect(listItemEl.nativeElement.className).not.toContain('mat-list-item-focus');
4949
});
5050

51+
it('should be able to set a value on a list option', () => {
52+
const optionValues = ['inbox', 'starred', 'sent-mail', 'drafts'];
53+
54+
optionValues.forEach((optionValue, index) => {
55+
expect(listOption[index].componentInstance.value).toBe(optionValue);
56+
});
57+
});
58+
5159
it('should be able to dispatch one selected item', () => {
5260
let testListItem = listOption[2].injector.get<MdListOption>(MdListOption);
5361
let selectList = selectionList.injector.get<MdSelectionList>(MdSelectionList).selectedOptions;
@@ -379,17 +387,18 @@ describe('MdSelectionList', () => {
379387

380388

381389
@Component({template: `
382-
<mat-selection-list id = "selection-list-1">
383-
<md-list-option checkboxPosition = "before" disabled = "true">
390+
<mat-selection-list id="selection-list-1">
391+
<md-list-option checkboxPosition="before" disabled="true" value="inbox">
384392
Inbox (disabled selection-option)
385393
</md-list-option>
386-
<md-list-option id = "testSelect" checkboxPosition = "before" class="test-native-focus">
394+
<md-list-option id="testSelect" checkboxPosition="before" class="test-native-focus"
395+
value="starred">
387396
Starred
388397
</md-list-option>
389-
<md-list-option checkboxPosition = "before">
398+
<md-list-option checkboxPosition="before" value="sent-mail">
390399
Sent Mail
391400
</md-list-option>
392-
<md-list-option checkboxPosition = "before">
401+
<md-list-option checkboxPosition="before" value="drafts">
393402
Drafts
394403
</md-list-option>
395404
</mat-selection-list>`})

src/lib/list/selection-list.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export class MdListOption extends _MdListOptionMixinBase
8282
private _lineSetter: MdLineSetter;
8383
private _selected: boolean = false;
8484
private _disabled: boolean = false;
85-
private _value: any;
8685

8786
/** Whether the option has focus. */
8887
_hasFocus: boolean = false;
@@ -92,20 +91,18 @@ export class MdListOption extends _MdListOptionMixinBase
9291
/** Whether the label should appear before or after the checkbox. Defaults to 'after' */
9392
@Input() checkboxPosition: 'before' | 'after' = 'after';
9493

94+
/** Value of the option */
95+
@Input() value: any;
96+
9597
/** Whether the option is disabled. */
9698
@Input()
9799
get disabled() { return (this.selectionList && this.selectionList.disabled) || this._disabled; }
98100
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); }
99101

100-
/** Value of the option */
101-
@Input()
102-
get value() { return this._value; }
103-
set value( val: any) { this._value = coerceBooleanProperty(val); }
104-
105102
/** Whether the option is selected. */
106103
@Input()
107104
get selected() { return this._selected; }
108-
set selected( val: boolean) { this._selected = coerceBooleanProperty(val); }
105+
set selected(value: boolean) { this._selected = coerceBooleanProperty(value); }
109106

110107
/** Emitted when the option is focused. */
111108
onFocus = new EventEmitter<MdSelectionListOptionEvent>();
@@ -123,7 +120,7 @@ export class MdListOption extends _MdListOptionMixinBase
123120
private _element: ElementRef,
124121
private _changeDetector: ChangeDetectorRef,
125122
@Optional() @Inject(forwardRef(() => MdSelectionList))
126-
public selectionList: MdSelectionList) {
123+
public selectionList: MdSelectionList) {
127124
super();
128125
}
129126

0 commit comments

Comments
 (0)