turn widget rendering upside down

integration-tests
Josha von Gizycki 6 years ago
parent 63bb45587c
commit 245342fa8f

@ -82,71 +82,57 @@
true
(:params req)))
(defn render-input [id {:keys [label required spec]} value req-value validate?]
(list
(when (and validate? (not (spec/valid? spec req-value)))
(spec-to-errmsg label spec req-value))
(hform/label id label)
(hform/text-field {:required (or required false)}
(defn input-widget [id def value]
(hform/text-field {:required (or (:required def) false)}
id
(if validate? req-value value))))
value))
(defn render-checkbox [id {:keys [label spec]} value req-value validate?]
(list
(when (and validate? (not (spec/valid? spec req-value)))
(spec-to-errmsg label spec value))
(hform/label id label)
(defn checkbox-widget [id def value]
(hform/check-box id
(and (some? value) (not= 0 value))
(if validate? req-value value))))
(and (some? value)
(not= 0 value))
value))
(defn render-textarea [id
{:keys [label required spec]}
value
req-value
validate?]
(list
(when (and validate? (not (spec/valid? spec req-value)))
(spec-to-errmsg label spec value))
(hform/label id label)
(hform/text-area {:required (or required false)}
(defn textarea-widget [id def value]
(hform/text-area {:required (or (:required def) false)}
id
(if validate? req-value value))))
value))
(defn render-select [id {:keys [label spec options]} value req-value validate?]
(list
(when (and validate? (not (spec/valid? spec req-value)))
(spec-to-errmsg label spec value))
(hform/label id label)
(defn select-widget [id def value]
(hform/drop-down id
options
(if validate? req-value value))))
(:options def)
value))
(defn render-multiselect [id
{:keys [label spec options]}
value
req-value
validate?]
(list
(when (and validate? (not (spec/valid? spec req-value)))
(spec-to-errmsg label spec value))
(hform/label id label)
(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 (in? (if validate? req-value value)
(second option))}
:selected (in? value (second option))}
(first option)])]))
(def render-mapping
{:input render-input
:checkbox render-checkbox
:textarea render-textarea
:select render-select
:mselect render-multiselect})
(def widget-mapping
{:input input-widget
:checkbox checkbox-widget
:textarea textarea-widget
:select select-widget
:mselect multiselect-widget})
(defn widget
[id def value req-value validate?]
(let [{:keys [label spec widget options]} def
widget (cond (some? widget) widget
(some? options) :select
:else :input)
renderer (widget widget-mapping)]
(list
(when (and validate? (not (spec/valid? spec req-value)))
(spec-to-errmsg label spec req-value))
(hform/label id label)
(renderer id def (if validate? req-value value)))))
(defn form-hash [def values]
(str (hash [def values])))
@ -158,14 +144,10 @@
validate? (= form-hash submitted-hash)]
(conj (map
(fn [[field-id field-def]]
(let [{:keys [options widget to-form]} field-def
(let [{:keys [options to-form]} field-def
value (field-id values)
req-value (get-in req [:params field-id])
widget (cond (some? widget) widget
(some? options) :select
:else :input)
widget-renderer (widget render-mapping)]
(widget-renderer field-id
req-value (get-in req [:params field-id])]
(widget field-id
field-def
(if to-form (to-form value) value)
req-value

Loading…
Cancel
Save