reagent-project / reagent-forms

Bootstrap form components for Reagent
339 stars 78 forks source link

Doc for adding events is off, or there is some other bug #151

Closed acim1 closed 6 years ago

acim1 commented 6 years ago

https://github.com/reagent-project/reagent-forms#adding-events

For the below component, the "id" parameter supplied in the event function was actually a sequence (or (:day), when changing the day). That's why I changed the name to ids to make it less confusing for myself, and updated the logic accordingly. The given is what worked to fire the event when changing day:

(defn day-selector-component []
  [:div
   [:label "Select a day"]
   [bind-fields
    [:select {:field :list :id :day}
     (for [opt (range 1 26)] [:option {:key (keyword (str opt))} opt])]
    state
    (fn [ids _ doc]
      (when (some #{:day} ids)
        (assoc-in doc [:result :status] nil)))]])
yogthos commented 6 years ago

The reason is that the :id represents a path into the document. If you change your select :id key to be :calendar.id, then you'll see (:calendar :id) as the first argument.

yogthos commented 6 years ago

I'm open to switching this to pass in the original id keyword instead so that you'd get :calendar.id instead of :(calendar :id). That might be a more intuitive behavior.

yogthos commented 6 years ago

Just released 0.5.43 with the new API for events. The new version event handler signature accepts the following arguments:

(fn [id path value doc]
      (println id doc)
      (case id
        :calendar.day
        (assoc-in doc [:result :status] :hello)
        value))

The path contains the path for looking up the field in the document, and the id contains the :id of the field in the form.