add attrs key to pass any html attribute to widgets

master
Josha von Gizycki 5 years ago
parent efdfe2ab62
commit 62cb81a59a

1
.gitignore vendored

@ -9,3 +9,4 @@ pom.xml.asc
/.nrepl-port /.nrepl-port
.hgignore .hgignore
.hg/ .hg/
*.iml

@ -19,6 +19,7 @@
#{:input :select :checkbox :textarea :mselect :hidden}) #{:input :select :checkbox :textarea :mselect :hidden})
(spec/def ::from-req ifn?) (spec/def ::from-req ifn?)
(spec/def ::to-form ::from-req) (spec/def ::to-form ::from-req)
(spec/def ::attrs (spec/map-of keyword? (fn [_] true)))
(spec/def ::field (spec/def ::field
(spec/keys :opt-un [::label (spec/keys :opt-un [::label
::options ::options
@ -26,7 +27,8 @@
::from-req ::from-req
::to-form ::to-form
::required ::required
::spec])) ::spec
::attrs]))
(spec/def ::fields (spec/def ::fields
(spec/map-of keyword? ::field)) (spec/map-of keyword? ::field))
(spec/def ::form-specs (spec/def ::form-specs

@ -38,7 +38,8 @@
(hform/label field-id (:label field-def)))) (hform/label field-id (:label field-def))))
(defn input-widget [id def value] (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 id
value)) value))
@ -49,7 +50,8 @@
value)) value))
(defn textarea-widget [id def 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 id
value)) value))
@ -59,19 +61,19 @@
value)) value))
(defn multiselect-widget [id def value] (defn multiselect-widget [id def value]
(let [options (:options def)] [:select (merge {:multiple "multiple"
[:select {:multiple "multiple"
:size (:size def 5) :size (:size def 5)
:name (name id) :name (name id)
:id (name id)} :id (name id)}
(for [option options] (:attrs def))
(for [option (:options def)]
[:option {:value (second option) [:option {:value (second option)
:selected (some? (some (partial = (second option)) :selected (some? (some (partial = (second option))
value))} value))}
(hcore/h (first option))])])) (hcore/h (first option))])])
(comment (comment
(multiselect-widget :id {:options [["<>" "key"]]} {})) (multiselect-widget :id {:options [["<>" "key"]]} {}))
(defn hidden-widget [id def value] (defn hidden-widget [id _ value]
(hform/hidden-field id value)) (hform/hidden-field id value))

Loading…
Cancel
Save