Skip to content

Commit

Permalink
fix(runtime-dom): should remove attribute when binding null to `val…
Browse files Browse the repository at this point in the history
…ue` (#3564)
  • Loading branch information
Justineo authored May 28, 2021
1 parent 071986a commit e3f5dcb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe('runtime-dom: props patching', () => {
// prop with string value should be set to empty string on null values
patchProp(el, 'id', null, null)
expect(el.id).toBe('')
expect(el.getAttribute('id')).toBe(null)
})

test('value', () => {
Expand All @@ -17,12 +18,25 @@ describe('runtime-dom: props patching', () => {
expect(el.value).toBe('foo')
patchProp(el, 'value', null, null)
expect(el.value).toBe('')
expect(el.getAttribute('value')).toBe(null)
const obj = {}
patchProp(el, 'value', null, obj)
expect(el.value).toBe(obj.toString())
expect((el as any)._value).toBe(obj)
})

// For <input type="text">, setting el.value won't create a `value` attribute
// so we need to add tests for other elements
test('value for non-text input', () => {
const el = document.createElement('option')
patchProp(el, 'value', null, 'foo')
expect(el.value).toBe('foo')
patchProp(el, 'value', null, null)
expect(el.value).toBe('')
// #3475
expect(el.getAttribute('value')).toBe(null)
})

test('boolean prop', () => {
const el = document.createElement('select')
patchProp(el, 'multiple', null, '')
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime-dom/src/modules/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export function patchDOMProp(
if (el.value !== newValue) {
el.value = newValue
}
if (value == null) {
el.removeAttribute('value')
}
return
}

Expand Down

0 comments on commit e3f5dcb

Please sign in to comment.