20
20
(defn register-editor [t f]
21
21
(swap! editors assoc t f))
22
22
23
- (defn get-editor [t]
23
+ (defn get-editor [t]
24
24
(or (get @editors t) editor-view))
25
25
26
-
27
- (defn get-value [state path]
28
- (when path
29
- (get @state path)))
30
-
31
- (defn set-value [state path v]
32
- (when path
33
- (println " set-value path: " path " value: " v)
34
- (swap! state assoc path v)))
35
-
36
- (defn create-edit-element [state {:keys [path name type] :as options}]
26
+ (defn create-edit-element [{:keys [set-fn get-fn]} {:keys [path name type] :as options}]
37
27
(let [editor (get-editor type)]
38
28
[:<>
39
29
[:span name] ; <label for= "pet-select" >Choose a pet:</label>
40
- [editor {:state state
41
- :set-fn #(set-value state path %)
30
+ [editor {:set-fn (partial set-fn path)
42
31
:options options}
43
- (get-value state path)]]))
32
+ (get-fn path)]]))
33
+
34
+ (defn options-ui2 [{:keys [class style
35
+ edit
36
+ state
37
+ set-fn
38
+ get-fn]
39
+ :or {set-fn (fn [path v]
40
+ ; (println "setting " path " to: " v)
41
+ (swap! state assoc path v))
42
+ get-fn (fn [path]
43
+ (get @state path))}}]
44
+ (into [:div {:style style
45
+ :class class}]
46
+ (map #(create-edit-element {:set-fn set-fn
47
+ :get-fn get-fn} %) edit)))
44
48
45
49
(defn options-ui [{:keys [class style]} ; styling
46
50
{:keys [current state options]
47
51
:or {state (r/atom current)} :as config}] ; data
48
52
(reset! state current)
49
- ( fn [_styling {:keys [state options] :as config}]
50
- ( into [ :div { :style style
51
- :class class}]
52
- ( map #( create-edit-element state %) options))) )
53
+ [options-ui2 {:class class
54
+ :style style
55
+ :state state
56
+ :edit options}] )
0 commit comments