From 62cb81a59a90b9f56d645d51cdf6293e583bad9a Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Fri, 26 Jul 2019 10:38:42 +0200 Subject: [PATCH] add attrs key to pass any html attribute to widgets --- .gitignore | 1 + src/formulare/core.clj | 4 +++- src/formulare/theme.clj | 28 +++++++++++++++------------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index c53038e..b52008d 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ pom.xml.asc /.nrepl-port .hgignore .hg/ +*.iml diff --git a/src/formulare/core.clj b/src/formulare/core.clj index 1aeb2e7..e3f0132 100644 --- a/src/formulare/core.clj +++ b/src/formulare/core.clj @@ -19,6 +19,7 @@ #{:input :select :checkbox :textarea :mselect :hidden}) (spec/def ::from-req ifn?) (spec/def ::to-form ::from-req) +(spec/def ::attrs (spec/map-of keyword? (fn [_] true))) (spec/def ::field (spec/keys :opt-un [::label ::options @@ -26,7 +27,8 @@ ::from-req ::to-form ::required - ::spec])) + ::spec + ::attrs])) (spec/def ::fields (spec/map-of keyword? ::field)) (spec/def ::form-specs diff --git a/src/formulare/theme.clj b/src/formulare/theme.clj index bde2f6a..080a726 100644 --- a/src/formulare/theme.clj +++ b/src/formulare/theme.clj @@ -38,7 +38,8 @@ (hform/label field-id (:label field-def)))) (defn input-widget [id def value] - (hform/text-field {:required (or (:required def) false)} + (hform/text-field (merge {:required (or (:required def) false)} + (:attrs def)) id value)) @@ -49,7 +50,8 @@ value)) (defn textarea-widget [id def value] - (hform/text-area {:required (or (:required def) false)} + (hform/text-area (merge {:required (or (:required def) false)} + (:attrs def)) id value)) @@ -59,19 +61,19 @@ value)) (defn multiselect-widget [id def value] - (let [options (:options def)] - [:select {:multiple "multiple" - :size (:size def 5) - :name (name id) - :id (name id)} - (for [option options] - [:option {:value (second option) - :selected (some? (some (partial = (second option)) - value))} - (hcore/h (first option))])])) + [:select (merge {:multiple "multiple" + :size (:size def 5) + :name (name id) + :id (name id)} + (:attrs def)) + (for [option (:options def)] + [:option {:value (second option) + :selected (some? (some (partial = (second option)) + value))} + (hcore/h (first option))])]) (comment (multiselect-widget :id {:options [["<>" "key"]]} {})) -(defn hidden-widget [id def value] +(defn hidden-widget [id _ value] (hform/hidden-field id value))