Skip to content

Instantly share code, notes, and snippets.

@pleasetrythisathome
Last active May 7, 2016 05:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pleasetrythisathome/3e14892686846ef33ef6 to your computer and use it in GitHub Desktop.
Save pleasetrythisathome/3e14892686846ef33ef6 to your computer and use it in GitHub Desktop.
om forms - form parent local state as cursor
(def inputs [{:korks :email}
{:korks [:name :first]}
{:korks [:name :last]}])
(defn form [app owner]
(reify
om/IInitState
(init-state [this]
(reduce (fn [state {:keys [korks]}]
(assoc-in state korks nil))
{}
inputs))
om/IRenderState
(render-state [this state]
(letfn [(submit []
(print state))]
(html
[:form {:on-submit (fn [e]
(.preventDefault e)
(submit))}
(map (fn [{:keys [korks]}]
(om/build input state {:react-key (string/join (map name korks) "/")
:opts {:korks korks
:on-change (partial om/set-state! owner korks)}}))
inputs)])))))
(defn input [data owner {:keys [korks
on-change]
:or {on-change (partial om/update! data korks)}}]
(reify
om/IRender
(render [this]
(let [korks (if (keyword? korks)
(vector korks)
korks)
value (get-in data korks)]
(html
[:input {:value value
:on-change (fn [e]
(on-change (.. e -target -value)))}])))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment