Skip to content

Commit b0bb29e

Browse files
committed
feat(select): allow setting viewValue to override text content
1 parent fbed180 commit b0bb29e

File tree

2 files changed

+62
-18
lines changed

2 files changed

+62
-18
lines changed

src/lib/core/option/option.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export class MdOption {
5656

5757
private _id: string = `md-option-${_uniqueIdCounter++}`;
5858

59+
private _viewValue: string;
60+
5961
/** The unique ID of the option. */
6062
get id() { return this._id; }
6163

@@ -89,11 +91,16 @@ export class MdOption {
8991

9092
/**
9193
* The displayed value of the option. It is necessary to show the selected option in the
92-
* select's trigger.
94+
* select's trigger. If not set explicitly, it defaults to the text content of the option
95+
* element.
9396
*/
97+
@Input()
9498
get viewValue(): string {
95-
// TODO(kara): Add input property alternative for node envs.
96-
return this._getHostElement().textContent.trim();
99+
return this._viewValue || this._getHostElement().textContent.trim();
100+
}
101+
102+
set viewValue(value: string) {
103+
this._viewValue = value;
97104
}
98105

99106
/** Selects the option. */

src/lib/select/select.spec.ts

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ describe('MdSelect', () => {
2626
SelectInitWithoutOptions,
2727
SelectWithChangeEvent,
2828
CustomSelectAccessor,
29-
CompWithCustomSelect
29+
CompWithCustomSelect,
30+
ViewValueSelect
3031
],
3132
providers: [
3233
{provide: OverlayContainer, useFactory: () => {
@@ -543,20 +544,6 @@ describe('MdSelect', () => {
543544

544545
});
545546

546-
describe('misc forms', () => {
547-
it('should support use inside a custom value accessor', () => {
548-
const fixture = TestBed.createComponent(CompWithCustomSelect);
549-
spyOn(fixture.componentInstance.customAccessor, 'writeValue');
550-
fixture.detectChanges();
551-
552-
expect(fixture.componentInstance.customAccessor.select._control)
553-
.toBe(null, 'Expected md-select NOT to inherit control from parent value accessor.');
554-
expect(fixture.componentInstance.customAccessor.writeValue).toHaveBeenCalled();
555-
});
556-
557-
});
558-
559-
560547
describe('animations', () => {
561548
let fixture: ComponentFixture<BasicSelect>;
562549
let trigger: HTMLElement;
@@ -1254,7 +1241,38 @@ describe('MdSelect', () => {
12541241

12551242
expect(fixture.componentInstance.changeListener).toHaveBeenCalledTimes(1);
12561243
});
1244+
1245+
});
1246+
1247+
describe('misc', () => {
1248+
it('should support use inside a custom value accessor', () => {
1249+
const fixture = TestBed.createComponent(CompWithCustomSelect);
1250+
spyOn(fixture.componentInstance.customAccessor, 'writeValue');
1251+
fixture.detectChanges();
1252+
1253+
expect(fixture.componentInstance.customAccessor.select._control)
1254+
.toBe(null, 'Expected md-select NOT to inherit control from parent value accessor.');
1255+
expect(fixture.componentInstance.customAccessor.writeValue).toHaveBeenCalled();
1256+
});
1257+
1258+
it('should display the viewValue in the trigger instead of textContent if it is set', () => {
1259+
const fixture = TestBed.createComponent(ViewValueSelect);
1260+
fixture.detectChanges();
1261+
1262+
const trigger = fixture.debugElement.query(By.css('.md-select-trigger')).nativeElement;
1263+
trigger.click();
1264+
fixture.detectChanges();
1265+
1266+
const option = overlayContainerElement.querySelector('md-option') as HTMLElement;
1267+
option.click();
1268+
fixture.detectChanges();
1269+
1270+
expect(trigger.textContent).toContain('Steak', 'Expected trigger to contain viewValue.');
1271+
expect(trigger.textContent).not.toContain('0', 'Expected trigger to contain viewValue.');
1272+
});
1273+
12571274
});
1275+
12581276
});
12591277

12601278
@Component({
@@ -1289,6 +1307,25 @@ class BasicSelect {
12891307
@ViewChildren(MdOption) options: QueryList<MdOption>;
12901308
}
12911309

1310+
@Component({
1311+
selector: 'view-value-select',
1312+
template: `
1313+
<md-select placeholder="Food">
1314+
<md-option *ngFor="let food of foods" [value]="food.value" [viewValue]="food.name">
1315+
{{ food.id }}: {{food.name}}
1316+
</md-option>
1317+
</md-select>
1318+
`
1319+
})
1320+
class ViewValueSelect {
1321+
foods: any[] = [
1322+
{ value: 'steak-0', name: 'Steak' , id: 0},
1323+
{ value: 'pizza-1', name: 'Pizza' , id: 1},
1324+
{ value: 'tacos-2', name: 'Tacos', id: 2},
1325+
];
1326+
}
1327+
1328+
12921329
@Component({
12931330
selector: 'ng-model-select',
12941331
template: `

0 commit comments

Comments
 (0)