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

fix: function type props not correct #1349

Merged
merged 3 commits into from
Jun 12, 2020
Merged
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
7 changes: 7 additions & 0 deletions packages/runtime-core/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -165,6 +166,10 @@ describe('component props', () => {
},
bar: {
default: defaultFn
},
baz: {
type: Function,
default: defaultBaz
}
},
render() {
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.type !== Function && isFunction(defaultValue)
? defaultValue()
: defaultValue
}
// boolean casting
if (opt[BooleanFlags.shouldCast]) {
Expand Down