Skip to content

Commit 7ddaf8c

Browse files
authored
fix: set has-value attribute on radio button click (#2874)
1 parent f2b0583 commit 7ddaf8c

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,13 @@ class RadioGroupElement extends ThemableMixin(DirMixin(PolymerElement)) {
462462
return;
463463
}
464464

465-
if (!this._checkedButton || newV != this._checkedButton.value) {
466-
const newCheckedButton = this._radioButtons.filter((button) => button.value == newV)[0];
465+
const newCheckedButton = this._radioButtons.find((button) => button.value == newV);
467466

468-
if (newCheckedButton) {
469-
this._selectButton(newCheckedButton);
470-
this.setAttribute('has-value', '');
471-
} else {
472-
console.warn(`No <vaadin-radio-button> with value ${newV} found.`);
473-
}
467+
if (newCheckedButton) {
468+
this._selectButton(newCheckedButton);
469+
this.setAttribute('has-value', '');
470+
} else {
471+
console.warn(`No <vaadin-radio-button> with value ${newV} found.`);
474472
}
475473
}
476474

packages/vaadin-radio-button/test/radio-group.test.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,6 @@ describe('radio-group', () => {
180180
expect(buttons[1].checked).to.be.false;
181181
});
182182

183-
it('should not have has-value attribute by default', () => {
184-
expect(group.hasAttribute('has-value')).to.be.false;
185-
});
186-
187-
it('should toggle has-value attribute on value change', () => {
188-
group.value = '2';
189-
expect(group.hasAttribute('has-value')).to.be.true;
190-
group.value = '';
191-
expect(group.hasAttribute('has-value')).to.be.false;
192-
});
193-
194183
it('should dispatch value-changed event when value changes', () => {
195184
const spy = sinon.spy();
196185
group.addEventListener('value-changed', spy);
@@ -215,6 +204,26 @@ describe('radio-group', () => {
215204
});
216205
});
217206

207+
describe('has-value attribute', () => {
208+
it('should not have has-value attribute by default', () => {
209+
expect(group.hasAttribute('has-value')).to.be.false;
210+
});
211+
212+
it('should set has-value on radio button click', () => {
213+
buttons[0].click();
214+
expect(group.hasAttribute('has-value')).to.be.true;
215+
buttons[1].click();
216+
expect(group.hasAttribute('has-value')).to.be.true;
217+
});
218+
219+
it('should toggle has-value attribute on value change', () => {
220+
group.value = '2';
221+
expect(group.hasAttribute('has-value')).to.be.true;
222+
group.value = '';
223+
expect(group.hasAttribute('has-value')).to.be.false;
224+
});
225+
});
226+
218227
describe('roving tabindex', () => {
219228
it('sets focus to the first element by default', () => {
220229
expect(buttons[0].tabIndex).to.eq(0);
@@ -526,6 +535,12 @@ describe('radio-group', () => {
526535
group.invalid = false;
527536
expect(group.hasAttribute('invalid')).to.be.false;
528537
});
538+
539+
it('should run validation only once on radio button click', () => {
540+
const spy = sinon.spy(group, 'validate');
541+
buttons[1].click();
542+
expect(spy.calledOnce).to.be.true;
543+
});
529544
});
530545

531546
describe('errorMessage property', () => {

0 commit comments

Comments
 (0)