Skip to content

Commit c7ae269

Browse files
authored
fix(runtime-core): empty boolean props (#844)
close #843
1 parent 760c3e0 commit c7ae269

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

+28-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,34 @@ describe('renderer: component', () => {
1414

1515
test.todo('componentProxy')
1616

17-
test.todo('componentProps')
17+
describe('componentProps', () => {
18+
test.todo('should work')
19+
20+
test('should convert empty booleans to true', () => {
21+
let b1: any, b2: any, b3: any
22+
23+
const Comp = defineComponent({
24+
props: {
25+
b1: Boolean,
26+
b2: [Boolean, String],
27+
b3: [String, Boolean]
28+
},
29+
setup(props) {
30+
;({ b1, b2, b3 } = props)
31+
return () => ''
32+
}
33+
})
34+
35+
render(
36+
h(Comp, <any>{ b1: '', b2: '', b3: '' }),
37+
nodeOps.createElement('div')
38+
)
39+
40+
expect(b1).toBe(true)
41+
expect(b2).toBe(true)
42+
expect(b3).toBe('')
43+
})
44+
})
1845

1946
describe('slots', () => {
2047
test('should respect $stable flag', async () => {

packages/runtime-core/src/componentProps.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ function normalizePropsOptions(
260260
const booleanIndex = getTypeIndex(Boolean, prop.type)
261261
const stringIndex = getTypeIndex(String, prop.type)
262262
prop[BooleanFlags.shouldCast] = booleanIndex > -1
263-
prop[BooleanFlags.shouldCastTrue] = booleanIndex < stringIndex
263+
prop[BooleanFlags.shouldCastTrue] =
264+
stringIndex < 0 || booleanIndex < stringIndex
264265
// if the prop needs boolean casting or default value
265266
if (booleanIndex > -1 || hasOwn(prop, 'default')) {
266267
needCastKeys.push(normalizedKey)
@@ -297,7 +298,7 @@ function getTypeIndex(
297298
return i
298299
}
299300
}
300-
} else if (isObject(expectedTypes)) {
301+
} else if (isFunction(expectedTypes)) {
301302
return isSameType(expectedTypes, type) ? 0 : -1
302303
}
303304
return -1

0 commit comments

Comments
 (0)