Last active
May 7, 2016 05:45
-
-
Save pleasetrythisathome/3e14892686846ef33ef6 to your computer and use it in GitHub Desktop.
om forms - form parent local state as cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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