Skip to content

Commit f676e31

Browse files
author
awb99
committed
select works with any datatype
1 parent 1c644e9 commit f676e31

File tree

2 files changed

+52
-34
lines changed

2 files changed

+52
-34
lines changed

demo/src/demo/page/options.cljs

+14-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
:current {; select
1111
:year 2023
1212
:client 2
13+
:vendor "4"
1314
:pet :hamster
15+
:temperature 36.4
1416
; bool
1517
:run-parallel true
1618
:environment {:enabled true}
@@ -29,11 +31,22 @@
2931
{:id 4 :name "Dumbledor"}
3032
{:id 5 :name "The Hulk"}]
3133
:class "placeholder-gray-400 text-gray-700 relative bg-white rounded text-sm border border-gray-400 outline-none focus:outline-none focus:shadow-outline"}
34+
{:path :vendor
35+
:name "Vendor"
36+
:spec [{:id "1" :name "Batman"}
37+
{:id "2" :name "Robin"}
38+
{:id "3" :name "Harry Potter"}
39+
{:id "4" :name "Dumbledor"}
40+
{:id "5" :name "The Hulk"}]
41+
:class "placeholder-gray-400 text-gray-700 relative bg-white rounded text-sm border border-gray-400 outline-none focus:outline-none focus:shadow-outline"}
3242
{:path :pet
3343
:name "Pet"
3444
:spec [:cat :dog :parrot :hamster]
3545
:class "placeholder-gray-400 text-gray-700 relative bg-white rounded text-sm border border-gray-400 outline-none focus:outline-none focus:shadow-outline"}
36-
46+
{:path :temperature
47+
:name "Temperature"
48+
:spec [36.0 36.4 36.5 36.6 36.7 36.8 39.3]
49+
:class "placeholder-gray-400 text-gray-700 relative bg-white rounded text-sm border border-gray-400 outline-none focus:outline-none focus:shadow-outline"}
3750
{:path :run-parallel
3851
:name "RunParallel?"
3952
:spec :bool
@@ -49,7 +62,6 @@
4962
:spec :button
5063
:class "bg-blue-500 hover:bg-blue-700 text-white font-bold rounded" ; py-2 px-4
5164
:on-click #(js/alert "yeah!")}
52-
5365
{:name "view"
5466
:spec :view
5567
:path :msg}

src/options/editor/select.cljs

+38-32
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,58 @@
11
(ns options.editor.select)
22

3-
(defn entry->option [current-val {:keys [id name]}]
4-
;; works for spec like:
5-
;; [{:id 1 :name "Batman"}
6-
;; {:id 2 :name "Robin"}
7-
;; {:id 3 :name "Harry Potter"}]
8-
(if (= current-val id)
9-
[:option {:value id :selected true} name]
10-
[:option {:value id} name]))
3+
; create-spec from vals
114

12-
(defn spec->map [spec1]
5+
(defn val->map [idx val]
136
;; convert spec like
147
;; [:blue :green :red]
158
;; to a spec with :id and :name
16-
{:id spec1
17-
:name spec1})
9+
{:id (str idx)
10+
:name (str val)
11+
:val val})
12+
13+
(defn vals->spec [vals]
14+
(map-indexed val->map vals))
15+
16+
; create-spec from spec
1817

19-
(defn type-convert [val1 v]
20-
(cond
21-
(number? val1)
22-
(long v)
18+
(defn add-val [{:keys [id] :as opts}]
19+
(assoc opts :val id :id (str id)))
2320

24-
(keyword? val1)
25-
(keyword v)
21+
(defn spec->spec [specs]
22+
(map add-val specs))
2623

27-
(string? val1)
28-
v
24+
; html helper
25+
26+
(defn entry->option [current-val {:keys [id name val]}]
27+
;; works for spec like:
28+
;; [{:id 1 :name "Batman"}
29+
;; {:id 2 :name "Robin"}
30+
;; {:id 3 :name "Harry Potter"}]
31+
(if (= current-val val)
32+
[:option {:value id :selected true} name]
33+
[:option {:value id} name]))
2934

30-
:else
31-
v))
35+
(defn get-id [spec id]
36+
(println "get-id: " id " in spec: " spec)
37+
(-> (filter #(= id (:id %)) spec)
38+
first))
3239

3340
(defn editor-select [{:keys [set-fn options]} current-val]
3441
(let [{:keys [class spec]
3542
:or {class ""
3643
spec []}} options
37-
option1 (first spec)
38-
spec (if (map? option1)
39-
spec
40-
(map spec->map spec))
41-
val1 (-> spec first :id)
42-
otype (type val1)]
43-
(println "select id type: " otype)
44+
mapped? (map? (first spec))
45+
spec (if mapped?
46+
(spec->spec spec)
47+
(vals->spec spec))]
4448
(into [:select {:class class
4549
:on-change (fn [e]
46-
(let [v (-> e .-target .-value)
47-
v2 (type-convert val1 v)]
48-
(println "setting select to: " v "v2: " v2)
49-
(set-fn v2)))}]
50+
(let [id-str (-> e .-target .-value)
51+
entry (get-id spec id-str)
52+
v (:val entry)]
53+
;(println "entry: " entry)
54+
;(println "setting select to id: " id-str "val: " v)
55+
(set-fn v)))}]
5056
(map #(entry->option current-val %) spec))))
5157

5258
(defn select? [spec]

0 commit comments

Comments
 (0)