Просмотр исходного кода

generate the initial value from the type.

this also means that types support defaults now.
Lucas Stadler лет назад: 12
Родитель
Сommit
5229dc38a4
1 измененных файлов с 30 добавлено и 3 удалено
  1. 30 3
      clj/clarity/cljs/clarity.cljs

+ 30 - 3
clj/clarity/cljs/clarity.cljs

@ -27,6 +27,29 @@
27 27
      (keyword name)
28 28
      nil)))
29 29
30
(defmulti empty-value
31
  (fn get-type [type]
32
    (cond
33
      (sequential? type) (first type)
34
      (map? type) (get-type (:type type))
35
      :else type)))
36
37
(defmethod empty-value 'Number [{:keys [default]}]
38
  (or default 0))
39
40
(defmethod empty-value 'Keyword [{:keys [default]}]
41
  (or default :keyword))
42
43
(defmethod empty-value 'String [{:keys [default]}]
44
  (or default ""))
45
46
(defmethod empty-value 'HMap [spec]
47
  (let [entries (nth spec 2)]
48
    (into {}
49
          (map (fn [[k v]]
50
                 [k (empty-value v)])
51
               entries))))
52
30 53
(defmulti make-typed-input
31 54
  (fn [_ _ {type :type} & _]
32 55
    (cond
@ -68,9 +91,13 @@
68 91
      (dom/span nil "}"))))
69 92
70 93
(def app-state
71
  (atom
72
    '{:type (HMap :mandatory {:name String, :age Number, :gender Keyword})
73
      :data {:name "Paul", :age 3, :gender :unknown}}))
94
  (let [type '(HMap :mandatory
95
                    {:name {:type String :default "Paul"},
96
                     :age {:type Number, :default 10},
97
                     :gender Keyword})]
98
    (atom
99
     {:type type
100
      :data (empty-value type)})))
74 101
75 102
(defn typed-input [{:keys [type data]} owner]
76 103
  (reify