]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): empty boolean props (#844)
authorjods <jods4@users.noreply.github.com>
Mon, 16 Mar 2020 14:19:06 +0000 (15:19 +0100)
committerGitHub <noreply@github.com>
Mon, 16 Mar 2020 14:19:06 +0000 (10:19 -0400)
close #843

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

index bf9377e1b99cd8ec7c5d6b4aa5178cd9a88fcfb2..dad6f8ca8bbce4d394e300d1380dab64338fad3f 100644 (file)
@@ -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 () => {
index 61b8122e8cf9276f51ac7b6c6a134d52d64886a5..67ba81b9b01f7f7af00e181a5f9451a5d48f0635 100644 (file)
@@ -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