Skip to content

Commit 55c506d

Browse files
fix(radio): updates the tabindex when a radio is added or removed (#30976)
Issue number: resolves internal --------- ## What is the current behavior? When radio buttons are added after the radio group has already been initialised (e.g., as a result of a fetch), the `tabindex` of the radio buttons is not updated. ## What is the new behavior? When a radio button is added or removed, the radio group updates the `tabindex` of its radio buttons in accordance with the existing logic. ## Does this introduce a breaking change? - [ ] Yes - [ ] No ## Other information [Current Behavior Example](https://stackblitz.com/edit/mjr76e54?file=src%2FApp.tsx,src%2Fmain.tsx)
1 parent 5cc8adb commit 55c506d

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

core/src/components.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,6 +3098,7 @@ export namespace Components {
30983098
* The theme determines the visual appearance of the component.
30993099
*/
31003100
"theme"?: "ios" | "md" | "ionic";
3101+
"updateRadiosTabindex": () => Promise<void>;
31013102
/**
31023103
* the value of the radio group.
31033104
*/

core/src/components/radio-group/radio-group.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ComponentInterface, EventEmitter } from '@stencil/core';
22
import { Build, Component, Element, Event, Host, Listen, Method, Prop, State, Watch, h } from '@stencil/core';
33
import { checkInvalidState } from '@utils/forms';
4-
import { renderHiddenInput } from '@utils/helpers';
4+
import { debounce, renderHiddenInput } from '@utils/helpers';
55
import { hostContext } from '@utils/theme';
66

77
import { getIonTheme } from '../../global/ionic-global';
@@ -110,6 +110,15 @@ export class RadioGroup implements ComponentInterface {
110110
this.valueChanged(this.value);
111111
}
112112

113+
/** @internal - Recompute which radio has tabindex 0. Call when radios are added/removed. */
114+
@Method()
115+
async updateRadiosTabindex() {
116+
this.scheduleTabindexUpdate();
117+
}
118+
119+
/** Ensures that the tabindex update is debounced and only called once. */
120+
private scheduleTabindexUpdate = debounce(() => this.setRadioTabindex(this.value), 0);
121+
113122
private setRadioTabindex = (value: any | undefined) => {
114123
const radios = this.getRadios();
115124

core/src/components/radio/radio.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,15 @@ export class Radio implements ComponentInterface {
151151
if (radioGroup) {
152152
this.updateState();
153153
addEventListener(radioGroup, 'ionValueChange', this.updateState);
154+
radioGroup.updateRadiosTabindex();
154155
}
155156
}
156157

157158
disconnectedCallback() {
158159
const radioGroup = this.radioGroup;
159160
if (radioGroup) {
160161
removeEventListener(radioGroup, 'ionValueChange', this.updateState);
162+
radioGroup.updateRadiosTabindex();
161163
this.radioGroup = null;
162164
}
163165
}

0 commit comments

Comments
 (0)