feat(runtime-core): add inheritRef option + make <transition> & <keep-alive> inherit refs

This commit is contained in:
Evan You 2020-05-22 10:26:02 -04:00
parent 88c1e626a7
commit 38f2d23a60
7 changed files with 30 additions and 6 deletions

View File

@ -81,6 +81,7 @@ export interface FunctionalComponent<
props?: ComponentPropsOptions<P>
emits?: E | (keyof E)[]
inheritAttrs?: boolean
inheritRef?: boolean
displayName?: string
}

View File

@ -100,6 +100,7 @@ export interface ComponentOptionsBase<
components?: Record<string, PublicAPIComponent>
directives?: Record<string, Directive>
inheritAttrs?: boolean
inheritRef?: boolean
emits?: E | EE[]
// Internal ------------------------------------------------------------------

View File

@ -172,6 +172,10 @@ export function renderComponentRoot(
}
root.transition = vnode.transition
}
// inherit ref
if (Component.inheritRef && vnode.ref != null) {
root.ref = vnode.ref
}
if (__DEV__ && setRoot) {
setRoot(root)

View File

@ -100,6 +100,8 @@ export function useTransitionState(): TransitionState {
const BaseTransitionImpl = {
name: `BaseTransition`,
inheritRef: true,
props: {
mode: String,
appear: Boolean,

View File

@ -63,6 +63,8 @@ const KeepAliveImpl = {
// would prevent it from being tree-shaken.
__isKeepAlive: true,
inheritRef: true,
props: {
include: [String, RegExp, Array],
exclude: [String, RegExp, Array],

View File

@ -17,7 +17,8 @@ import {
ComponentInternalInstance,
createComponentInstance,
Data,
setupComponent
setupComponent,
Component
} from './component'
import {
renderComponentRoot,
@ -64,6 +65,7 @@ import {
import { createHydrationFunctions, RootHydrateFunction } from './hydration'
import { invokeDirectiveHook } from './directives'
import { startMeasure, endMeasure } from './profiling'
import { ComponentPublicInstance } from './componentProxy'
export interface Renderer<HostElement = any> {
render: RootRenderFunction<HostElement>
@ -276,11 +278,21 @@ export const setRef = (
parent: ComponentInternalInstance,
vnode: VNode | null
) => {
const value = vnode
? vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT
? vnode.component!.proxy
: vnode.el
: null
let value: ComponentPublicInstance | RendererNode | null
if (!vnode) {
value = null
} else {
const { el, component, shapeFlag, type } = vnode
if (shapeFlag & ShapeFlags.COMPONENT && (type as Component).inheritRef) {
return
}
if (shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
value = component!.proxy
} else {
value = el
}
}
const [owner, ref] = rawRef
if (__DEV__ && !owner) {
warn(

View File

@ -37,6 +37,8 @@ export const Transition: FunctionalComponent<TransitionProps> = (
{ slots }
) => h(BaseTransition, resolveTransitionProps(props), slots)
Transition.inheritRef = true
export const TransitionPropsValidators = (Transition.props = {
...(BaseTransition as any).props,
name: String,