Skip to content

Commit 39e1fa2

Browse files
Fix selected attribute on <wa-option> when <wa-select> has with-clear #1922 (#1985)
* updated wa-option * updated change * added fix to past tests * fixed lock * simplified approach * updated changelog
1 parent 66a873c commit 39e1fa2

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

packages/webawesome/docs/docs/resources/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Components with the <wa-badge variant="warning">Experimental</wa-badge> badge sh
1313
<small>TBD</small>
1414

1515
- Added `wa-button` class for styling `<a>` elements as buttons [pr:2040]
16+
- Fixed a bug in `<wa-select>` where options with `selected` set via framework property binding (e.g., Vue's `:selected`) were not respected when `with-clear` was present [pr:1985]
1617
- Fixed a bug `<wa-color-picker>` that prevented it from flipping horizontally when position to the right of the viewport. [pr:2024]
1718
- Fixed a bug by adding `color: inherit` to the `<wa-dialog>` and `<wa-drawer>` styles so they inherit the text color from the document context rather than the browser default. [pr:2064]
1819
- Fixed a bug that caused 0ms animations to not fire correctly in the internal `animateWithClass()` function [pr#2068]
@@ -30,6 +31,7 @@ Components with the <wa-badge variant="warning">Experimental</wa-badge> badge sh
3031

3132
<small>February 4, 2026</small>
3233

34+
- Fixed a bug in `<wa-select>` where the `selected` attribute on `<wa-option>` was ignored when `with-clear` was present [#1922]
3335
- Added `<wa-file-input>` as an experimental pro component [issue:1240]
3436
- Added `<wa-sparkline>` as an experimental pro component
3537
- Added `<wa-number-input>` as an experimental component for numeric input with stepper buttons [issue:1688]

packages/webawesome/src/components/option/option.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,13 @@ export default class WaOption extends WebAwesomeElement {
147147
if (changedProperties.has('defaultSelected')) {
148148
// We cast to <wa-select> because it shares the same API as combobox
149149
if (!this.closest<WaSelect>('wa-combobox, wa-select')?.hasInteracted) {
150-
const oldVal = this.selected;
151-
this.selected = this.defaultSelected;
152-
this.requestUpdate('selected', oldVal);
150+
// Only sync if defaultSelected is becoming true
151+
// This prevents overwriting `selected` when it was set directly by frameworks like Vue
152+
if (this.defaultSelected) {
153+
const oldVal = this.selected;
154+
this.selected = this.defaultSelected;
155+
this.requestUpdate('selected', oldVal);
156+
}
153157
}
154158
}
155159
super.willUpdate(changedProperties);
@@ -183,6 +187,21 @@ export default class WaOption extends WebAwesomeElement {
183187
}
184188
}
185189

190+
protected firstUpdated(changedProperties: PropertyValues<this>) {
191+
super.firstUpdated(changedProperties);
192+
193+
// If the `selected` property was set directly (e.g., by Vue's :selected binding),
194+
// notify the parent select to update its selection. This is needed because
195+
// Vue binds to the `selected` property instead of the `defaultSelected` property
196+
// when using `:selected="true"` syntax.
197+
if (this.selected && !this.defaultSelected) {
198+
const parent = this.closest<WaSelect>('wa-select, wa-combobox');
199+
if (parent && !parent.hasInteracted) {
200+
parent.selectionChanged?.();
201+
}
202+
}
203+
}
204+
186205
private updateDefaultLabel() {
187206
let oldValue = this.defaultLabel;
188207
this.defaultLabel = getText(this).trim();

packages/webawesome/src/components/select/select.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,47 @@ describe('<wa-select>', () => {
857857
const values = formData.getAll('test');
858858
expect(values).to.have.members(['option with spaces', 'another option']);
859859
});
860+
861+
it('should select options using the selected attribute with with-clear', async () => {
862+
// Issue #1922: selected attribute was ignored when with-clear was present
863+
const el = await fixture<WaSelect>(html`
864+
<wa-select with-clear>
865+
<wa-option value="option-1">Option 1</wa-option>
866+
<wa-option value="option-2" selected>Option 2</wa-option>
867+
<wa-option value="option-3">Option 3</wa-option>
868+
</wa-select>
869+
`);
870+
871+
expect(el.value).to.equal('option-2');
872+
expect(el.displayInput.value).to.equal('Option 2');
873+
});
874+
875+
it('should select options with selected attribute, with-clear, and placeholder', async () => {
876+
// This is the exact combination reported in bug #1922
877+
const el = await fixture<WaSelect>(html`
878+
<wa-select placeholder="Placeholder" with-clear>
879+
<wa-option value="option-1" selected>Option 1</wa-option>
880+
<wa-option value="option-2">Option 2</wa-option>
881+
<wa-option value="option-3">Option 3</wa-option>
882+
</wa-select>
883+
`);
884+
885+
expect(el.value).to.equal('option-1');
886+
expect(el.displayInput.value).to.equal('Option 1');
887+
});
888+
889+
it('should select multiple options with selected attribute and with-clear', async () => {
890+
const el = await fixture<WaSelect>(html`
891+
<wa-select multiple with-clear>
892+
<wa-option value="option-1" selected>Option 1</wa-option>
893+
<wa-option value="option-2" selected>Option 2</wa-option>
894+
<wa-option value="option-3">Option 3</wa-option>
895+
</wa-select>
896+
`);
897+
898+
expect(el.value).to.have.members(['option-1', 'option-2']);
899+
expect(el.value).to.have.length(2);
900+
});
860901
});
861902

862903
it('should allow interaction after being disabled and re-enabled', async () => {

packages/webawesome/src/components/select/select.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ export default class WaSelect extends WebAwesomeFormAssociatedElement {
102102
private selectionOrder: Map<string, number> = new Map();
103103
private typeToSelectString = '';
104104
private typeToSelectTimeout: number;
105-
106105
@query('.select') popup: WaPopup;
107106
@query('.combobox') combobox: HTMLSlotElement;
108107
@query('.display-input') displayInput: HTMLInputElement;

0 commit comments

Comments
 (0)