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(runtime-core): empty boolean props #844

Merged
merged 1 commit into from
Mar 16, 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
29 changes: 28 additions & 1 deletion packages/runtime-core/__tests__/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,34 @@ describe('renderer: component', () => {

test.todo('componentProxy')

test.todo('componentProps')
describe('componentProps', () => {
test.todo('should work')

test('should convert empty booleans to true', () => {
let b1: any, b2: any, b3: any

const Comp = defineComponent({
props: {
b1: Boolean,
b2: [Boolean, String],
b3: [String, Boolean]
},
setup(props) {
;({ b1, b2, b3 } = props)
return () => ''
}
})

render(
h(Comp, <any>{ b1: '', b2: '', b3: '' }),
nodeOps.createElement('div')
)

expect(b1).toBe(true)
expect(b2).toBe(true)
expect(b3).toBe('')
})
})

describe('slots', () => {
test('should respect $stable flag', async () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ function normalizePropsOptions(
const booleanIndex = getTypeIndex(Boolean, prop.type)
const stringIndex = getTypeIndex(String, prop.type)
prop[BooleanFlags.shouldCast] = booleanIndex > -1
prop[BooleanFlags.shouldCastTrue] = booleanIndex < stringIndex
prop[BooleanFlags.shouldCastTrue] =
stringIndex < 0 || booleanIndex < stringIndex
// if the prop needs boolean casting or default value
if (booleanIndex > -1 || hasOwn(prop, 'default')) {
needCastKeys.push(normalizedKey)
Expand Down Expand Up @@ -297,7 +298,7 @@ function getTypeIndex(
return i
}
}
} else if (isObject(expectedTypes)) {
} else if (isFunction(expectedTypes)) {
return isSameType(expectedTypes, type) ? 0 : -1
}
return -1
Expand Down