Skip to content

Commit a81b54d

Browse files
committed
feat(select): custom text for multiple selected values
add input property to set a callback which will display a custom text if multiple values are selected. Similar to MdAutocomplete.displayWith. fixes #6218
1 parent 8a23157 commit a81b54d

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/lib/select/select.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2435,6 +2435,42 @@ describe('MdSelect', () => {
24352435
expect(trigger.textContent).toContain('Tacos, Pizza, Steak');
24362436
});
24372437

2438+
it('should display a custom text if multipleDisplayWith is set', () => {
2439+
testInstance.select.multipleDisplayWith = (values: string[]): string => {
2440+
return `${values.length} selected`;
2441+
};
2442+
trigger.click();
2443+
fixture.detectChanges();
2444+
2445+
const options = overlayContainerElement.querySelectorAll('md-option') as
2446+
NodeListOf<HTMLElement>;
2447+
2448+
options[0].click();
2449+
options[2].click();
2450+
options[5].click();
2451+
fixture.detectChanges();
2452+
2453+
expect(trigger.textContent).toContain('3 selected');
2454+
});
2455+
2456+
it('should only affect the text if multipleDisplayWith is set', () => {
2457+
testInstance.select.multipleDisplayWith = (values: string[]): string => {
2458+
return `${values.length} selected`;
2459+
};
2460+
trigger.click();
2461+
fixture.detectChanges();
2462+
2463+
const options = overlayContainerElement.querySelectorAll('md-option') as
2464+
NodeListOf<HTMLElement>;
2465+
2466+
options[0].click();
2467+
options[2].click();
2468+
options[5].click();
2469+
fixture.detectChanges();
2470+
2471+
expect(testInstance.control.value).toEqual(['steak-0', 'tacos-2', 'eggs-5']);
2472+
});
2473+
24382474
it('should throw an exception when trying to set a non-array value', async(() => {
24392475
expect(() => {
24402476
testInstance.control.setValue('not-an-array');

src/lib/select/select.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
318318
this._multiple = coerceBooleanProperty(value);
319319
}
320320

321+
/** Function that maps the selected values to (a single) display value in the trigger. */
322+
@Input() multipleDisplayWith: ((viewValues: any[]) => string) | null = null;
323+
321324
/** Whether to float the placeholder text. */
322325
@Input()
323326
get floatPlaceholder(): FloatPlaceholderType { return this._floatPlaceholder; }
@@ -531,6 +534,10 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
531534
if (this._multiple) {
532535
let selectedOptions = this._selectionModel.selected.map(option => option.viewValue);
533536

537+
if (this.multipleDisplayWith) {
538+
return this.multipleDisplayWith(selectedOptions);
539+
}
540+
534541
if (this._isRtl()) {
535542
selectedOptions.reverse();
536543
}

0 commit comments

Comments
 (0)