Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime-vapor): template ref on component #185

Merged
merged 6 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/runtime-vapor/__tests__/dom/templateRef.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { createComponent, ref, setRef, template } from '../../src'
import { makeRender } from '../_utils'

const define = makeRender()

describe('api: template ref', () => {
test('ref on element', () => {
Doctor-wu marked this conversation as resolved.
Show resolved Hide resolved
const t0 = template('<div ref="foo">hello</div>')
const { render } = define({
setup() {
return {
foo: ref(null),
}
},
render() {
const n0 = t0()
setRef(n0 as any, 'foo')
return n0
},
})

const { host, instance } = render()
expect(instance.refs.foo).toBe(host.firstChild)
})

test('ref on component', () => {
const exposeRef = ref<Record<string, string> | undefined>()
const { component: Child } = define({
setup(_, { expose }) {
expose(exposeRef.value)
},
})
const compRef = ref()
const { render } = define({
setup() {
return {
compRef,
}
},
render: () => {
const n0 = createComponent(Child)
setRef(n0, 'compRef')
return n0
},
})

expect(render().instance.refs.compRef).toBeDefined()

const exposeValue = { foo: 'bar' }
exposeRef.value = exposeValue

expect(render().instance.refs.compRef).toEqual(exposeValue)
})
})
6 changes: 6 additions & 0 deletions packages/runtime-vapor/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ export function createComponentInstance(
return instance
}

export const isVaporComponent = (
Doctor-wu marked this conversation as resolved.
Show resolved Hide resolved
val: any,
): val is ComponentInternalInstance => {
return val && componentKey in val
}

function getAttrsProxy(instance: ComponentInternalInstance): Data {
return (
instance.attrsProxy ||
Expand Down
31 changes: 20 additions & 11 deletions packages/runtime-vapor/src/dom/templateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
isRef,
onScopeDispose,
} from '@vue/reactivity'
import { currentInstance } from '../component'
import {
type ComponentInternalInstance,
currentInstance,
isVaporComponent,
} from '../component'
import { VaporErrorCodes, callWithErrorHandling } from '../errorHandling'
import {
EMPTY_OBJ,
Expand All @@ -18,25 +22,30 @@ import { warn } from '../warning'
import { queuePostFlushCb } from '../scheduler'

export type NodeRef = string | Ref | ((ref: Element) => void)
export type RefEl = Element | ComponentInternalInstance

/**
* Function for handling a template ref
*/
export function setRef(el: Element, ref: NodeRef, refFor = false) {
export function setRef(el: RefEl, ref: NodeRef, refFor = false) {
if (!currentInstance) return
const { setupState, isUnmounted } = currentInstance

if (isUnmounted) {
return
}

const refValue = isVaporComponent(el) ? el.exposed || currentInstance : el
Doctor-wu marked this conversation as resolved.
Show resolved Hide resolved

const refs =
currentInstance.refs === EMPTY_OBJ
? (currentInstance.refs = {})
: currentInstance.refs

if (isFunction(ref)) {
const invokeRefSetter = (value: Element | null) => {
const invokeRefSetter = (
value: Element | ComponentInternalInstance['exposed'] | null,
Doctor-wu marked this conversation as resolved.
Show resolved Hide resolved
) => {
callWithErrorHandling(
ref,
currentInstance,
Expand All @@ -45,7 +54,7 @@ export function setRef(el: Element, ref: NodeRef, refFor = false) {
)
}

invokeRefSetter(el)
invokeRefSetter(refValue)
onScopeDispose(() => invokeRefSetter(null))
} else {
const _isString = isString(ref)
Expand All @@ -62,7 +71,7 @@ export function setRef(el: Element, ref: NodeRef, refFor = false) {
: ref.value

if (!isArray(existing)) {
existing = [el]
existing = [refValue]
if (_isString) {
refs[ref] = existing
if (hasOwn(setupState, ref)) {
Expand All @@ -75,16 +84,16 @@ export function setRef(el: Element, ref: NodeRef, refFor = false) {
} else {
ref.value = existing
}
} else if (!existing.includes(el)) {
existing.push(el)
} else if (!existing.includes(refValue)) {
existing.push(refValue)
}
} else if (_isString) {
refs[ref] = el
refs[ref] = refValue
if (hasOwn(setupState, ref)) {
setupState[ref] = el
setupState[ref] = refValue
}
} else if (_isRef) {
ref.value = el
ref.value = refValue
} else if (__DEV__) {
warn('Invalid template ref type:', ref, `(${typeof ref})`)
}
Expand All @@ -95,7 +104,7 @@ export function setRef(el: Element, ref: NodeRef, refFor = false) {
onScopeDispose(() => {
queuePostFlushCb(() => {
if (isArray(existing)) {
remove(existing, el)
remove(existing, refValue)
} else if (_isString) {
refs[ref] = null
if (hasOwn(setupState, ref)) {
Expand Down
Loading