]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: adjust absent prop casting logic
authorEvan You <yyx990803@gmail.com>
Mon, 24 May 2021 23:09:18 +0000 (19:09 -0400)
committerEvan You <yyx990803@gmail.com>
Mon, 24 May 2021 23:09:18 +0000 (19:09 -0400)
packages/runtime-core/src/componentProps.ts

index f94d1522ccd649b0656a098a25a54d2e6f217533..3c9afc7c5c1582ee74d428f705e7e8dc0ee73abd 100644 (file)
@@ -226,7 +226,8 @@ export function updateProps(
               rawCurrentProps,
               camelizedKey,
               value,
-              instance
+              instance,
+              false /* isAbsent */
             )
           }
         } else {
@@ -271,10 +272,11 @@ export function updateProps(
           ) {
             props[key] = resolvePropValue(
               options,
-              rawProps || EMPTY_OBJ,
+              rawCurrentProps,
               key,
               undefined,
-              instance
+              instance,
+              true /* isAbsent */
             )
           }
         } else {
@@ -363,14 +365,17 @@ function setFullProps(
   }
 
   if (needCastKeys) {
+    const rawCurrentProps = toRaw(props)
+    const castValues = rawCastValues || EMPTY_OBJ
     for (let i = 0; i < needCastKeys.length; i++) {
       const key = needCastKeys[i]
       props[key] = resolvePropValue(
         options!,
-        rawCastValues || EMPTY_OBJ,
+        rawCurrentProps,
         key,
-        rawCastValues && rawCastValues[key],
-        instance
+        castValues[key],
+        instance,
+        !hasOwn(castValues, key)
       )
     }
   }
@@ -383,7 +388,8 @@ function resolvePropValue(
   props: Data,
   key: string,
   value: unknown,
-  instance: ComponentInternalInstance
+  instance: ComponentInternalInstance,
+  isAbsent: boolean
 ) {
   const opt = options[key]
   if (opt != null) {
@@ -412,13 +418,13 @@ function resolvePropValue(
     }
     // boolean casting
     if (opt[BooleanFlags.shouldCast]) {
-      if (
+      if (isAbsent && !hasDefault) {
+        value = false
+      } else if (
         opt[BooleanFlags.shouldCastTrue] &&
         (value === '' || value === hyphenate(key))
       ) {
         value = true
-      } else if (!hasOwn(props, key) && !hasDefault) {
-        value = false
       }
     }
   }