Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/vast-buses-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@sl-design-system/radio-group': patch
---

Fix bug where radiogroup is invalid even though it has an initial value

The validity was not updated as part of the `slotchange` handler, causing the radiogroup to be invalid on initial render even though one of the radios was checked. This has been fixed by calling `#updateValueAndValidity` at the end of the `slotchange` handler.
32 changes: 32 additions & 0 deletions packages/components/radio-group/src/radio-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,38 @@ describe('sl-radio-group', () => {
});
});

describe('initial value', () => {
it('should be valid when the initial value matches one of the options', async () => {
el = await fixture(html`
<sl-radio-group value="2" required>
<sl-radio value="1">Option 1</sl-radio>
<sl-radio value="2">Option 2</sl-radio>
<sl-radio value="3">Option 3</sl-radio>
</sl-radio-group>
`);

await new Promise(resolve => setTimeout(resolve));

expect(el.value).to.equal('2');
expect(el.valid).to.be.true;
});

it('should be invalid when the initial value does not match any of the options', async () => {
el = await fixture(html`
<sl-radio-group value="dummy" required>
<sl-radio value="1">Option 1</sl-radio>
<sl-radio value="2">Option 2</sl-radio>
<sl-radio value="3">Option 3</sl-radio>
</sl-radio-group>
`);

await new Promise(resolve => setTimeout(resolve));

expect(el.value).to.equal('dummy');
expect(el.valid).to.be.false;
});
});

describe('validation', () => {
beforeEach(async () => {
el = await fixture(html`
Expand Down
1 change: 1 addition & 0 deletions packages/components/radio-group/src/radio-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export class RadioGroup<T = any> extends FormControlMixin(LitElement) {
}

this.#observer.observe(this, OBSERVER_OPTIONS);
this.#updateValueAndValidity();
}

#setSelectedOption(option?: Radio<T>, emitEvent = true): void {
Expand Down