Skip to content

fix(runtime-vapor): set ref on dynamic component #13172

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

Open
wants to merge 2 commits into
base: vapor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 38 additions & 1 deletion packages/runtime-vapor/__tests__/dom/templateRef.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { NodeRef } from '../../src/apiTemplateRef'
import {
child,
createComponent,
createDynamicComponent,
createFor,
createIf,
createSlot,
createTemplateRefSetter,
defineVaporComponent,
insert,
renderEffect,
template,
Expand All @@ -19,7 +22,8 @@ import {
useTemplateRef,
watchEffect,
} from '@vue/runtime-dom'
import { setElementText } from '../../src/dom/prop'
import { setElementText, setText } from '../../src/dom/prop'
import type { VaporComponent } from '../../src/component'

const define = makeRender()

Expand Down Expand Up @@ -676,6 +680,39 @@ describe('api: template ref', () => {
expect(r!.value).toBe(n)
})

test('work with dynamic component', async () => {
const Child = defineVaporComponent({
setup(_, { expose }) {
const msg = ref('one')
expose({ setMsg: (m: string) => (msg.value = m) })
const n0 = template(`<div> </div>`)() as any
const x0 = child(n0) as any
renderEffect(() => setText(x0, msg.value))
return n0
},
})

const views: Record<string, VaporComponent> = { child: Child }
const view = ref('child')
const refKey = ref<any>(null)

const { html } = define({
setup() {
const setRef = createTemplateRefSetter()
const n0 = createDynamicComponent(() => views[view.value]) as any
setRef(n0, refKey)
return n0
},
}).render()

expect(html()).toBe('<div>one</div><!--dynamic-component-->')
expect(refKey.value).toBeDefined()

refKey.value.setMsg('changed')
await nextTick()
expect(html()).toBe('<div>changed</div><!--dynamic-component-->')
})

// TODO: can not reproduce in Vapor
// // #2078
// test('handling multiple merged refs', async () => {
Expand Down
12 changes: 11 additions & 1 deletion packages/runtime-vapor/src/apiTemplateRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
isString,
remove,
} from '@vue/shared'
import { DynamicFragment } from './block'

export type NodeRef = string | Ref | ((ref: Element) => void)
export type RefEl = Element | VaporComponentInstance
Expand Down Expand Up @@ -49,7 +50,7 @@ export function setRef(
if (!instance || instance.isUnmounted) return

const setupState: any = __DEV__ ? instance.setupState || {} : null
const refValue = isVaporComponent(el) ? getExposed(el) || el : el
const refValue = getRefValue(el)

const refs =
instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs
Expand Down Expand Up @@ -143,3 +144,12 @@ export function setRef(
}
return ref
}

const getRefValue = (el: RefEl) => {
if (isVaporComponent(el)) {
return getExposed(el) || el
} else if (el instanceof DynamicFragment) {
return getRefValue(el.nodes as RefEl)
}
return el
}