@@ -59,14 +59,7 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
59
59
if ( newValue === null || newValue === '' ) {
60
60
this . _makeFirstEnabledFocusable ( ) ;
61
61
}
62
- this . _radioElements . forEach ( ( el , index ) => {
63
- if ( el . value === newValue ) {
64
- el . checked = true ;
65
- this . _selected = index ;
66
- } else {
67
- el . checked = false ;
68
- }
69
- } ) ;
62
+ this . _updateRadioElementsCheckedState ( newValue ) ;
70
63
}
71
64
72
65
private _selected : number | null = null ;
@@ -75,6 +68,16 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
75
68
super ( ) ;
76
69
this . addEventListener ( 'keydown' , this . _onKeydown ) ;
77
70
this . addEventListener ( 'keypress' , this . _onKeypress ) ;
71
+
72
+ // Wait for the radio elements to be added to the dom before updating the checked state.
73
+ this . updateComplete . then ( ( ) => {
74
+ this . _updateRadioElementsCheckedState ( this . value ) ;
75
+ } ) ;
76
+ }
77
+
78
+ connectedCallback ( ) {
79
+ super . connectedCallback ( ) ;
80
+ if ( ! this . hasAttribute ( 'role' ) ) this . setAttribute ( 'role' , 'radiogroup' ) ;
78
81
}
79
82
80
83
/**
@@ -102,16 +105,24 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
102
105
return undefined ;
103
106
}
104
107
105
- connectedCallback ( ) {
106
- super . connectedCallback ( ) ;
107
- if ( ! this . hasAttribute ( 'role' ) ) this . setAttribute ( 'role' , 'radiogroup' ) ;
108
- }
109
-
110
- private _radioElements ! : UUIRadioElement [ ] ;
108
+ private _radioElements : UUIRadioElement [ ] = [ ] ;
111
109
private _setNameOnRadios ( name : string ) {
112
110
this . _radioElements ?. forEach ( el => ( el . name = name ) ) ;
113
111
}
114
112
113
+ private _updateRadioElementsCheckedState (
114
+ newValue : FormData | FormDataEntryValue
115
+ ) {
116
+ this . _radioElements . forEach ( ( el , index ) => {
117
+ if ( el . value === newValue ) {
118
+ el . checked = true ;
119
+ this . _selected = index ;
120
+ } else {
121
+ el . checked = false ;
122
+ }
123
+ } ) ;
124
+ }
125
+
115
126
private _setDisableOnRadios ( value : boolean ) {
116
127
this . _radioElements ?. forEach ( el => ( el . disabled = value ) ) ;
117
128
}
@@ -134,52 +145,51 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
134
145
. assignedElements ( { flatten : true } )
135
146
. filter ( el => el instanceof UUIRadioElement ) as UUIRadioElement [ ] ;
136
147
137
- if ( this . _radioElements . length > 0 ) {
138
- this . _radioElements . forEach ( el => {
139
- el . addEventListener (
140
- UUIRadioEvent . CHANGE ,
141
- // @ts -ignore TODO: fix typescript error
142
- this . _handleSelectOnClick as EventHandlerNonNull
143
- ) ;
144
- el . addEventListener ( 'blur' , this . _onChildBlur ) ;
145
- } ) ;
148
+ // Only continue if there are radio elements
149
+ if ( this . _radioElements . length === 0 ) return ;
146
150
147
- this . _setNameOnRadios ( this . name ) ;
148
- if ( this . disabled ) {
149
- this . _setDisableOnRadios ( true ) ;
150
- }
151
+ this . _radioElements . forEach ( el => {
152
+ el . addEventListener (
153
+ UUIRadioEvent . CHANGE ,
154
+ // @ts -ignore TODO: fix typescript error
155
+ this . _handleSelectOnClick as EventHandlerNonNull
156
+ ) ;
157
+ el . addEventListener ( 'blur' , this . _onChildBlur ) ;
158
+ } ) ;
151
159
152
- const checkedRadios = this . _radioElements . filter (
153
- el => el . checked === true
160
+ this . _setNameOnRadios ( this . name ) ;
161
+ if ( this . disabled ) {
162
+ this . _setDisableOnRadios ( true ) ;
163
+ }
164
+
165
+ const checkedRadios = this . _radioElements . filter ( el => el . checked === true ) ;
166
+
167
+ if ( checkedRadios . length > 1 ) {
168
+ this . _radioElements . forEach ( el => {
169
+ el . checked = false ;
170
+ } ) ;
171
+ this . value = '' ;
172
+ console . error (
173
+ 'There can only be one checked radio among the <' +
174
+ this . nodeName +
175
+ '> children' ,
176
+ this
154
177
) ;
178
+ }
155
179
156
- if ( checkedRadios . length > 1 ) {
180
+ if ( checkedRadios . length === 1 ) {
181
+ this . value = checkedRadios [ 0 ] . value ;
182
+ this . _selected = this . _radioElements . indexOf ( checkedRadios [ 0 ] ) ;
183
+ if ( checkedRadios [ 0 ] . disabled === false ) {
157
184
this . _radioElements . forEach ( el => {
158
- el . checked = false ;
185
+ el . makeUnfocusable ( ) ;
159
186
} ) ;
160
- this . value = '' ;
161
- console . error (
162
- 'There can only be one checked radio among the <' +
163
- this . nodeName +
164
- '> children' ,
165
- this
166
- ) ;
167
- }
168
-
169
- if ( checkedRadios . length === 1 ) {
170
- this . value = checkedRadios [ 0 ] . value ;
171
- this . _selected = this . _radioElements . indexOf ( checkedRadios [ 0 ] ) ;
172
- if ( checkedRadios [ 0 ] . disabled === false ) {
173
- this . _radioElements . forEach ( el => {
174
- el . makeUnfocusable ( ) ;
175
- } ) ;
176
- checkedRadios [ 0 ] . makeFocusable ( ) ;
177
- } else {
178
- this . _makeFirstEnabledFocusable ( ) ;
179
- }
187
+ checkedRadios [ 0 ] . makeFocusable ( ) ;
180
188
} else {
181
189
this . _makeFirstEnabledFocusable ( ) ;
182
190
}
191
+ } else {
192
+ this . _makeFirstEnabledFocusable ( ) ;
183
193
}
184
194
}
185
195
0 commit comments