reagent-project / reagent-forms

Bootstrap form components for Reagent
339 stars 78 forks source link

Update form when atom is replaced? #107

Open bpicolo opened 8 years ago

bpicolo commented 8 years ago

I believe I'm hitting similar behavior to a few of the other issues, but it's quite unclear how to resolve.

I have a form as so:

(defn some-form [foo] 
   (let [doc (r/atom {:bar (:bar foo)])
     (fn []
         [bind-fields some-form-template doc])))

The issue is, when some-form is re-rendered (because a level above it is reacting to @\foo), the form does not seem to re-render. (If I throw in a some text field reacting to @\doc here, the text field will update)

What is the proper way to handle this? It's not quite that I am updating the atom externally, it's that I'm asynchronously loading the data that goes into the atom. I want to replace it entirely.

yogthos commented 8 years ago

In your example, you're creating the doc atom once the first time the form is rendered. That code will not be rerun on changes to foo. The only way to do this right now would be to pass foo directly to bind-fields. Unfortunately, not all the fields can handle external updates so YMMV with that approach.

bpicolo commented 8 years ago

@yogthos

The question is more this. I want to populate a form with some data I fetch asynchronously. How can I make sure that, given I start with no data and render an empty form, then load data to populate it, how to I make sure it's populated with that latter data?

yogthos commented 8 years ago

Currently, the data has to be available at the time bind-fields runs. This is the same issue as https://github.com/reagent-project/reagent-forms/issues/19 where the atom has to be updated externally after the form is created.

khdegraaf commented 8 years ago

Actually, it appears to work properly for textbox, and I have a fix for :list. It looks like radio groups and selection groups have this problem still. It looks like a minor fix to handle, I may give it a try for some other fields...

yogthos commented 8 years ago

Yeah that looks promising, if you think you can get it to work I'd love a pr for this.

khdegraaf commented 8 years ago

Tested fix for single-select for selection groups. See mods at https://github.com/khdegraaf/reagent-forms. Multi-select may be fixed too, but haven't tested it. Did test and verify checkbox, textbox (and presumably variations), and datepicker all work in my project. I haven't tested the others. I'll do a push request at some point.

yogthos commented 8 years ago

Fantastic, thanks for spearheading this.