Skip to content

Commit b7e01e5

Browse files
johanrdNullVoxPopuli
authored andcommitted
[BUGFIX] Fix missing value attribute on radio/checkbox inputs bound to empty string (#19219)
1 parent e9c61cc commit b7e01e5

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

packages/@ember/-internals/glimmer/tests/integration/input-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,30 @@ moduleFor(
201201
this.assertValue('hola', 'Value is used');
202202
}
203203

204+
['@test GH19219 input[type=radio] value attribute is rendered when bound to empty string']() {
205+
this.render(`<input type="radio" value={{this.value}}>`, { value: '' });
206+
207+
this.assert.strictEqual(
208+
this.inputElement().getAttribute('value'),
209+
'',
210+
'value attribute is present and empty on initial render'
211+
);
212+
this.assertPropertyHasValue('value', '', 'value property is empty string on initial render');
213+
214+
runTask(() => this.rerender());
215+
216+
this.assert.strictEqual(
217+
this.inputElement().getAttribute('value'),
218+
'',
219+
'value attribute is present after no-op rerender'
220+
);
221+
this.assertPropertyHasValue(
222+
'value',
223+
'',
224+
'value property is empty string after no-op rerender'
225+
);
226+
}
227+
204228
['@test GH18211 input checked attribute, without a value, works with attributes with values']() {
205229
this.render(`<input type="checkbox" checked>`, {});
206230
this.assertPropertyHasValue('checked', true);

packages/@glimmer/runtime/lib/vm/attributes/dynamic.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,15 @@ export class SafeDynamicAttribute extends SimpleDynamicAttribute {
178178

179179
export class InputValueDynamicAttribute extends DefaultDynamicProperty {
180180
override set(dom: TreeBuilder, value: unknown) {
181-
dom.__setProperty('value', normalizeStringValue(value));
181+
const normalized = normalizeStringValue(value);
182+
dom.__setProperty('value', normalized);
183+
184+
// GH#19219: Browsers don't reflect `input.value = ''` as a value attribute when
185+
// type is later changed to "radio"/"checkbox". Explicitly set the attribute for <input>.
186+
// Not needed for <textarea> (no value attribute).
187+
if (value === '' && this.attribute.element.tagName === 'INPUT') {
188+
dom.__setAttribute('value', '', null);
189+
}
182190
}
183191

184192
override update(value: unknown) {

0 commit comments

Comments
 (0)