Skip to content

Commit

Permalink
feat: add name property to radio-group and propagate it to radio butt…
Browse files Browse the repository at this point in the history
…ons (#8100)
  • Loading branch information
web-padawan authored Nov 7, 2024
1 parent b9c6590 commit 4823a40
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/radio-group/src/vaadin-radio-group-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export declare function RadioGroupMixin<T extends Constructor<HTMLElement>>(
T;

export declare class RadioGroupMixinClass {
/**
* The name of the control, which is submitted with the form data.
*/
name: string | null | undefined;

/**
* The value of the radio group.
*/
Expand Down
17 changes: 16 additions & 1 deletion packages/radio-group/src/vaadin-radio-group-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export const RadioGroupMixin = (superclass) =>
class RadioGroupMixinClass extends FieldMixin(FocusMixin(DisabledMixin(KeyboardMixin(superclass)))) {
static get properties() {
return {
/**
* The name of the control, which is submitted with the form data.
*/
name: {
type: String,
observer: '__nameChanged',
},

/**
* The value of the radio group.
*
Expand Down Expand Up @@ -190,6 +198,13 @@ export const RadioGroupMixin = (superclass) =>
}
}

/** @private */
__nameChanged(name) {
this.__radioButtons.forEach((radioButton) => {
radioButton.name = name || this._fieldName;
});
}

/**
* @param {number} index
* @private
Expand Down Expand Up @@ -234,7 +249,7 @@ export const RadioGroupMixin = (superclass) =>
* @private
*/
__registerRadioButton(radioButton) {
radioButton.name = this._fieldName;
radioButton.name = this.name || this._fieldName;
radioButton.addEventListener('checked-changed', this.__onRadioButtonCheckedChange);

if (this.disabled || this.readonly) {
Expand Down
32 changes: 31 additions & 1 deletion packages/radio-group/test/radio-group.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,43 @@ describe('radio-group', () => {
groupName = group._fieldName;
});

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

expect(radio.name).to.equal(groupName);
});

it('should propagate the group name to the existing radio buttons', async () => {
group.name = 'name';
await nextFrame();
buttons.forEach((radio) => {
expect(radio.name).to.equal(group.name);
});
});

it('should propagate the group name to the dynamically added radio buttons', async () => {
group.name = 'name';
await nextFrame();

const radio = document.createElement('vaadin-radio-button');
group.appendChild(radio);
await nextFrame();

expect(radio.name).to.equal(group.name);
});

it('should restore the default name on the radio buttons when group name removed', async () => {
group.name = 'name';
await nextFrame();

group.name = null;
await nextFrame();
buttons.forEach((radio) => {
expect(radio.name).to.equal(groupName);
});
});
});

describe('readonly property', () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/radio-group/test/typings/radio-button.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ radio.addEventListener('checked-changed', (event) => {

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

// Group properties
assertType<string | null | undefined>(group.name);
assertType<string | null | undefined>(group.value);
assertType<boolean>(group.readonly);

// Group mixins
assertType<ControllerMixinClass>(group);
assertType<DisabledMixinClass>(group);
Expand Down

0 comments on commit 4823a40

Please sign in to comment.