Skip to content

Commit 04df44c

Browse files
committed
cleanup
1 parent 334113c commit 04df44c

File tree

1 file changed

+42
-43
lines changed

1 file changed

+42
-43
lines changed

packages/uui-radio/lib/uui-radio-group.element.ts

+42-43
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
7777
this.addEventListener('keypress', this._onKeypress);
7878
}
7979

80+
connectedCallback() {
81+
super.connectedCallback();
82+
if (!this.hasAttribute('role')) this.setAttribute('role', 'radiogroup');
83+
}
84+
8085
/**
8186
* This method enables <label for="..."> to focus the select
8287
*/
@@ -102,12 +107,7 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
102107
return undefined;
103108
}
104109

105-
connectedCallback() {
106-
super.connectedCallback();
107-
if (!this.hasAttribute('role')) this.setAttribute('role', 'radiogroup');
108-
}
109-
110-
private _radioElements!: UUIRadioElement[];
110+
private _radioElements: UUIRadioElement[] = [];
111111
private _setNameOnRadios(name: string) {
112112
this._radioElements?.forEach(el => (el.name = name));
113113
}
@@ -134,52 +134,51 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
134134
.assignedElements({ flatten: true })
135135
.filter(el => el instanceof UUIRadioElement) as UUIRadioElement[];
136136

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;
146139

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);
151155

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
154166
);
167+
}
155168

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) {
157173
this._radioElements.forEach(el => {
158-
el.checked = false;
174+
el.makeUnfocusable();
159175
});
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();
180177
} else {
181178
this._makeFirstEnabledFocusable();
182179
}
180+
} else {
181+
this._makeFirstEnabledFocusable();
183182
}
184183
}
185184

0 commit comments

Comments
 (0)