Skip to content

Commit

Permalink
fix(runtime-dom): allow force updating value bindings for controlled …
Browse files Browse the repository at this point in the history
…inputs

fix #1471
  • Loading branch information
yyx990803 committed Jun 30, 2020
1 parent 062835d commit b3536d8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
12 changes: 10 additions & 2 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface RendererOptions<
parentSuspense?: SuspenseBoundary | null,
unmountChildren?: UnmountChildrenFn
): void
forcePatchProp?(el: HostElement, key: string): boolean
insert(el: HostNode, parent: HostElement, anchor?: HostNode | null): void
remove(el: HostNode): void
createElement(
Expand Down Expand Up @@ -383,6 +384,7 @@ function baseCreateRenderer(
insert: hostInsert,
remove: hostRemove,
patchProp: hostPatchProp,
forcePatchProp: hostForcePatchProp,
createElement: hostCreateElement,
createText: hostCreateText,
createComment: hostCreateComment,
Expand Down Expand Up @@ -845,7 +847,10 @@ function baseCreateRenderer(
const key = propsToUpdate[i]
const prev = oldProps[key]
const next = newProps[key]
if (prev !== next) {
if (
next !== prev ||
(hostForcePatchProp && hostForcePatchProp(el, key))
) {
hostPatchProp(
el,
key,
Expand Down Expand Up @@ -969,7 +974,10 @@ function baseCreateRenderer(
if (isReservedProp(key)) continue
const next = newProps[key]
const prev = oldProps[key]
if (next !== prev) {
if (
next !== prev ||
(hostForcePatchProp && hostForcePatchProp(el, key))
) {
hostPatchProp(
el,
key,
Expand Down
5 changes: 1 addition & 4 deletions packages/runtime-dom/src/directives/vModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,8 @@ export const vModelText: ModelDirective<
addEventListener(el, 'change', onCompositionEnd)
}
},
beforeUpdate(el, { value, oldValue, modifiers: { trim, number } }, vnode) {
beforeUpdate(el, { value, modifiers: { trim, number } }, vnode) {
el._assign = getModelAssigner(vnode)
if (value === oldValue) {
return
}
if (document.activeElement === el) {
if (trim && el.value.trim() === value) {
return
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-dom/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
RootHydrateFunction
} from '@vue/runtime-core'
import { nodeOps } from './nodeOps'
import { patchProp } from './patchProp'
import { patchProp, forcePatchProp } from './patchProp'
// Importing from the compiler, will be tree-shaken in prod
import { isFunction, isString, isHTMLTag, isSVGTag, extend } from '@vue/shared'

Expand All @@ -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
// in case the user only imports reactivity utilities from Vue.
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime-dom/src/patchProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { RendererOptions } from '@vue/runtime-core'

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,
key,
prevValue,
Expand Down

0 comments on commit b3536d8

Please sign in to comment.