Skip to content

Commit 4823a40

Browse files
authored
feat: add name property to radio-group and propagate it to radio buttons (#8100)
1 parent b9c6590 commit 4823a40

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

packages/radio-group/src/vaadin-radio-group-mixin.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export declare function RadioGroupMixin<T extends Constructor<HTMLElement>>(
5353
T;
5454

5555
export declare class RadioGroupMixinClass {
56+
/**
57+
* The name of the control, which is submitted with the form data.
58+
*/
59+
name: string | null | undefined;
60+
5661
/**
5762
* The value of the radio group.
5863
*/

packages/radio-group/src/vaadin-radio-group-mixin.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ export const RadioGroupMixin = (superclass) =>
2424
class RadioGroupMixinClass extends FieldMixin(FocusMixin(DisabledMixin(KeyboardMixin(superclass)))) {
2525
static get properties() {
2626
return {
27+
/**
28+
* The name of the control, which is submitted with the form data.
29+
*/
30+
name: {
31+
type: String,
32+
observer: '__nameChanged',
33+
},
34+
2735
/**
2836
* The value of the radio group.
2937
*
@@ -190,6 +198,13 @@ export const RadioGroupMixin = (superclass) =>
190198
}
191199
}
192200

201+
/** @private */
202+
__nameChanged(name) {
203+
this.__radioButtons.forEach((radioButton) => {
204+
radioButton.name = name || this._fieldName;
205+
});
206+
}
207+
193208
/**
194209
* @param {number} index
195210
* @private
@@ -234,7 +249,7 @@ export const RadioGroupMixin = (superclass) =>
234249
* @private
235250
*/
236251
__registerRadioButton(radioButton) {
237-
radioButton.name = this._fieldName;
252+
radioButton.name = this.name || this._fieldName;
238253
radioButton.addEventListener('checked-changed', this.__onRadioButtonCheckedChange);
239254

240255
if (this.disabled || this.readonly) {

packages/radio-group/test/radio-group.common.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,43 @@ describe('radio-group', () => {
9696
groupName = group._fieldName;
9797
});
9898

99-
it('should set the group name to the dynamically added radio buttons', async () => {
99+
it('should set the default name to the dynamically added radio buttons', async () => {
100100
const radio = document.createElement('vaadin-radio-button');
101101
group.appendChild(radio);
102102
await nextFrame();
103103

104104
expect(radio.name).to.equal(groupName);
105105
});
106+
107+
it('should propagate the group name to the existing radio buttons', async () => {
108+
group.name = 'name';
109+
await nextFrame();
110+
buttons.forEach((radio) => {
111+
expect(radio.name).to.equal(group.name);
112+
});
113+
});
114+
115+
it('should propagate the group name to the dynamically added radio buttons', async () => {
116+
group.name = 'name';
117+
await nextFrame();
118+
119+
const radio = document.createElement('vaadin-radio-button');
120+
group.appendChild(radio);
121+
await nextFrame();
122+
123+
expect(radio.name).to.equal(group.name);
124+
});
125+
126+
it('should restore the default name on the radio buttons when group name removed', async () => {
127+
group.name = 'name';
128+
await nextFrame();
129+
130+
group.name = null;
131+
await nextFrame();
132+
buttons.forEach((radio) => {
133+
expect(radio.name).to.equal(groupName);
134+
});
135+
});
106136
});
107137

108138
describe('readonly property', () => {

packages/radio-group/test/typings/radio-button.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ radio.addEventListener('checked-changed', (event) => {
5151

5252
const group = document.createElement('vaadin-radio-group');
5353

54+
// Group properties
55+
assertType<string | null | undefined>(group.name);
56+
assertType<string | null | undefined>(group.value);
57+
assertType<boolean>(group.readonly);
58+
5459
// Group mixins
5560
assertType<ControllerMixinClass>(group);
5661
assertType<DisabledMixinClass>(group);

0 commit comments

Comments
 (0)