From c9e5fe701b3c52b3dd4d3274919ac12d4f430bec Mon Sep 17 00:00:00 2001 From: ShadoySV Date: Thu, 28 May 2020 01:55:29 +0300 Subject: [PATCH 1/3] Input current value sync --- yew/src/virtual_dom/vtag.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/yew/src/virtual_dom/vtag.rs b/yew/src/virtual_dom/vtag.rs index 0ed95b482e1..1470f71a2ca 100644 --- a/yew/src/virtual_dom/vtag.rs +++ b/yew/src/virtual_dom/vtag.rs @@ -379,6 +379,27 @@ impl VDiff for VTag { if self.tag == vtag.tag { // If tags are equal, preserve the reference that already exists. self.reference = vtag.reference.take(); + if let Some(element) = self.reference.as_ref() { + if let Some(input) = { + cfg_match! { + feature = "std_web" => InputElement::try_from(element.clone()).ok(), + feature = "web_sys" => element.dyn_ref::(), + } + } { + let current_value = cfg_match! { + feature = "std_web" => input.raw_value(), + feature = "web_sys" => input.value(), + }; + vtag.set_value(¤t_value) + } else if let Some(tae) = { + cfg_match! { + feature = "std_web" => TextAreaElement::try_from(element.clone()).ok(), + feature = "web_sys" => element.dyn_ref::(), + } + } { + vtag.set_value(&tae.value()) + } + } (Reform::Keep, Some(vtag)) } else { // We have to create a new reference, remove ancestor. From 4ccc434d987495edfc2f1db2a482e4a2dee17765 Mon Sep 17 00:00:00 2001 From: ShadoySV Date: Thu, 28 May 2020 16:49:04 +0300 Subject: [PATCH 2/3] check_input_current_value_sync --- yew/src/virtual_dom/vtag.rs | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/yew/src/virtual_dom/vtag.rs b/yew/src/virtual_dom/vtag.rs index 1470f71a2ca..7add8dfd2fb 100644 --- a/yew/src/virtual_dom/vtag.rs +++ b/yew/src/virtual_dom/vtag.rs @@ -1216,4 +1216,67 @@ mod tests { expected ); } + + #[test] + fn check_input_current_value_sync() { + let scope = test_scope(); + let parent = document().create_element("div").unwrap(); + + #[cfg(feature = "std_web")] + document().body().unwrap().append_child(&parent); + #[cfg(feature = "web_sys")] + document().body().unwrap().append_child(&parent).unwrap(); + + let expected = "not_changed_value"; + + // Initial state + let mut elem = html! { + + }; + elem.apply(&scope, &parent, None, None); + + let vtag = if let VNode::VTag(vtag) = elem { + vtag + } else { + panic!("should be vtag") + }; + + // User input + let input_ref = vtag.reference.as_ref().unwrap(); + let input = cfg_match! { + feature = "std_web" => InputElement::try_from(input_ref.clone()).ok(), + feature = "web_sys" => input_ref.dyn_ref::(), + }; + input.unwrap().set_value("User input"); + + let ancestor = vtag; + // Same state after onInput or onChange event + let elem = html! { + + }; + let mut vtag = if let VNode::VTag(vtag) = elem { + vtag + } else { + panic!("should be vtag") + }; + + // Sync happens here + vtag.apply(&scope, &parent, None, Some(VNode::VTag(ancestor))); + + // Get new current value of the input element + let input_ref = vtag.reference.as_ref().unwrap(); + let input = cfg_match! { + feature = "std_web" => InputElement::try_from(input_ref.clone()).ok(), + feature = "web_sys" => input_ref.dyn_ref::(), + } + .unwrap(); + + let current_value = cfg_match! { + feature = "std_web" => input.raw_value(), + feature = "web_sys" => input.value(), + }; + + // check whether not changed virtual dom value has been set to the input element + assert_eq!(current_value, expected); + } } From 916dcb78373441f7a7cdda71b8cc21f997dd1248 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Fri, 29 May 2020 00:24:30 +0800 Subject: [PATCH 3/3] Update yew/src/virtual_dom/vtag.rs --- yew/src/virtual_dom/vtag.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/yew/src/virtual_dom/vtag.rs b/yew/src/virtual_dom/vtag.rs index 4b9a18c1824..61cdb83265f 100644 --- a/yew/src/virtual_dom/vtag.rs +++ b/yew/src/virtual_dom/vtag.rs @@ -1247,7 +1247,10 @@ mod tests { feature = "std_web" => InputElement::try_from(input_ref.clone()).ok(), feature = "web_sys" => input_ref.dyn_ref::(), }; - input.unwrap().set_value("User input"); + cfg_match! { + feature = "std_web" => input.unwrap().set_raw_value("User input"), + feature = "web_sys" => input.unwrap().set_value("User input"), + }; let ancestor = vtag; // Same state after onInput or onChange event