fix(runtime-dom): allow force updating value bindings for controlled inputs

fix #1471
This commit is contained in:
Evan You 2020-06-30 11:23:09 -04:00
parent 062835d45a
commit b3536d87a5
4 changed files with 19 additions and 9 deletions

View File

@ -96,6 +96,7 @@ export interface RendererOptions<
parentSuspense?: SuspenseBoundary | null, parentSuspense?: SuspenseBoundary | null,
unmountChildren?: UnmountChildrenFn unmountChildren?: UnmountChildrenFn
): void ): void
forcePatchProp?(el: HostElement, key: string): boolean
insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void
remove(el: HostNode): void remove(el: HostNode): void
createElement( createElement(
@ -383,6 +384,7 @@ function baseCreateRenderer(
insert: hostInsert, insert: hostInsert,
remove: hostRemove, remove: hostRemove,
patchProp: hostPatchProp, patchProp: hostPatchProp,
forcePatchProp: hostForcePatchProp,
createElement: hostCreateElement, createElement: hostCreateElement,
createText: hostCreateText, createText: hostCreateText,
createComment: hostCreateComment, createComment: hostCreateComment,
@ -845,7 +847,10 @@ function baseCreateRenderer(
const key = propsToUpdate[i] const key = propsToUpdate[i]
const prev = oldProps[key] const prev = oldProps[key]
const next = newProps[key] const next = newProps[key]
if (prev !== next) { if (
next !== prev ||
(hostForcePatchProp && hostForcePatchProp(el, key))
) {
hostPatchProp( hostPatchProp(
el, el,
key, key,
@ -969,7 +974,10 @@ function baseCreateRenderer(
if (isReservedProp(key)) continue if (isReservedProp(key)) continue
const next = newProps[key] const next = newProps[key]
const prev = oldProps[key] const prev = oldProps[key]
if (next !== prev) { if (
next !== prev ||
(hostForcePatchProp && hostForcePatchProp(el, key))
) {
hostPatchProp( hostPatchProp(
el, el,
key, key,

View File

@ -75,11 +75,8 @@ export const vModelText: ModelDirective<
addEventListener(el, 'change', onCompositionEnd) addEventListener(el, 'change', onCompositionEnd)
} }
}, },
beforeUpdate(el, { value, oldValue, modifiers: { trim, number } }, vnode) { beforeUpdate(el, { value, modifiers: { trim, number } }, vnode) {
el._assign = getModelAssigner(vnode) el._assign = getModelAssigner(vnode)
if (value === oldValue) {
return
}
if (document.activeElement === el) { if (document.activeElement === el) {
if (trim && el.value.trim() === value) { if (trim && el.value.trim() === value) {
return return

View File

@ -10,7 +10,7 @@ import {
RootHydrateFunction RootHydrateFunction
} from '@vue/runtime-core' } from '@vue/runtime-core'
import { nodeOps } from './nodeOps' import { nodeOps } from './nodeOps'
import { patchProp } from './patchProp' import { patchProp, forcePatchProp } from './patchProp'
// Importing from the compiler, will be tree-shaken in prod // Importing from the compiler, will be tree-shaken in prod
import { isFunction, isString, isHTMLTag, isSVGTag, extend } from '@vue/shared' import { isFunction, isString, isHTMLTag, isSVGTag, extend } from '@vue/shared'
@ -21,7 +21,7 @@ declare module '@vue/reactivity' {
} }
} }
const rendererOptions = extend({ patchProp }, nodeOps) const rendererOptions = extend({ patchProp, forcePatchProp }, nodeOps)
// lazy create the renderer - this makes core renderer logic tree-shakable // lazy create the renderer - this makes core renderer logic tree-shakable
// in case the user only imports reactivity utilities from Vue. // in case the user only imports reactivity utilities from Vue.

View File

@ -8,7 +8,12 @@ import { RendererOptions } from '@vue/runtime-core'
const nativeOnRE = /^on[a-z]/ const nativeOnRE = /^on[a-z]/
export const patchProp: RendererOptions<Node, Element>['patchProp'] = ( type DOMRendererOptions = RendererOptions<Node, Element>
export const forcePatchProp: DOMRendererOptions['forcePatchProp'] = (_, key) =>
key === 'value'
export const patchProp: DOMRendererOptions['patchProp'] = (
el, el,
key, key,
prevValue, prevValue,