fix(runtime-core): empty boolean props (#844)

close #843
This commit is contained in:
jods 2020-03-16 15:19:06 +01:00 committed by GitHub
parent 760c3e0fd6
commit c7ae269972
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 3 deletions

View File

@ -14,7 +14,34 @@ describe('renderer: component', () => {
test.todo('componentProxy') 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', () => { describe('slots', () => {
test('should respect $stable flag', async () => { test('should respect $stable flag', async () => {

View File

@ -260,7 +260,8 @@ function normalizePropsOptions(
const booleanIndex = getTypeIndex(Boolean, prop.type) const booleanIndex = getTypeIndex(Boolean, prop.type)
const stringIndex = getTypeIndex(String, prop.type) const stringIndex = getTypeIndex(String, prop.type)
prop[BooleanFlags.shouldCast] = booleanIndex > -1 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 the prop needs boolean casting or default value
if (booleanIndex > -1 || hasOwn(prop, 'default')) { if (booleanIndex > -1 || hasOwn(prop, 'default')) {
needCastKeys.push(normalizedKey) needCastKeys.push(normalizedKey)
@ -297,7 +298,7 @@ function getTypeIndex(
return i return i
} }
} }
} else if (isObject(expectedTypes)) { } else if (isFunction(expectedTypes)) {
return isSameType(expectedTypes, type) ? 0 : -1 return isSameType(expectedTypes, type) ? 0 : -1
} }
return -1 return -1