Skip to content

Commit 6a5f636

Browse files
madsrasmusseniOvergaard
authored andcommitted
feat: readonly state for uui-radio-group
1 parent 201917e commit 6a5f636

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ export class UUIRadioGroupElement extends UUIFormControlMixin(LitElement, '') {
3535
@property({ type: Boolean, reflect: true })
3636
disabled = false;
3737

38+
/**
39+
* Sets the input to readonly mode, meaning value cannot be changed but still able to read and select its content.
40+
* @type {boolean}
41+
* @attr
42+
* @default false
43+
*/
44+
@property({ type: Boolean, reflect: true })
45+
readonly = false;
46+
3847
get value() {
3948
return super.value;
4049
}
@@ -121,6 +130,10 @@ export class UUIRadioGroupElement extends UUIFormControlMixin(LitElement, '') {
121130
this._radioElements?.forEach(el => (el.disabled = value));
122131
}
123132

133+
private _setReadonlyOnRadios(value: boolean) {
134+
this._radioElements?.forEach(el => (el.readonly = value));
135+
}
136+
124137
private _handleSlotChange(e: Event) {
125138
e.stopPropagation();
126139
// TODO: make sure to diff new and old ones to only add and remove event listeners on relevant elements.
@@ -152,10 +165,15 @@ export class UUIRadioGroupElement extends UUIFormControlMixin(LitElement, '') {
152165
});
153166

154167
this._setNameOnRadios(this.name);
168+
155169
if (this.disabled) {
156170
this._setDisableOnRadios(true);
157171
}
158172

173+
if (this.readonly) {
174+
this._setReadonlyOnRadios(true);
175+
}
176+
159177
const checkedRadios = this._radioElements.filter(el => el.checked === true);
160178

161179
if (checkedRadios.length > 1) {
@@ -172,13 +190,18 @@ export class UUIRadioGroupElement extends UUIFormControlMixin(LitElement, '') {
172190
}
173191

174192
if (checkedRadios.length === 1) {
175-
this.value = checkedRadios[0].value;
176-
this._selected = this._radioElements.indexOf(checkedRadios[0]);
177-
if (checkedRadios[0].disabled === false) {
193+
const firstCheckedRadio = checkedRadios[0];
194+
this.value = firstCheckedRadio.value;
195+
this._selected = this._radioElements.indexOf(firstCheckedRadio);
196+
197+
if (
198+
firstCheckedRadio.disabled === false &&
199+
firstCheckedRadio.readonly === false
200+
) {
178201
this._radioElements.forEach(el => {
179202
el.makeUnfocusable();
180203
});
181-
checkedRadios[0].makeFocusable();
204+
firstCheckedRadio.makeFocusable();
182205
} else {
183206
this._makeFirstEnabledFocusable();
184207
}
@@ -206,12 +229,16 @@ export class UUIRadioGroupElement extends UUIFormControlMixin(LitElement, '') {
206229
let i = this._selected === null ? 0 : 1; //If we have something selected we will skip checking it self.
207230
while (i < len) {
208231
let checkIndex = (origin + i * direction) % len;
232+
const radioElement = this._radioElements[checkIndex];
233+
209234
if (checkIndex < 0) {
210235
checkIndex += len;
211236
}
212-
if (this._radioElements[checkIndex].disabled === false) {
213-
return this._radioElements[checkIndex];
237+
238+
if (radioElement.disabled === false && radioElement.readonly === false) {
239+
return radioElement;
214240
}
241+
215242
i++;
216243
}
217244
return null;
@@ -281,6 +308,10 @@ export class UUIRadioGroupElement extends UUIFormControlMixin(LitElement, '') {
281308
this._setDisableOnRadios(this.disabled);
282309
}
283310

311+
if (_changedProperties.has('readonly')) {
312+
this._setReadonlyOnRadios(this.readonly);
313+
}
314+
284315
if (_changedProperties.has('name')) {
285316
this._setNameOnRadios(_changedProperties.get('name') as string);
286317
}

0 commit comments

Comments
 (0)