reagent-project / reagent-forms

Bootstrap form components for Reagent
339 stars 78 forks source link

Add a version of `bind-fields` accepting a map of `get/update!/save!` functions #133

Closed smogg closed 6 years ago

smogg commented 6 years ago

This PR allows a user to pass a map of get/save!/update! functions to override the default bind-fields behavior for those events (as per #127). In particular, this will allow the user to make reagent-forms compatible with libraries like re-frame. For example:

[bind-fields
 [:input {:field :text :id :xyz}]
 {:get (fn [id] (deref (re-frame/subscribe [id])))
  :save! (fn [id v] (re-frame/dispatch [id v]))
  :update! (fn [id v] (re-frame/dispatch [id v]))}]

In this version, bind-fields no longer supports the & events part since it seems redundant - the functions would have the same signature as the functions in above map.

In addition, visible? will now support passing an id to be used by the specified :get function. In above example, an input like this:

[:input {:field :text :visible? :some-subscription}]

would call (deref (re-frame/subscribe [:some-subscription]) and the value returned would be used as a predicate for input's visibility.

yogthos commented 6 years ago

@smogg I think we might've missed a few spots where doc is being dereferenced, such as here and here. I wonder if it might be better for :doc to point to a subscription to the entire document as opposed to doing (assoc doc :doc (:get doc)) for re-frame.