pepsighan / ruukh-ui

An experimental next-gen frontend framework for the Web in Rust.
MIT License
0 stars 1 forks source link

Controlled components not possible #34

Open aidanhs opened 5 years ago

aidanhs commented 5 years ago

See DenisKolodin/yew#233 (comment) for context.

Essentially, the desired behavior is: if an edit for a field (e.g. an input or text area) is 'rejected' (i.e. not applied to the model), the render should not update. In react this is known as 'controlled components'.

In ruukh you can test this by applying the patch I've pasted at the bottom and then editing the input element on the input example. Because the internal state isn't changing, arguably neither should the render. Unfortunately, it does.

React solves this by restoring state after a change event (described on the linked issue). Note that react also provides uncontrolled components where the value field is not set. I think the important thing is that both options are available (somehow).

diff --git a/examples/input/src/lib.rs b/examples/input/src/lib.rs
index 200c848..61c2f1c 100644
--- a/examples/input/src/lib.rs
+++ b/examples/input/src/lib.rs
@@ -14,7 +14,7 @@ struct MainApp {
 impl Render for MainApp {
     fn render(&self) -> Markup<Self> {
         html! {
-            "Name: "<input @input={Self::on_input}/>
+            "Name: "<input /*@input={Self::on_input}*/ value={"abc"}/>
             {
                 if !self.input.is_empty() {
                     html! {