Skip to content

Commit 1902987

Browse files
iOvergaardJesmoDev
andcommitted
fix: Sync disable state for uui-radio with uui-radio-group (#393)
Co-authored-by: Jesper Møller Jensen <[email protected]>
1 parent 535283c commit 1902987

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
144144
el.addEventListener('blur', this._onChildBlur);
145145
});
146146

147+
this._setNameOnRadios(this.name);
148+
if (this.disabled) {
149+
this._setDisableOnRadios(true);
150+
}
151+
147152
const checkedRadios = this._radioElements.filter(
148153
el => el.checked === true
149154
);
@@ -172,11 +177,6 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
172177
} else {
173178
this._makeFirstEnabledFocusable();
174179
}
175-
176-
this._setNameOnRadios(this.name);
177-
if (this.disabled) {
178-
this._setDisableOnRadios(true);
179-
}
180180
} else {
181181
this._makeFirstEnabledFocusable();
182182
}
@@ -276,7 +276,7 @@ export class UUIRadioGroupElement extends FormControlMixin(LitElement) {
276276
updated(_changedProperties: Map<string | number | symbol, unknown>) {
277277
super.updated(_changedProperties);
278278
if (_changedProperties.has('disabled')) {
279-
this._setDisableOnRadios(_changedProperties.get('disabled') as boolean);
279+
this._setDisableOnRadios(this.disabled);
280280
}
281281

282282
if (_changedProperties.has('name')) {

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { expect, fixture, html, oneEvent } from '@open-wc/testing';
1+
import {
2+
elementUpdated,
3+
expect,
4+
fixture,
5+
html,
6+
oneEvent,
7+
} from '@open-wc/testing';
28

39
import { UUIRadioGroupElement } from './uui-radio-group.element';
410
import { UUIRadioElement } from './uui-radio.element';
@@ -13,7 +19,7 @@ describe('UuiRadio', () => {
1319
let radios: UUIRadioElement[];
1420
beforeEach(async () => {
1521
element = await fixture(html`
16-
<uui-radio-group>
22+
<uui-radio-group name="groupname">
1723
<uui-radio .value=${'Value 1'} label="Option 1">Option 1</uui-radio>
1824
<uui-radio .value=${'Value 2'} label="Option 2"></uui-radio>
1925
<uui-radio .value=${'Value 3'} label="Option 3">Option 3</uui-radio>
@@ -123,6 +129,26 @@ describe('UuiRadioGroup value', () => {
123129
radios[2].click();
124130
expect(element.value).to.equal(radios[2].value);
125131
});
132+
133+
it('name is propagated to radio children', () => {
134+
expect(element.name).to.equal(radios[0].name);
135+
expect(element.name).to.equal(radios[1].name);
136+
expect(element.name).to.equal(radios[2].name);
137+
});
138+
139+
it('disabled is propagated to radio children', () => {
140+
expect(element.disabled).to.equal(radios[0].disabled);
141+
expect(element.disabled).to.equal(radios[1].disabled);
142+
expect(element.disabled).to.equal(radios[2].disabled);
143+
});
144+
145+
it('disabled state on radio-group is reflected on radio children', async () => {
146+
element.disabled = true;
147+
await elementUpdated(element);
148+
expect(element.disabled).to.equal(radios[0].disabled);
149+
expect(element.disabled).to.equal(radios[1].disabled);
150+
expect(element.disabled).to.equal(radios[2].disabled);
151+
});
126152
});
127153

128154
describe('UuiRadioGroup in a Form', () => {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,9 @@ export class UUIRadioElement extends LitElement {
196196
set disabled(newVal) {
197197
const oldVal = this._disabled;
198198
this._disabled = newVal;
199-
if (newVal) {
200-
this.setAttribute('aria-hidden', 'true');
201-
this.setAttribute('tabindex', '-1');
202-
}
199+
200+
this.setAttribute('aria-hidden', newVal ? 'true' : 'false');
201+
this.setAttribute('tabindex', newVal ? '-1' : '0');
203202
this.requestUpdate('disabled', oldVal);
204203
}
205204
private _disabled = false;

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ RadioGroup.parameters = {
124124
</uui-radio-group>
125125
126126
<uui-radio-group name="f331672b-e6f3-4b73-8b44-67a51a24f296">
127-
<uui-radio
127+
<uui-radio
128128
value="Prolific - I live and breathe Umbraco."
129129
label="Prolific - I live and breathe Umbraco."
130130
>
@@ -159,7 +159,7 @@ RadioGroup.parameters = {
159159
label="Lapsed - I miss XSLT. It was all so much better before"
160160
>
161161
</uui-radio>
162-
<uui-radio
162+
<uui-radio
163163
value="Other (Please state)"
164164
label="Other (Please state)"
165165
>
@@ -169,3 +169,15 @@ RadioGroup.parameters = {
169169
},
170170
},
171171
};
172+
173+
export const DisabledGroup: Story = props => html`
174+
<uui-radio-group .disabled=${props.disabled}>
175+
<uui-radio value="1">one</uui-radio>
176+
<uui-radio value="2" .checked=${props.checked}>two</uui-radio>
177+
<uui-radio value="3">three</uui-radio>
178+
<uui-radio value="4">fou2r</uui-radio>
179+
</uui-radio-group>
180+
`;
181+
DisabledGroup.args = {
182+
disabled: true,
183+
};

0 commit comments

Comments
 (0)