Skip to content

Commit 16c23d9

Browse files
committed
radio group
1 parent cb1b232 commit 16c23d9

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

packages/uui-form/lib/uui-form.story.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ import '@umbraco-ui/uui-checkbox/lib';
55
import '@umbraco-ui/uui-slider/lib';
66
import '@umbraco-ui/uui-radio/lib';
77
import '@umbraco-ui/uui-toggle/lib';
8+
import { UUIRadioGroupEvent } from '@umbraco-ui/uui-radio/lib/UUIRadioGroupEvent';
89

910
export default {
1011
id: 'uui-form',
1112
title: 'Inputs/Form',
1213
component: 'uui-form',
1314
};
1415

15-
export const Overview: Story = () => html` <form is="uui-form">
16+
const _onRadioGroupChanged = (e: UUIRadioGroupEvent) => {
17+
e.target.error = e.target.value !== 'radio2';
18+
};
19+
20+
export const Overview: Story = () => html` <form
21+
is="uui-form"
22+
style="max-width: 800px;">
1623
<div style="margin-bottom: 15px;">
1724
<uui-checkbox
1825
name="checkbox"
@@ -31,7 +38,11 @@ export const Overview: Story = () => html` <form is="uui-form">
3138
</div>
3239
3340
<div style="margin-bottom: 15px;">
34-
<uui-radio-group name="radio" label="This is my radio" required>
41+
<uui-radio-group
42+
name="radio"
43+
label="This is my radio"
44+
required
45+
@change=${_onRadioGroupChanged}>
3546
<uui-radio value="radio1" label="radio1" name="radio1">Label</uui-radio>
3647
<uui-radio value="radio2" label="radio2" name="radio2">Label</uui-radio>
3748
<uui-radio value="radio3" label="radio3" name="radio3">Label</uui-radio>

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,14 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
115115
private _handleSlotChange(e: Event) {
116116
// TODO: make sure to diff new and old ones to only add and remove event listeners on relevant elements.
117117

118-
if (this._radioElements) {
119-
this._radioElements.forEach(el => {
120-
el.removeEventListener(
121-
UUIRadioEvent.CHANGE,
122-
// @ts-ignore TODO: fix typescript error
123-
this._handleSelectOnClick as EventHandlerNonNull
124-
);
125-
});
126-
}
118+
this._radioElements?.forEach(el => {
119+
el.removeEventListener(
120+
UUIRadioEvent.CHANGE,
121+
// @ts-ignore TODO: fix typescript error
122+
this._handleSelectOnClick as EventHandlerNonNull
123+
);
124+
el.removeEventListener('blur', this._onChildBlur);
125+
});
127126

128127
this._selected = null;
129128
this._radioElements = (e.target as HTMLSlotElement)
@@ -137,6 +136,7 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
137136
// @ts-ignore TODO: fix typescript error
138137
this._handleSelectOnClick as EventHandlerNonNull
139138
);
139+
el.addEventListener('blur', this._onChildBlur);
140140
});
141141

142142
const checkedRadios = this._radioElements.filter(
@@ -160,6 +160,9 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
160160
this.value = checkedRadios[0].value;
161161
this._selected = this._radioElements.indexOf(checkedRadios[0]);
162162
if (checkedRadios[0].disabled === false) {
163+
this._radioElements.forEach(el => {
164+
el.makeUnfocusable();
165+
});
163166
checkedRadios[0].makeFocusable();
164167
} else {
165168
this._makeFirstEnabledFocusable();
@@ -177,6 +180,9 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
177180

178181
private _makeFirstEnabledFocusable() {
179182
this._selected = null;
183+
this._radioElements?.forEach(el => {
184+
el.makeUnfocusable();
185+
});
180186
this._findNextEnabledElement()?.makeFocusable();
181187
}
182188

@@ -243,6 +249,10 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
243249
this.dispatchEvent(new UUIRadioGroupEvent(UUIRadioGroupEvent.CHANGE));
244250
}
245251

252+
private _onChildBlur = () => {
253+
this.pristine = false;
254+
};
255+
246256
private _handleSelectOnClick = (e: UUIRadioEvent) => {
247257
if (e.target.checked === true) {
248258
this.value = e.target.value;

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,22 @@ export class UUIRadioElement extends LitElement {
161161
public label = '';
162162

163163
@property({ type: Boolean, reflect: true })
164-
public checked = false;
164+
private _checked = false;
165+
public get checked() {
166+
return this._checked;
167+
}
168+
public set checked(value) {
169+
this._checked = value;
170+
if (value === true) {
171+
this.setAttribute('aria-checked', '');
172+
if (!this.disabled) {
173+
this.setAttribute('tabindex', '0');
174+
}
175+
} else {
176+
this.setAttribute('tabindex', '-1');
177+
this.removeAttribute('aria-checked');
178+
}
179+
}
165180

166181
private _disabled = false;
167182

@@ -204,17 +219,10 @@ export class UUIRadioElement extends LitElement {
204219
}
205220

206221
private _onChange() {
207-
if (this.inputElement.checked) {
208-
this.checked = true;
209-
this.setAttribute('aria-checked', '');
210-
if (!this.disabled) {
211-
this.setAttribute('tabindex', '0');
212-
this.focus();
213-
}
214-
} else {
215-
this.checked = false;
216-
this.setAttribute('tabindex', '-1');
217-
this.removeAttribute('aria-checked');
222+
const checked = this.inputElement.checked;
223+
this.checked = checked;
224+
if (checked) {
225+
this.focus();
218226
}
219227
this.dispatchEvent(new UUIRadioEvent(UUIRadioEvent.CHANGE));
220228
}
@@ -243,6 +251,15 @@ export class UUIRadioElement extends LitElement {
243251
this.setAttribute('tabindex', '0');
244252
}
245253
}
254+
/**
255+
* Call to make the element focusable, this sets tabindex to 0.
256+
* @method makeUnfocusable
257+
*/
258+
public makeUnfocusable() {
259+
if (!this.disabled) {
260+
this.setAttribute('tabindex', '-1');
261+
}
262+
}
246263

247264
connectedCallback() {
248265
super.connectedCallback();

0 commit comments

Comments
 (0)