Sfoglia il codice sorgente

fix(runtime-dom): fix unnecessary warning when setting coerced dom property value

fix #6616
Evan You 3 anni fa
parent
commit
b1817fe9ee

+ 2 - 0
packages/runtime-dom/__tests__/patchProps.spec.ts

@@ -240,6 +240,8 @@ describe('runtime-dom: props patching', () => {
     expect(el.size).toBe(100)
     patchProp(el, 'size', 100, null)
     expect(el.getAttribute('size')).toBe(null)
+    expect('Failed setting prop "size" on <input>').not.toHaveBeenWarned()
+    patchProp(el, 'size', null, 'foobar')
     expect('Failed setting prop "size" on <input>').toHaveBeenWarnedLast()
   })
 

+ 2 - 2
packages/runtime-dom/src/modules/props.ts

@@ -63,7 +63,6 @@ export function patchDOMProp(
       needRemove = true
     } else if (type === 'number') {
       // e.g. <img :width="null">
-      // the value of some IDL attr must be greater than 0, e.g. input.size = 0 -> error
       value = 0
       needRemove = true
     }
@@ -96,7 +95,8 @@ export function patchDOMProp(
   try {
     el[key] = value
   } catch (e: any) {
-    if (__DEV__) {
+    // do not warn if value is auto-coerced from nullish values
+    if (__DEV__ && !needRemove) {
       warn(
         `Failed setting prop "${key}" on <${el.tagName.toLowerCase()}>: ` +
           `value ${value} is invalid.`,