Skip to content

Commit d437a01

Browse files
authored
fix(runtime-core): default value for function type prop (vuejs#1349)
fix vuejs#1348
1 parent 4c4f39b commit d437a01

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

packages/runtime-core/__tests__/componentProps.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ describe('component props', () => {
158158
test('default value', () => {
159159
let proxy: any
160160
const defaultFn = jest.fn(() => ({ a: 1 }))
161+
const defaultBaz = jest.fn(() => ({ b: 1 }))
161162

162163
const Comp = {
163164
props: {
@@ -166,6 +167,10 @@ describe('component props', () => {
166167
},
167168
bar: {
168169
default: defaultFn
170+
},
171+
baz: {
172+
type: Function,
173+
default: defaultBaz
169174
}
170175
},
171176
render() {
@@ -178,7 +183,9 @@ describe('component props', () => {
178183
expect(proxy.foo).toBe(2)
179184
const prevBar = proxy.bar
180185
expect(proxy.bar).toEqual({ a: 1 })
186+
expect(proxy.baz).toEqual(defaultBaz)
181187
expect(defaultFn).toHaveBeenCalledTimes(1)
188+
expect(defaultBaz).toHaveBeenCalledTimes(0)
182189

183190
// #999: updates should not cause default factory of unchanged prop to be
184191
// called again

packages/runtime-core/src/componentProps.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,16 @@ function resolvePropValue(
270270
key: string,
271271
value: unknown
272272
) {
273-
const opt = options[key]
273+
const opt = options[key] as any
274274
if (opt != null) {
275275
const hasDefault = hasOwn(opt, 'default')
276276
// default values
277277
if (hasDefault && value === undefined) {
278278
const defaultValue = opt.default
279-
value = isFunction(defaultValue) ? defaultValue() : defaultValue
279+
value =
280+
opt.type !== Function && isFunction(defaultValue)
281+
? defaultValue()
282+
: defaultValue
280283
}
281284
// boolean casting
282285
if (opt[BooleanFlags.shouldCast]) {

0 commit comments

Comments
 (0)