From 44be7684ee2e5ab9a18d29428276030948ce58f1 Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Thu, 20 Jun 2024 17:50:13 +0800 Subject: [PATCH 1/3] fix: attach current instance to render slot --- .../__tests__/componentSlots.spec.ts | 34 +++++++++++++++++++ packages/runtime-vapor/src/componentSlots.ts | 15 ++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/packages/runtime-vapor/__tests__/componentSlots.spec.ts b/packages/runtime-vapor/__tests__/componentSlots.spec.ts index 2ea5715be..987130820 100644 --- a/packages/runtime-vapor/__tests__/componentSlots.spec.ts +++ b/packages/runtime-vapor/__tests__/componentSlots.spec.ts @@ -157,6 +157,40 @@ describe('component: slots', () => { }) test('the current instance should be kept in the slot', async () => { + let instanceInDefaultSlot: any + let instanceInFooSlot: any + + const Comp = defineComponent({ + render() { + const instance = getCurrentInstance() + instance!.slots.default!() + instance!.slots.foo!() + return template('
')() + }, + }) + + const { instance } = define({ + render() { + return createComponent(Comp, {}, [ + { + default: () => { + instanceInDefaultSlot = getCurrentInstance() + return template('content')() + }, + foo: () => { + instanceInFooSlot = getCurrentInstance() + return template('content')() + }, + }, + ]) + }, + }).render() + + expect(instanceInDefaultSlot).toBe(instance) + expect(instanceInFooSlot).toBe(instance) + }) + + test('the current instance should be kept in the dynamic slots', async () => { let instanceInDefaultSlot: any let instanceInVForSlot: any let instanceInVIfSlot: any diff --git a/packages/runtime-vapor/src/componentSlots.ts b/packages/runtime-vapor/src/componentSlots.ts index 022f1dca3..911c3973b 100644 --- a/packages/runtime-vapor/src/componentSlots.ts +++ b/packages/runtime-vapor/src/componentSlots.ts @@ -40,12 +40,21 @@ export function initSlots( if (!rawSlots) return if (!isArray(rawSlots)) rawSlots = [rawSlots] - if (rawSlots.length === 1 && !isDynamicSlotFn(rawSlots[0])) { - instance.slots = rawSlots[0] + const hasDynamicSlots = rawSlots.length > 1 || isDynamicSlotFn(rawSlots[0]) + + const resolved: StaticSlots = (instance.slots = hasDynamicSlots + ? shallowReactive({}) + : {}) + + if (!hasDynamicSlots) { + // with ctx + const slots = rawSlots[0] as StaticSlots + for (const name in slots) { + registerSlot(name, slots[name]) + } return } - const resolved: StaticSlots = (instance.slots = shallowReactive({})) const keys: Set[] = [] rawSlots.forEach((slots, index) => { const isDynamicSlot = isDynamicSlotFn(slots) From 317478c6456c1121745a3655b7ce5f511f2c8522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Thu, 20 Jun 2024 19:13:04 +0800 Subject: [PATCH 2/3] refactor --- packages/runtime-vapor/src/componentSlots.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/packages/runtime-vapor/src/componentSlots.ts b/packages/runtime-vapor/src/componentSlots.ts index 911c3973b..d0400c662 100644 --- a/packages/runtime-vapor/src/componentSlots.ts +++ b/packages/runtime-vapor/src/componentSlots.ts @@ -40,13 +40,9 @@ export function initSlots( if (!rawSlots) return if (!isArray(rawSlots)) rawSlots = [rawSlots] - const hasDynamicSlots = rawSlots.length > 1 || isDynamicSlotFn(rawSlots[0]) - - const resolved: StaticSlots = (instance.slots = hasDynamicSlots - ? shallowReactive({}) - : {}) - - if (!hasDynamicSlots) { + const hasDynamicSlot = rawSlots.some(slot => isDynamicSlotFn(slot)) + if (!hasDynamicSlot) { + instance.slots = {} // with ctx const slots = rawSlots[0] as StaticSlots for (const name in slots) { @@ -55,6 +51,7 @@ export function initSlots( return } + const resolved: StaticSlots = (instance.slots = shallowReactive({})) const keys: Set[] = [] rawSlots.forEach((slots, index) => { const isDynamicSlot = isDynamicSlotFn(slots) From 9a38c1f9717260ba9cc01780bc715ee0a5726b3c Mon Sep 17 00:00:00 2001 From: Rizumu Ayaka Date: Thu, 20 Jun 2024 20:31:48 +0800 Subject: [PATCH 3/3] refactor --- packages/runtime-vapor/src/componentSlots.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/runtime-vapor/src/componentSlots.ts b/packages/runtime-vapor/src/componentSlots.ts index d0400c662..d0f5e3573 100644 --- a/packages/runtime-vapor/src/componentSlots.ts +++ b/packages/runtime-vapor/src/componentSlots.ts @@ -40,8 +40,7 @@ export function initSlots( if (!rawSlots) return if (!isArray(rawSlots)) rawSlots = [rawSlots] - const hasDynamicSlot = rawSlots.some(slot => isDynamicSlotFn(slot)) - if (!hasDynamicSlot) { + if (!rawSlots.some(slot => isDynamicSlotFn(slot))) { instance.slots = {} // with ctx const slots = rawSlots[0] as StaticSlots @@ -51,7 +50,7 @@ export function initSlots( return } - const resolved: StaticSlots = (instance.slots = shallowReactive({})) + instance.slots = shallowReactive({}) const keys: Set[] = [] rawSlots.forEach((slots, index) => { const isDynamicSlot = isDynamicSlotFn(slots) @@ -77,7 +76,7 @@ export function initSlots( : dynamicSlot && dynamicSlot.name === name) ) { recordNames.delete(name) - delete resolved[name] + delete instance.slots[name] } } }) @@ -89,7 +88,7 @@ export function initSlots( }) function registerSlot(name: string, fn: Slot, recordNames?: Set) { - resolved[name] = withCtx(fn) + instance.slots[name] = withCtx(fn) recordNames && recordNames.add(name) }