Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sbb-radio-group): disable focus when disabling radio #3116

Merged
merged 1 commit into from
Sep 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,25 @@ import { SbbRadioButtonGroupElement } from './radio-button-group.js';
expect(firstRadio).to.have.attribute('checked');
});

it('should update tabIndex on disabled child change', async () => {
const firstRadio = element.querySelector('#sbb-radio-1') as
| SbbRadioButtonElement
| SbbRadioButtonPanelElement;

const secondRadio = element.querySelector('#sbb-radio-2') as
| SbbRadioButtonElement
| SbbRadioButtonPanelElement;

expect(firstRadio.tabIndex).to.be.equal(0);
expect(secondRadio.tabIndex).to.be.equal(-1);

firstRadio.disabled = true;
await waitForLitRender(element);

expect(firstRadio.tabIndex).to.be.equal(-1);
expect(secondRadio.tabIndex).to.be.equal(0);
});

it('preserves radio button disabled state after being disabled from group', async () => {
const firstRadio = element.querySelector('#sbb-radio-1') as
| SbbRadioButtonElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
this.addEventListener(
'stateChange',
(e: CustomEvent<SbbStateChange>) =>
this._onRadioButtonSelect(e as CustomEvent<SbbRadioButtonStateChange>),
this._onRadioButtonChange(e as CustomEvent<SbbRadioButtonStateChange>),
{
signal,
passive: true,
Expand Down Expand Up @@ -184,21 +184,26 @@
super.disconnectedCallback();
}

private _onRadioButtonSelect(event: CustomEvent<SbbRadioButtonStateChange>): void {
private _onRadioButtonChange(event: CustomEvent<SbbRadioButtonStateChange>): void {
event.stopPropagation();
if (event.detail.type !== 'checked' || !this._didLoad) {

if (!this._didLoad) {
return;
}

const radioButton = event.target as SbbRadioButtonElement;

if (event.detail.checked) {
this.value = radioButton.value;
this._emitChange(radioButton, this.value);
} else if (this.allowEmptySelection) {
this.value = this.radioButtons.find((radio) => radio.checked)?.value;
if (!this.value) {
this._emitChange(radioButton);
if (event.detail.type === 'disabled') {
this._updateRadios(this.value);
} else if (event.detail.type === 'checked') {
const radioButton = event.target as SbbRadioButtonElement;

if (event.detail.checked) {
this.value = radioButton.value;
this._emitChange(radioButton, this.value);
} else if (this.allowEmptySelection) {
this.value = this.radioButtons.find((radio) => radio.checked)?.value;
if (!this.value) {
this._emitChange(radioButton);
}

Check warning on line 206 in src/elements/radio-button/radio-button-group/radio-button-group.ts

View check run for this annotation

Codecov / codecov/patch

src/elements/radio-button/radio-button-group/radio-button-group.ts#L203-L206

Added lines #L203 - L206 were not covered by tests
}
}
}
Expand Down
Loading