parent
d8b5e6d022
commit
04e14c7455
@ -0,0 +1,70 @@
|
|||||||
|
(ns formulare.theme
|
||||||
|
(:require [clojure.spec.alpha :as spec]
|
||||||
|
[hiccup.form :as hform]))
|
||||||
|
|
||||||
|
(defn row [& content] content)
|
||||||
|
|
||||||
|
(defn err-msg [content]
|
||||||
|
[:section.flash--error
|
||||||
|
[:h2.flash__heading--error "Warning"]
|
||||||
|
content])
|
||||||
|
|
||||||
|
(defn widget-error [id field-def field-value]
|
||||||
|
(err-msg
|
||||||
|
(map
|
||||||
|
(fn [prob]
|
||||||
|
[:p
|
||||||
|
"Field "
|
||||||
|
[:span.flash__field (:label field-def)]
|
||||||
|
" must comply to "
|
||||||
|
[:span.flash__pred (:pred prob)]])
|
||||||
|
(:clojure.spec.alpha/problems
|
||||||
|
(spec/explain-data (:spec field-def)
|
||||||
|
field-value)))))
|
||||||
|
|
||||||
|
(defn form-error [form-def req]
|
||||||
|
(for [prob (mapcat #(:clojure.spec.alpha/problems
|
||||||
|
(spec/explain-data % req))
|
||||||
|
(:form-specs form-def))]
|
||||||
|
(err-msg [:p "The form must comply to "
|
||||||
|
(:pred prob)])))
|
||||||
|
|
||||||
|
(defn label [field-id field-def]
|
||||||
|
(when-not (= :hidden (:widget field-def))
|
||||||
|
(hform/label field-id (:label field-def))))
|
||||||
|
|
||||||
|
(defn input-widget [id def value]
|
||||||
|
(hform/text-field {:required (or (:required def) false)}
|
||||||
|
id
|
||||||
|
value))
|
||||||
|
|
||||||
|
(defn checkbox-widget [id def value]
|
||||||
|
(hform/check-box id
|
||||||
|
(and (some? value)
|
||||||
|
(not= 0 value))
|
||||||
|
value))
|
||||||
|
|
||||||
|
(defn textarea-widget [id def value]
|
||||||
|
(hform/text-area {:required (or (:required def) false)}
|
||||||
|
id
|
||||||
|
value))
|
||||||
|
|
||||||
|
(defn select-widget [id def value]
|
||||||
|
(hform/drop-down id
|
||||||
|
(:options def)
|
||||||
|
value))
|
||||||
|
|
||||||
|
(defn multiselect-widget [id def value]
|
||||||
|
(let [options (:options def)]
|
||||||
|
[:select {:multiple "multiple"
|
||||||
|
:size 5
|
||||||
|
:name (name id)
|
||||||
|
:id (name id)}
|
||||||
|
(for [option options]
|
||||||
|
[:option {:value (second option)
|
||||||
|
:selected (some? (some (partial = (second option))
|
||||||
|
value))}
|
||||||
|
(first option)])]))
|
||||||
|
|
||||||
|
(defn hidden-widget [id def value]
|
||||||
|
(hform/hidden-field id value))
|
@ -0,0 +1,92 @@
|
|||||||
|
(ns formulare.theme-test
|
||||||
|
(:require [clojure.test :refer :all]
|
||||||
|
[formulare.core :refer :all]))
|
||||||
|
|
||||||
|
(deftest row-is-overridable
|
||||||
|
(binding [*row-theme* (fn [& content] [1])]
|
||||||
|
(is (= [1]
|
||||||
|
(nth (render-widgets {:fields {:foo {}}} {} {})
|
||||||
|
2)))))
|
||||||
|
|
||||||
|
(deftest widget-error-is-overridable
|
||||||
|
(binding [*widget-error-theme* (fn [& args] \a)]
|
||||||
|
(let [def {:fields {:foo {:spec (fn [_] false)}}}
|
||||||
|
hash (form-hash def {})]
|
||||||
|
(is (= \a
|
||||||
|
(-> (render-widgets def {}
|
||||||
|
{:params {:__form-hash hash}})
|
||||||
|
(nth 2)
|
||||||
|
first))))))
|
||||||
|
|
||||||
|
(deftest form-error-is-overridable
|
||||||
|
(binding [*form-error-theme* (fn [& args] \a)]
|
||||||
|
(let [def {:fields {:foo {:spec (fn [_] false)}}
|
||||||
|
:form-specs [(fn [_] false)]}
|
||||||
|
hash (form-hash def {})]
|
||||||
|
(is (= \a
|
||||||
|
(first (render-widgets def {}
|
||||||
|
{:params {:__form-hash hash}})))))))
|
||||||
|
|
||||||
|
(deftest label-is-overridable
|
||||||
|
(binding [*label-theme* (fn [& _] \a)]
|
||||||
|
(is (= \a
|
||||||
|
(-> (render-widgets {:fields {:foo {:label "b"}}}
|
||||||
|
{} {})
|
||||||
|
(nth 2)
|
||||||
|
(nth 1))))))
|
||||||
|
|
||||||
|
(deftest input-is-overridable
|
||||||
|
(binding [*input-widget-theme* (fn [& _] \a)]
|
||||||
|
(is (= \a
|
||||||
|
(-> (render-widgets {:fields {:foo {}}}
|
||||||
|
{} {})
|
||||||
|
(nth 2)
|
||||||
|
(nth 2))))))
|
||||||
|
|
||||||
|
(deftest checkbox-is-overridable
|
||||||
|
(binding [*checkbox-widget-theme* (fn [& _] \a)]
|
||||||
|
(is (= \a
|
||||||
|
(-> (render-widgets {:fields {:foo {:widget :checkbox}}}
|
||||||
|
{} {})
|
||||||
|
(nth 2)
|
||||||
|
(nth 2))))))
|
||||||
|
|
||||||
|
(deftest checkbox-is-overridable
|
||||||
|
(binding [*checkbox-widget-theme* (fn [& _] \a)]
|
||||||
|
(is (= \a
|
||||||
|
(-> (render-widgets {:fields {:foo {:widget :checkbox}}}
|
||||||
|
{} {})
|
||||||
|
(nth 2)
|
||||||
|
(nth 2))))))
|
||||||
|
|
||||||
|
(deftest textarea-is-overridable
|
||||||
|
(binding [*textarea-widget-theme* (fn [& _] \a)]
|
||||||
|
(is (= \a
|
||||||
|
(-> (render-widgets {:fields {:foo {:widget :textarea}}}
|
||||||
|
{} {})
|
||||||
|
(nth 2)
|
||||||
|
(nth 2))))))
|
||||||
|
|
||||||
|
(deftest select-is-overridable
|
||||||
|
(binding [*select-widget-theme* (fn [& _] \a)]
|
||||||
|
(is (= \a
|
||||||
|
(-> (render-widgets {:fields {:foo {:widget :select}}}
|
||||||
|
{} {})
|
||||||
|
(nth 2)
|
||||||
|
(nth 2))))))
|
||||||
|
|
||||||
|
(deftest mselect-is-overridable
|
||||||
|
(binding [*mselect-widget-theme* (fn [& _] \a)]
|
||||||
|
(is (= \a
|
||||||
|
(-> (render-widgets {:fields {:foo {:widget :mselect}}}
|
||||||
|
{} {})
|
||||||
|
(nth 2)
|
||||||
|
(nth 2))))))
|
||||||
|
|
||||||
|
(deftest hidden-is-overridable
|
||||||
|
(binding [*hidden-widget-theme* (fn [& _] \a)]
|
||||||
|
(is (= \a
|
||||||
|
(-> (render-widgets {:fields {:foo {:widget :hidden}}}
|
||||||
|
{} {})
|
||||||
|
(nth 2)
|
||||||
|
(nth 2))))))
|
Loading…
Reference in new issue