Skip to content

Commit 52ea7a3

Browse files
crisbetotinayuangao
authored andcommitted
fix(select): wrong item order in label in rtl (#3567)
Fixes selected items not having the proper order in the label when `md-select` is in `multiple` mode and in RTL.
1 parent 8e6720b commit 52ea7a3

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/lib/select/select.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {wrappedErrorMessage} from '../core/testing/wrapped-error-message';
2626

2727
describe('MdSelect', () => {
2828
let overlayContainerElement: HTMLElement;
29-
let dir: {value: string};
29+
let dir: {value: 'ltr'|'rtl'};
3030

3131
beforeEach(async(() => {
3232
TestBed.configureTestingModule({
@@ -1571,6 +1571,23 @@ describe('MdSelect', () => {
15711571
expect(fixture.componentInstance.control.value).toEqual(['steak-0', 'pizza-1', 'tacos-2']);
15721572
});
15731573

1574+
it('should sort the selected options in reverse in rtl', () => {
1575+
dir.value = 'rtl';
1576+
trigger.click();
1577+
fixture.detectChanges();
1578+
1579+
const options = overlayContainerElement.querySelectorAll('md-option') as
1580+
NodeListOf<HTMLElement>;
1581+
1582+
options[2].click();
1583+
options[0].click();
1584+
options[1].click();
1585+
fixture.detectChanges();
1586+
1587+
expect(trigger.textContent).toContain('Tacos, Pizza, Steak');
1588+
expect(fixture.componentInstance.control.value).toEqual(['steak-0', 'pizza-1', 'tacos-2']);
1589+
});
1590+
15741591
it('should sort the values, that get set via the model, based on the panel order', () => {
15751592
trigger.click();
15761593
fixture.detectChanges();
@@ -1581,6 +1598,17 @@ describe('MdSelect', () => {
15811598
expect(trigger.textContent).toContain('Steak, Pizza, Tacos');
15821599
});
15831600

1601+
it('should reverse sort the values, that get set via the model in rtl', () => {
1602+
dir.value = 'rtl';
1603+
trigger.click();
1604+
fixture.detectChanges();
1605+
1606+
testInstance.control.setValue(['tacos-2', 'steak-0', 'pizza-1']);
1607+
fixture.detectChanges();
1608+
1609+
expect(trigger.textContent).toContain('Tacos, Pizza, Steak');
1610+
});
1611+
15841612
it('should throw an exception when trying to set a non-array value', () => {
15851613
expect(() => {
15861614
testInstance.control.setValue('not-an-array');

src/lib/select/select.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,18 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr
420420

421421
/** The value displayed in the trigger. */
422422
get triggerValue(): string {
423-
return this.multiple ?
424-
this._selectionModel.selected.map(option => option.viewValue).join(', ') :
425-
this._selectionModel.selected[0].viewValue;
423+
if (this._multiple) {
424+
let selectedOptions = this._selectionModel.selected.map(option => option.viewValue);
425+
426+
if (this._isRtl()) {
427+
selectedOptions.reverse();
428+
}
429+
430+
// TODO(crisbeto): delimiter should be configurable for proper localization.
431+
return selectedOptions.join(', ');
432+
}
433+
434+
return this._selectionModel.selected[0].viewValue;
426435
}
427436

428437
/** Whether the element is in RTL mode. */

0 commit comments

Comments
 (0)