Skip to content

Commit 6ae94ee

Browse files
committed
fix(uui-select): 🐛 sets the value to the value of selected option if present
set the element value to the value of selected option
1 parent 92e2b07 commit 6ae94ee

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

packages/uui-select/lib/uui-select.element.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ export class UUISelectElement extends FormControlMixin(LitElement) {
210210
}
211211

212212
willUpdate(changedProperties: Map<string | number | symbol, unknown>) {
213-
if (changedProperties.has('options')) this._extractGroups();
213+
if (changedProperties.has('options')) {
214+
this._extractGroups();
215+
const selected = this.options.find(option => option.selected);
216+
this.value = selected ? selected.value : '';
217+
}
214218
if (changedProperties.has('disabledGroups')) this._createDisabledGroups();
215219
}
216220

@@ -274,7 +278,8 @@ export class UUISelectElement extends FormControlMixin(LitElement) {
274278
aria-label=${this.label}
275279
@change=${this.setValue}
276280
?disabled=${this.disabled}
277-
.name=${this.name}>
281+
.name=${this.name}
282+
.value=${this.value as string}>
278283
<option disabled selected value="" hidden>${this.placeholder}</option>
279284
${this._renderGrouped()}
280285
${this.options

packages/uui-select/lib/uui-select.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { elementUpdated, expect, fixture, html } from '@open-wc/testing';
33
import { UUISelectElement } from './uui-select.element';
44

55
const options: Array<Option> = [
6-
{ name: 'Carrot', value: 'orange' },
6+
{ name: 'Carrot', value: 'orange', selected: true },
77
{ name: 'Cucumber', value: 'green' },
88
{ name: 'Aubergine', value: 'purple' },
99
{ name: 'Blueberry', value: 'Blue' },
@@ -64,11 +64,7 @@ describe('UUISelect in Form', () => {
6464
beforeEach(async () => {
6565
formElement = await fixture(
6666
html` <form>
67-
<uui-select
68-
label="foo"
69-
name="bar"
70-
.options=${options}
71-
value=${options[0].value}></uui-select>
67+
<uui-select label="foo" name="bar" .options=${options}></uui-select>
7268
</form>`
7369
);
7470
element = formElement.querySelector('uui-select') as any;
@@ -117,7 +113,9 @@ describe('UUISelect in Form', () => {
117113
await elementUpdated(element);
118114
});
119115

120-
it('sets element to invalid when value is empty', () => {
116+
it('sets element to invalid when value is empty', async () => {
117+
element.value = '';
118+
await elementUpdated(element);
121119
expect(element.checkValidity()).to.be.false;
122120
});
123121

0 commit comments

Comments
 (0)