From cb1a744c587223cca9ce6031b40e250739d7fd9c Mon Sep 17 00:00:00 2001 From: undefined Date: Wed, 3 Jun 2020 22:10:05 +0800 Subject: [PATCH 1/3] fix: props default value --- packages/runtime-core/src/componentProps.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index d1989b5c65b..3a9f7145312 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -266,13 +266,16 @@ function resolvePropValue( key: string, value: unknown ) { - const opt = options[key] + const opt = options[key] as any if (opt != null) { const hasDefault = hasOwn(opt, 'default') // default values if (hasDefault && value === undefined) { const defaultValue = opt.default - value = isFunction(defaultValue) ? defaultValue() : defaultValue + value = + opt._vueTypes_name === 'function' && isFunction(defaultValue) + ? defaultValue() + : defaultValue } // boolean casting if (opt[BooleanFlags.shouldCast]) { From d05d2304ec70f87bfd6209e0070a6a4552454854 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Thu, 4 Jun 2020 10:27:03 +0800 Subject: [PATCH 2/3] Update componentProps.ts --- packages/runtime-core/src/componentProps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 3a9f7145312..c9bb90e3eb5 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -273,7 +273,7 @@ function resolvePropValue( if (hasDefault && value === undefined) { const defaultValue = opt.default value = - opt._vueTypes_name === 'function' && isFunction(defaultValue) + opt._vueTypes_name !== 'function' && isFunction(defaultValue) ? defaultValue() : defaultValue } From e7ce4eb0ff0361ae27c4abbdb996d9d91ada67a6 Mon Sep 17 00:00:00 2001 From: undefined Date: Fri, 12 Jun 2020 11:38:48 +0800 Subject: [PATCH 3/3] fix: function type props not correct #1348 --- packages/runtime-core/__tests__/componentProps.spec.ts | 7 +++++++ packages/runtime-core/src/componentProps.ts | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/componentProps.spec.ts b/packages/runtime-core/__tests__/componentProps.spec.ts index 26549cb1eff..c72319d683d 100644 --- a/packages/runtime-core/__tests__/componentProps.spec.ts +++ b/packages/runtime-core/__tests__/componentProps.spec.ts @@ -157,6 +157,7 @@ describe('component props', () => { test('default value', () => { let proxy: any const defaultFn = jest.fn(() => ({ a: 1 })) + const defaultBaz = jest.fn(() => ({ b: 1 })) const Comp = { props: { @@ -165,6 +166,10 @@ describe('component props', () => { }, bar: { default: defaultFn + }, + baz: { + type: Function, + default: defaultBaz } }, render() { @@ -177,7 +182,9 @@ describe('component props', () => { expect(proxy.foo).toBe(2) const prevBar = proxy.bar expect(proxy.bar).toEqual({ a: 1 }) + expect(proxy.baz).toEqual(defaultBaz) expect(defaultFn).toHaveBeenCalledTimes(1) + expect(defaultBaz).toHaveBeenCalledTimes(0) // #999: updates should not cause default factory of unchanged prop to be // called again diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index c9bb90e3eb5..5cd170f5f29 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -273,7 +273,7 @@ function resolvePropValue( if (hasDefault && value === undefined) { const defaultValue = opt.default value = - opt._vueTypes_name !== 'function' && isFunction(defaultValue) + opt.type !== Function && isFunction(defaultValue) ? defaultValue() : defaultValue }