Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-dom): avoid unset option's value #10416

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ describe('runtime-dom: props patching', () => {
expect(el.value).toBe('baz')
})

test('init empty value for option', () => {
const root = document.createElement('div')
render(
h('select', { value: 'foo' }, [h('option', { value: '' }, 'foo')]),
root,
)
const select = root.children[0] as HTMLSelectElement
const option = select.children[0] as HTMLOptionElement
expect(select.value).toBe('')
expect(option.value).toBe('')
})

// #8780
test('embedded tag with width and height', () => {
// Width and height of some embedded element such as img、video、source、canvas
Expand Down
14 changes: 12 additions & 2 deletions packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,23 @@ export function patchDOMProp(
// custom elements may use _value internally
!tag.includes('-')
) {
const mounted = '_value' in el
// store value as _value as well since
// non-string values will be stringified.
el._value = value
// #4956: <option> value will fallback to its text content so we need to
// compare against its attribute value instead.
const oldValue =
tag === 'OPTION' ? el.getAttribute('value') || '' : el.value
let oldValue = el.value
if (tag === 'OPTION') {
const attrValue = el.getAttribute('value')
// #10396
// avoid always resetting nullish option value
// use '' as oldValue when attrValue is falsy
// #10416
// but keep as the original attrValue when dom hasn't been mounted
// so that el.value can be initialized when option initial value is ''
oldValue = mounted ? attrValue || '' : attrValue
Doctor-wu marked this conversation as resolved.
Show resolved Hide resolved
}
const newValue = value == null ? '' : value
if (oldValue !== newValue) {
el.value = newValue
Expand Down
Loading