Skip to content

Instantly share code, notes, and snippets.

@kindlychung
Created May 10, 2015 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kindlychung/97bf9b74daf0230ea4f6 to your computer and use it in GitHub Desktop.
Save kindlychung/97bf9b74daf0230ea4f6 to your computer and use it in GitHub Desktop.
(ns-unmap *ns* 'fill)
(defn- fill-dispatch [node value]
(if-let [type (and (= :input (:tag node))
(-> node :attrs :type))]
[(keyword (str "input." type)) (class value)]
[(:tag node) (class value)]))
(def fill-hierarchy
(-> (make-hierarchy)
(derive :input.radio ::checkable)
(derive :input.checkbox ::checkable)))
(defmulti fill
#'fill-dispatch
:default nil
:hierarchy #'fill-hierarchy)
(defmethod fill nil
[node value]
(if (= :input (:tag node))
(do
(alter-var-root #'fill-hierarchy
derive
(first (fill-dispatch node value))
:input)
(fill node value))
(assoc node :content [(str value)])))
(defmethod fill
[:input Object] [node value]
(assoc-in node [:attrs :value] (str value)))
(defmethod fill [::checkable clojure.lang.IPersistentSet]
[node value]
(if (contains? value (-> node :attrs :value))
(assoc-in node [:attrs :checked] "checked")
(update-in node [:attrs] dissoc :checked)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment