-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.cljs
74 lines (57 loc) · 2.66 KB
/
common.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
(ns ez-wire.form.common
(:require [clojure.spec.alpha :as spec]
[ez-wire.protocols :refer [t]]
[ez-wire.wiring :as wiring]
[reagent.core :as reagent]))
;; allow override of wiring per rendering type (table, list, etc)
(spec/def ::wiring (spec/map-of keyword? any?))
;; allow override of everything
(spec/def ::template vector?)
(spec/def ::form map?)
(spec/def ::content fn?)
(defn render-error-element [{:keys [error-element name css]
:as field} form-map]
(when-not (#{:dispatch} error-element)
(if error-element
[error-element {:model (get-in form-map [:errors name])
:class (get css :error "error")}])))
(defn render-field [{:keys [field-fn name] :as field} form-map]
[field-fn (-> field
(assoc :model (reagent/cursor (:data form-map) [name]))
(dissoc :wiring :template :label))])
(defn render-text [{:keys [field-fn text css] :as field} form-map]
(if text
[:div {:class (get css :text "text")} (t text)]))
(defn render-help [{:keys [field-fn help css] :as field} form-map]
(if help
[:div {:class (get css :help "help")} (t help)]))
(defn render-label [{:keys [css id label name] :as field} form-map]
(if (false? label)
nil
[:label {:for id :class (get css :label "label")} (t (or label name))]))
(defn assoc-wiring [{:keys [name] :as field} params]
(let [params-wiring (:wiring params)]
(assoc field :wiring
(cond (contains? params-wiring name)
(get params-wiring name)
:else
(:wiring field)))))
(defn get-body [row-fn params form-map]
(doall
(map (fn [field]
(let [field (assoc-wiring field params)
;; fetch the row
row (row-fn field form-map)]
;; if we have wiring or label-wiring for the field we replace it using wiring
(cond (:wiring field)
(wiring/wiring row {:$wrapper wiring/unwrapper
:$key {:key (str "ez-wire-form-" (:id field))}
:$label (render-label field form-map)
:$field (render-field field form-map)
:$errors (render-error-element field form-map)
:$text (render-text field form-map)
:$help (render-help field form-map)})
;; otherwise we're good to go with using the default row
:else
row)))
(map #(get-in form-map [:fields %]) (:field-ks form-map)))))