Skip to content

Commit e16e2f6

Browse files
asynclizcopybara-github
authored andcommitted
fix(select): stale tabindex caused incorrect typeahead value changes
Fixes #5913 PiperOrigin-RevId: 883616950
1 parent e5ed2fe commit e16e2f6

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

select/internal/select.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,11 @@ export abstract class Select extends selectBaseClass {
732732
selectedOptions.forEach(([option]) => {
733733
if (item !== option) {
734734
option.selected = false;
735+
option.tabIndex = -1;
735736
}
736737
});
737738
item.selected = true;
739+
item.tabIndex = 0;
738740

739741
return this.updateValueAndDisplayText();
740742
}

select/select_test.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,101 @@ describe('<md-outlined-select>', () => {
5252
expect(changed).toBeTrue();
5353
});
5454

55+
it('typeahead selects an option', async () => {
56+
let changed = false;
57+
render(
58+
html` <md-outlined-select
59+
@change=${() => {
60+
changed = true;
61+
}}>
62+
<md-select-option
63+
value="apple"
64+
typeahead-text="apple"></md-select-option>
65+
<md-select-option
66+
value="banana"
67+
typeahead-text="banana"></md-select-option>
68+
</md-outlined-select>`,
69+
root,
70+
);
71+
const selectEl = root.querySelector('md-outlined-select')!;
72+
await selectEl.updateComplete;
73+
74+
const harness = new SelectHarness(selectEl);
75+
await harness.focusWithKeyboard();
76+
await harness.keypress('b');
77+
await selectEl.updateComplete;
78+
79+
expect(selectEl.value).withContext('value after typeahead').toBe('banana');
80+
expect(changed).withContext('option changed').toBeTrue();
81+
});
82+
83+
it('typeahead with no matching option does not change selected option', async () => {
84+
let changed = false;
85+
render(
86+
html` <md-outlined-select
87+
@change=${() => {
88+
changed = true;
89+
}}>
90+
<md-select-option
91+
value="apple"
92+
typeahead-text="apple"
93+
selected></md-select-option>
94+
<md-select-option
95+
value="banana"
96+
typeahead-text="banana"></md-select-option>
97+
</md-outlined-select>`,
98+
root,
99+
);
100+
const selectEl = root.querySelector('md-outlined-select')!;
101+
await selectEl.updateComplete;
102+
103+
const harness = new SelectHarness(selectEl);
104+
await harness.focusWithKeyboard();
105+
await harness.keypress('z');
106+
await selectEl.updateComplete;
107+
108+
expect(selectEl.value)
109+
.withContext('value after non-matching typeahead keypress')
110+
.toBe('apple');
111+
expect(changed).withContext('did not change').toBeFalse();
112+
});
113+
114+
it('clicking an option with mouse and then typeahead with no matching option does not change the selected option', async () => {
115+
render(
116+
html` <md-outlined-select>
117+
<md-select-option
118+
value="apple"
119+
typeahead-text="apple"
120+
selected></md-select-option>
121+
<md-select-option
122+
value="banana"
123+
typeahead-text="banana"></md-select-option>
124+
</md-outlined-select>`,
125+
root,
126+
);
127+
const selectEl = root.querySelector('md-outlined-select')!;
128+
await selectEl.updateComplete;
129+
130+
// Open menu to click an option
131+
const harness = new SelectHarness(selectEl);
132+
await harness.clickWithMouse();
133+
await selectEl.updateComplete; // wait for menu to open
134+
135+
await harness.clickOption(1);
136+
expect(selectEl.value)
137+
.withContext('value after clicking option')
138+
.toBe('banana');
139+
140+
await harness.keypress('z');
141+
await selectEl.updateComplete;
142+
143+
expect(selectEl.value)
144+
.withContext(
145+
'value after non-matching typeahead keypress stays the same (banana)',
146+
)
147+
.toBe('banana');
148+
});
149+
55150
describe('forms', () => {
56151
createFormTests({
57152
queryControl: (root) => root.querySelector('md-outlined-select'),

0 commit comments

Comments
 (0)