Skip to content

Commit b3f8b5a

Browse files
authored
fix(runtime-dom): avoid unset option's value (#10416)
close #10412 re-fix #10396
1 parent bc37258 commit b3f8b5a

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

packages/runtime-dom/__tests__/patchProps.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,18 @@ describe('runtime-dom: props patching', () => {
291291
expect(el.value).toBe('baz')
292292
})
293293

294+
test('init empty value for option', () => {
295+
const root = document.createElement('div')
296+
render(
297+
h('select', { value: 'foo' }, [h('option', { value: '' }, 'foo')]),
298+
root,
299+
)
300+
const select = root.children[0] as HTMLSelectElement
301+
const option = select.children[0] as HTMLOptionElement
302+
expect(select.value).toBe('')
303+
expect(option.value).toBe('')
304+
})
305+
294306
// #8780
295307
test('embedded tag with width and height', () => {
296308
// Width and height of some embedded element such as img、video、source、canvas

packages/runtime-dom/src/modules/props.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ export function patchDOMProp(
3434
// custom elements may use _value internally
3535
!tag.includes('-')
3636
) {
37-
// store value as _value as well since
38-
// non-string values will be stringified.
39-
el._value = value
4037
// #4956: <option> value will fallback to its text content so we need to
4138
// compare against its attribute value instead.
4239
const oldValue =
4340
tag === 'OPTION' ? el.getAttribute('value') || '' : el.value
4441
const newValue = value == null ? '' : value
45-
if (oldValue !== newValue) {
42+
if (oldValue !== newValue || !('_value' in el)) {
4643
el.value = newValue
4744
}
4845
if (value == null) {
4946
el.removeAttribute(key)
5047
}
48+
// store value as _value as well since
49+
// non-string values will be stringified.
50+
el._value = value
5151
return
5252
}
5353

0 commit comments

Comments
 (0)