Skip to content

Commit

Permalink
fix(select): wrong item order in label in rtl
Browse files Browse the repository at this point in the history
Fixes selected items not having the proper order in the label when `md-select` is in `multiple` mode and in RTL.
  • Loading branch information
crisbeto committed Mar 12, 2017
1 parent cdb3763 commit 32cf848
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/lib/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {dispatchFakeEvent} from '../core/testing/dispatch-events';

describe('MdSelect', () => {
let overlayContainerElement: HTMLElement;
let dir: {value: string};
let dir: {value: 'ltr'|'rtl'};

beforeEach(async(() => {
TestBed.configureTestingModule({
Expand Down Expand Up @@ -1553,6 +1553,23 @@ describe('MdSelect', () => {
expect(fixture.componentInstance.control.value).toEqual(['steak-0', 'pizza-1', 'tacos-2']);
});

it('should sort the selected options in reverse in rtl', () => {
dir.value = 'rtl';
trigger.click();
fixture.detectChanges();

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

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

expect(trigger.textContent).toContain('Tacos, Pizza, Steak');
expect(fixture.componentInstance.control.value).toEqual(['steak-0', 'pizza-1', 'tacos-2']);
});

it('should sort the values, that get set via the model, based on the panel order', () => {
trigger.click();
fixture.detectChanges();
Expand All @@ -1563,6 +1580,17 @@ describe('MdSelect', () => {
expect(trigger.textContent).toContain('Steak, Pizza, Tacos');
});

it('should reverse sort the values, that get set via the model in rtl', () => {
dir.value = 'rtl';
trigger.click();
fixture.detectChanges();

testInstance.control.setValue(['tacos-2', 'steak-0', 'pizza-1']);
fixture.detectChanges();

expect(trigger.textContent).toContain('Tacos, Pizza, Steak');
});

it('should throw an exception when trying to set a non-array value', () => {
expect(() => {
testInstance.control.setValue('not-an-array');
Expand Down
14 changes: 11 additions & 3 deletions src/lib/select/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,9 +413,17 @@ export class MdSelect implements AfterContentInit, ControlValueAccessor, OnDestr

/** The value displayed in the trigger. */
get triggerValue(): string {
return this.multiple ?
this._selectionModel.selected.map(option => option.viewValue).join(', ') :
this._selectionModel.selected[0].viewValue;
if (this._multiple) {
let array = this._selectionModel.selected.map(option => option.viewValue);

if (this._isRtl()) {
array.reverse();
}

return array.join(', ');
}

return this._selectionModel.selected[0].viewValue;
}

/** Whether the element is in RTL mode. */
Expand Down

0 comments on commit 32cf848

Please sign in to comment.