Skip to content

feat(select): custom text for multiple selected values #6227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2435,6 +2435,42 @@ describe('MdSelect', () => {
expect(trigger.textContent).toContain('Tacos, Pizza, Steak');
});

it('should display a custom text if multipleDisplayWith is set', () => {
testInstance.select.multipleDisplayWith = (values: string[]): string => {
return `${values.length} selected`;
};
trigger.click();
fixture.detectChanges();

const options = overlayContainerElement.querySelectorAll('md-option') as
NodeListOf<HTMLElement>;

options[0].click();
options[2].click();
options[5].click();
fixture.detectChanges();

expect(trigger.textContent).toContain('3 selected');
});

it('should only affect the text if multipleDisplayWith is set', () => {
testInstance.select.multipleDisplayWith = (values: string[]): string => {
return `${values.length} selected`;
};
trigger.click();
fixture.detectChanges();

const options = overlayContainerElement.querySelectorAll('md-option') as
NodeListOf<HTMLElement>;

options[0].click();
options[2].click();
options[5].click();
fixture.detectChanges();

expect(testInstance.control.value).toEqual(['steak-0', 'tacos-2', 'eggs-5']);
});

it('should throw an exception when trying to set a non-array value', async(() => {
expect(() => {
testInstance.control.setValue('not-an-array');
Expand Down
7 changes: 7 additions & 0 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
this._multiple = coerceBooleanProperty(value);
}

/** Function that maps the selected values to (a single) display value in the trigger. */
@Input() multipleDisplayWith: ((viewValues: any[]) => string) | null = null;

/** Whether to float the placeholder text. */
@Input()
get floatPlaceholder(): FloatPlaceholderType { return this._floatPlaceholder; }
Expand Down Expand Up @@ -531,6 +534,10 @@ export class MdSelect extends _MdSelectMixinBase implements AfterContentInit, On
if (this._multiple) {
let selectedOptions = this._selectionModel.selected.map(option => option.viewValue);

if (this.multipleDisplayWith) {
return this.multipleDisplayWith(selectedOptions);
}

if (this._isRtl()) {
selectedOptions.reverse();
}
Expand Down