Lucas Stadler лет назад: 12
Родитель
Сommit
e237f05588
1 измененных файлов с 15 добавлено и 4 удалено
  1. 15 4
      clj/clarity/cljs/clarity.cljs

+ 15 - 4
clj/clarity/cljs/clarity.cljs

@ -22,6 +22,12 @@
22 22
      (keyword name)
23 23
      nil)))
24 24
25
(defn read-string-safe [s default]
26
  (try
27
    (r/read-string s)
28
    (catch js/Error e
29
      default)))
30
25 31
(defmulti empty-value
26 32
  (fn get-type [type]
27 33
    (cond
@ -46,6 +52,9 @@
46 52
(defmethod empty-value 'Value [[_ v]]
47 53
  v)
48 54
55
(defmethod empty-value 'Any [{:keys [default]}]
56
  (or default nil))
57
49 58
(defmethod empty-value 'Option [[_ v]]
50 59
  (empty-value v))
51 60
@ -115,6 +124,12 @@
115 124
                           (keyword? value) {:type "text", :pattern keyword-pattern}
116 125
                           :else {:type "text"})))))))
117 126
127
(defmethod make-typed-input 'Any [m owner {:keys [key] :as opts}]
128
  (om/component
129
    (dom/input #js {:type "text"
130
                    :onChange (update-on-change! m key
131
                                                 (assoc opts :transform-fn #(read-string-safe % nil)))})))
132
118 133
(defmethod make-typed-input 'Option [m owner {type :type :as opts}]
119 134
  (let [[_ type] type]
120 135
    (make-typed-input m owner (assoc opts :type type))))
@ -134,10 +149,6 @@
134 149
      (om/build make-typed-input m {:opts {:type (om/value t), :key k, :val (k m)
135 150
                                           :optional? optional?}}))))
136 151
137
(defn read-string-safe [s default]
138
  (let [r (r/push-back-reader s)]
139
    (r/read r false default false)))
140
141 152
(defn update-dynamic-field! [type owner]
142 153
  (fn [ev]
143 154
    (let [k (om/get-state owner :key)