Skip to content

Instantly share code, notes, and snippets.

@micha

micha/form.clj Secret

Created August 16, 2015 22:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micha/c266cec4c2c36c576445 to your computer and use it in GitHub Desktop.
Save micha/c266cec4c2c36c576445 to your computer and use it in GitHub Desktop.
(ns ui.form)
(defmacro with-form
[[binding form-machine] & body]
`(let [form-machine# ~form-machine
~binding form-machine#
{data# :data
error# :error
state# :state
exception# :exception
loading# :loading} form-machine#]
(binding [*form-machine* form-machine#
*form-exception* exception#
*form-inputs* data#
*form-errors* error#
*form-state* state#
*form-loading* loading#]
~@body)))
(defmacro with-field
[attr & body]
`(let [[error# data#] (map ~attr [*form-errors* *form-inputs*])]
(binding [*input-error* error#
*input-data* data#
*input-name* (gensym)]
~@body)))
(defmacro definput
[name [attr kids] & body]
`(tailrecursion.hoplon/defelem ~name
[{state# :state
id# :id
:as attr#} kids#]
(let [state# (or state# *input-data*)
id# (or id# *input-name*)
~attr (merge attr# {:state state# :id id#})
~kids kids#]
~@body)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment