From 268a3db7876639773988a29c95c46405c85502c1 Mon Sep 17 00:00:00 2001 From: jods4 Date: Sun, 15 Mar 2020 18:24:46 +0100 Subject: [PATCH] fix(runtime-core): empty boolean props close #843 --- .../runtime-core/__tests__/component.spec.ts | 29 ++++++++++++++++++- packages/runtime-core/src/componentProps.ts | 5 ++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/__tests__/component.spec.ts b/packages/runtime-core/__tests__/component.spec.ts index bf9377e1b99..dad6f8ca8bb 100644 --- a/packages/runtime-core/__tests__/component.spec.ts +++ b/packages/runtime-core/__tests__/component.spec.ts @@ -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, { 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 () => { diff --git a/packages/runtime-core/src/componentProps.ts b/packages/runtime-core/src/componentProps.ts index 61b8122e8cf..67ba81b9b01 100644 --- a/packages/runtime-core/src/componentProps.ts +++ b/packages/runtime-core/src/componentProps.ts @@ -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) @@ -297,7 +298,7 @@ function getTypeIndex( return i } } - } else if (isObject(expectedTypes)) { + } else if (isFunction(expectedTypes)) { return isSameType(expectedTypes, type) ? 0 : -1 } return -1