Skip to content

Commit

Permalink
fix: force a name attribute on radio inputs (#1435)
Browse files Browse the repository at this point in the history
Radio inputs now have a correctly filled name attribute.
When this attribute is not set and we bind
a [formGroup] on the input, angular considers that the radio button is global.
It is therefore
impossible to create several different groups of radio buttons on the page (changing one of the
buttons will erase the status of the others).
This fix prevents this unexpected behavior

---------

Co-authored-by: Silke <s.grueber@intershop.de>
  • Loading branch information
tbouliere-datasolution and SGrueber authored Jun 2, 2023
1 parent 597edc9 commit a8f0817
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[formControl]="formControl"
[value]="props.value"
[id]="id"
[attr.name]="radioName"
class="form-check-input"
[ngClass]="props.inputClass"
[attr.data-testing-id]="'radio-' + props.label"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ describe('Radio Field Component', () => {

beforeEach(() => {
const testComponentInputs = {
model: { radio: '' },
model: { rkey: '' },
fields: [
{
key: 'radio',
key: 'rkey',
type: 'radio',
props: {
label: 'radio-label',
value: 'value1',
},
},
],
form: new FormGroup({}),
Expand All @@ -50,5 +54,16 @@ describe('Radio Field Component', () => {
it('should be rendered after creation', () => {
fixture.detectChanges();
expect(element.querySelector('ish-radio-field')).toBeTruthy();
expect(element.querySelector('ish-radio-field')).toMatchInlineSnapshot(`
<ish-radio-field
><input
type="radio"
class="form-check-input"
value="value1"
id="formly_1_radio_rkey_0"
name="formly_0_formly-group__rkey"
data-testing-id="radio-radio-label"
/></ish-radio-field>
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ import { FieldType, FieldTypeConfig } from '@ngx-formly/core';
templateUrl: './radio-field.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class RadioFieldComponent extends FieldType<FieldTypeConfig> {}
export class RadioFieldComponent extends FieldType<FieldTypeConfig> {
get radioName() {
return `${this.field.parent?.id || ''}${this.field.key}`;
}
}

0 comments on commit a8f0817

Please sign in to comment.