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> props?: ComponentPropsOptions<P>
emits?: E | (keyof E)[] emits?: E | (keyof E)[]
inheritAttrs?: boolean inheritAttrs?: boolean
inheritRef?: boolean
displayName?: string displayName?: string
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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