Skip to content

Commit b461f21

Browse files
committed
fix: select face isn't updated by selectedIndex
1 parent 81dff42 commit b461f21

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

src/test/vscode-single-select.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ describe('vscode-single-select', () => {
5656
</div>
5757
`);
5858
expect(el.value).to.eq('Ipsum');
59+
expect(el.selectedIndex).to.eq(1);
5960
});
6061

6162
it('select face should be empty when the value is invalid', async () => {
@@ -81,6 +82,59 @@ describe('vscode-single-select', () => {
8182
</div>
8283
`);
8384
expect(el.value).to.eq('trololo');
85+
expect(el.selectedIndex).to.eq(-1);
86+
});
87+
88+
it('should display selected value when selectedIndex prop is changed', async () => {
89+
const el = (await fixture(html`
90+
<vscode-single-select>
91+
<vscode-option>Lorem</vscode-option>
92+
<vscode-option>Ipsum</vscode-option>
93+
<vscode-option>Dolor</vscode-option>
94+
</vscode-single-select>
95+
`)) as VscodeSingleSelect;
96+
97+
el.selectedIndex = 1;
98+
await el.updateComplete;
99+
100+
expect(el).shadowDom.to.equal(`
101+
<slot class="main-slot"></slot>
102+
<div class="select-face">
103+
<span class="text">
104+
Ipsum
105+
</span>
106+
<span class="icon">
107+
</span>
108+
</div>
109+
`);
110+
expect(el.value).to.eq('Ipsum');
111+
expect(el.selectedIndex).to.eq(1);
112+
});
113+
114+
it('select face should be empty when the selectedIndex prop is invalid', async () => {
115+
const el = (await fixture(html`
116+
<vscode-single-select>
117+
<vscode-option>Lorem</vscode-option>
118+
<vscode-option>Ipsum</vscode-option>
119+
<vscode-option>Dolor</vscode-option>
120+
</vscode-single-select>
121+
`)) as VscodeSingleSelect;
122+
123+
el.selectedIndex = 999;
124+
await el.updateComplete;
125+
126+
expect(el).shadowDom.to.equal(`
127+
<slot class="main-slot"></slot>
128+
<div class="select-face">
129+
<span class="text">
130+
<span class="empty-label-placeholder"></span>
131+
</span>
132+
<span class="icon">
133+
</span>
134+
</div>
135+
`);
136+
expect(el.value).to.eq('');
137+
expect(el.selectedIndex).to.eq(999);
84138
});
85139

86140
it('should display selected value when an option is clicked', async () => {

src/vscode-select/vscode-single-select.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ export class VscodeSingleSelect extends VscodeSelectBase {
2929
@property({type: Number})
3030
set selectedIndex(val: number) {
3131
this._selectedIndex = val;
32+
this._value = this._options[this._selectedIndex]
33+
? this._options[this._selectedIndex].value
34+
: '';
35+
this._labelText = this._options[this._selectedIndex]
36+
? this._options[this._selectedIndex].label
37+
: '';
3238
}
3339
get selectedIndex(): number {
3440
return this._selectedIndex;

0 commit comments

Comments
 (0)