-
Notifications
You must be signed in to change notification settings - Fork 14
Add form_builder replacement / smart input components #245
Comments
This is beautifully done. I think your need for Watch is very closely related to ruby-hyperloop/hyper-mesh#47 Watch here is solving the problem of letting you manipulate a copy of the content of an AR attribute (or really any other global state variable) while keeping the copy in sync with master, but only writing back to the master at specific point (i.e. on-blur) |
Thanks. It's a related kind of issue, but I'm not doing this to solve render performance, I didn't have any problem theme. The 2 separate issues I'm solving here: First is to workaround special attribute types, namely Date. If you link a text input directly to a record attribute of type Date then it is impossible to type anything in it—because the field is strictly typed so the date is invalid until you've finished typing the whole thing, but you can't because every character you type causes an error and the input reverts to being empty. The only way to type a date is thus to copy paste in a full valid date. So that's the reason I use an intermediate state value; to let you finish typing the whole thing before assigning the attribute on blur, and Watch allows external attribute changes to update the input too. It works great. The second issue is only being able to type at then end. I thought my intermediate state also fixed that, but it seems not. Could've sworn it did though, so maybe a recent lap has changed state handling and broken this, I suspect so. Anyway, your funky workaround fixes that, so I have updated my example to use said technique too, within the Watch callback. |
Understand that while they are "related" they are not for the same underlying reason. Just thinking we might be able to kill two birds with one stone in solving ruby-hyperloop/hyper-mesh#47 |
A one size fits all form builder is hard. The Rails
form_builder
isn't flexible enough to work with frameworks like Bootstrap, so then there wassimple_form
but that's nowhere near simple. But we might be able to at least provide some smart form field components that are easily customisable.Customisation could be via passing in custom INPUT and LABEL equivalent components, or by inheriting from the field component and changing the render method to suit your UI framework's HTML structure.
This is an example implementation simplified from work app. You simply pass in a model and specify the attribute. The input then can automatically:
name
and labelfrom
(see add#html_attr_name
method hyper-mesh#62 for more)Watch
below).The HTML structure isn't customisable though.
You also need this "Watch" fake component. It lets you trigger a callback when a record attribute changes. In the Input component it is used to update the internal value state if the record attribute changes externally, e.g. by server push. So:
AKA, the input always shows the current "real" value of the record attribute. While you're typing it uses internal state until you're done (blur) then it assigns it back to the attribute.
I also had to use @catmando's technique from to allow typing in the middle of then input. #248
The text was updated successfully, but these errors were encountered: