From 83884bdc90c62c785061035bcd39c94eb6e90f52 Mon Sep 17 00:00:00 2001 From: Fabiano Taioli Date: Wed, 7 Feb 2018 17:46:39 +0100 Subject: [PATCH] element value is not cleared when value valor change to undefined --- docs/change-log.md | 1 + render/render.js | 4 ++++ render/tests/test-input.js | 10 ++++++++++ 3 files changed, 15 insertions(+) diff --git a/docs/change-log.md b/docs/change-log.md index 7bdc0478..5df143f6 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -43,6 +43,7 @@ - render: fixed an ommission that caused `oninit` to be called unnecessarily in some cases [#1992](https://github.com/MithrilJS/mithril.js/issues/1992) - render: Render state correctly on select change event [#1916](https://github.com/MithrilJS/mithril.js/issues/1916) ([#1918](https://github.com/MithrilJS/mithril.js/pull/1918) [@robinchew](https://github.com/robinchew), [#2052](https://github.com/MithrilJS/mithril.js/pull/2052)) - render: fix various updateNodes/removeNodes issues when the pool and fragments are involved [#1990](https://github.com/MithrilJS/mithril.js/issues/1990), [#1991](https://github.com/MithrilJS/mithril.js/issues/1991), [#2003](https://github.com/MithrilJS/mithril.js/issues/2003), [#2021](https://github.com/MithrilJS/mithril.js/pull/2021) +- render: fix element value don't change if new valor is undefined [#2082](https://github.com/MithrilJS/mithril.js/issues/2082) --- diff --git a/render/render.js b/render/render.js index aed616aa..9efbde96 100644 --- a/render/render.js +++ b/render/render.js @@ -638,6 +638,10 @@ module.exports = function($window) { function setAttr(vnode, key, old, value, ns) { if (key === "key" || key === "is" || isLifecycleMethod(key)) return if (key[0] === "o" && key[1] === "n") return updateEvent(vnode, key, value) + if (typeof value === "undefined" && key === "value" && old !== value) { + vnode.dom.value = "" + return + } if ((old === value && !isFormAttribute(vnode, key)) && typeof value !== "object" || value === undefined) return var element = vnode.dom if (key.slice(0, 6) === "xlink:") element.setAttributeNS("http://www.w3.org/1999/xlink", key, value) diff --git a/render/tests/test-input.js b/render/tests/test-input.js index 1f9a24d7..c443db1a 100644 --- a/render/tests/test-input.js +++ b/render/tests/test-input.js @@ -59,6 +59,16 @@ o.spec("form inputs", function() { o(updated.dom.value).equals("aaa") }) + o("clear element value if vdom value is set to undefined (aka removed)", function() { + var input = {tag: "input", attrs: {value: "aaa", oninput: function() {}}} + var updated = {tag: "input", attrs: {value: undefined, oninput: function() {}}} + + render(root, [input]) + render(root, [updated]) + + o(updated.dom.value).equals("") + }) + o("syncs input checked attribute if DOM value differs from vdom value", function() { var input = {tag: "input", attrs: {type: "checkbox", checked: true, onclick: function() {}}} var updated = {tag: "input", attrs: {type: "checkbox", checked: true, onclick: function() {}}}