@@ -77,6 +77,11 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
77
77
this . addEventListener ( 'keypress' , this . _onKeypress ) ;
78
78
}
79
79
80
+ connectedCallback ( ) {
81
+ super . connectedCallback ( ) ;
82
+ if ( ! this . hasAttribute ( 'role' ) ) this . setAttribute ( 'role' , 'radiogroup' ) ;
83
+ }
84
+
80
85
/**
81
86
* This method enables <label for="..."> to focus the select
82
87
*/
@@ -102,12 +107,7 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
102
107
return undefined ;
103
108
}
104
109
105
- connectedCallback ( ) {
106
- super . connectedCallback ( ) ;
107
- if ( ! this . hasAttribute ( 'role' ) ) this . setAttribute ( 'role' , 'radiogroup' ) ;
108
- }
109
-
110
- private _radioElements ! : UUIRadioElement [ ] ;
110
+ private _radioElements : UUIRadioElement [ ] = [ ] ;
111
111
private _setNameOnRadios ( name : string ) {
112
112
this . _radioElements ?. forEach ( el => ( el . name = name ) ) ;
113
113
}
@@ -134,52 +134,51 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
134
134
. assignedElements ( { flatten : true } )
135
135
. filter ( el => el instanceof UUIRadioElement ) as UUIRadioElement [ ] ;
136
136
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
- } ) ;
137
+ // Only continue if there are radio elements
138
+ if ( this . _radioElements . length === 0 ) return ;
146
139
147
- this . _setNameOnRadios ( this . name ) ;
148
- if ( this . disabled ) {
149
- this . _setDisableOnRadios ( true ) ;
150
- }
140
+ this . _radioElements . forEach ( el => {
141
+ el . addEventListener (
142
+ UUIRadioEvent . CHANGE ,
143
+ // @ts -ignore TODO: fix typescript error
144
+ this . _handleSelectOnClick as EventHandlerNonNull
145
+ ) ;
146
+ el . addEventListener ( 'blur' , this . _onChildBlur ) ;
147
+ } ) ;
148
+
149
+ this . _setNameOnRadios ( this . name ) ;
150
+ if ( this . disabled ) {
151
+ this . _setDisableOnRadios ( true ) ;
152
+ }
153
+
154
+ const checkedRadios = this . _radioElements . filter ( el => el . checked === true ) ;
151
155
152
- const checkedRadios = this . _radioElements . filter (
153
- el => el . checked === true
156
+ if ( checkedRadios . length > 1 ) {
157
+ this . _radioElements . forEach ( el => {
158
+ el . checked = false ;
159
+ } ) ;
160
+ this . value = '' ;
161
+ console . error (
162
+ 'There can only be one checked radio among the <' +
163
+ this . nodeName +
164
+ '> children' ,
165
+ this
154
166
) ;
167
+ }
155
168
156
- if ( checkedRadios . length > 1 ) {
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 ) {
157
173
this . _radioElements . forEach ( el => {
158
- el . checked = false ;
174
+ el . makeUnfocusable ( ) ;
159
175
} ) ;
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
- }
176
+ checkedRadios [ 0 ] . makeFocusable ( ) ;
180
177
} else {
181
178
this . _makeFirstEnabledFocusable ( ) ;
182
179
}
180
+ } else {
181
+ this . _makeFirstEnabledFocusable ( ) ;
183
182
}
184
183
}
185
184
0 commit comments