WebReflection / hyperHTML

A Fast & Light Virtual DOM Alternative
ISC License
3.07k stars 112 forks source link

Example of defaults in an input text box #43

Closed BentleyDavis closed 7 years ago

BentleyDavis commented 7 years ago

I have a text box which is populated with the value of the object for the user and I want it to update the object with each character that is entered. The problem is that if I type in the middle of the text, as I type it moves the caret to the end of the text box because it is updating the value. I believe in React they have a DefaultValue option. What is a good way to handle this in HyperHTML?

<input oninput="${events.updated.bind(info)}" value="${info.description}">

Example: https://plnkr.co/edit/jGTLn98Ggv76oqd69OTq?p=preview

WebReflection commented 7 years ago

Interesting use case. You are basically making the information redundant/duplicated on purpose, and you want to update the view with the same value already present in the input.

However, all you really want there is a value set the first time and the first time only.

In such case you can use the wire as instance to flag with properties and eventually operate into its DOM directly when/if needed, like I've done in here.

https://plnkr.co/edit/bTQBnXWHhAi3ecoOXXAo?p=preview

A wire with a single node returns the node itself, you can setup its value once and that's it, it's sync.

Alternatively, you can use utlities like wrist to sync forms and objects.

I would say I've never needed to re-show same text already shown in an input though 😄

BentleyDavis commented 7 years ago

That works. I just needed to default the textbox and your plunker example lays out how to do it. I think I have worked through all the experiments so I should be able to use this for my project.

Thanks