Parcourir la Source

support simple union types.

e.g. unions where the possibilities are all of type `Value`.
Lucas Stadler 12 ans auparavant
Parent
commit
5ac0b5ed01
1 fichiers modifiés avec 15 ajouts et 0 suppressions
  1. 15 0
      clj/clarity/cljs/clarity.cljs

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

@ -43,6 +43,9 @@
43 43
(defmethod empty-value 'String [{:keys [default]}]
44 44
  (or default ""))
45 45
46
(defmethod empty-value 'U [[_ & [[_ v]]]]
47
  v)
48
46 49
(defmethod empty-value 'HMap [spec]
47 50
  (let [entries (nth spec 2)]
48 51
    (into {}
@ -79,6 +82,14 @@
79 82
                    :value (om/value string)
80 83
                    :onChange #(om/update! string (fn [_ n] n) (.. % -target -value))})))
81 84
85
(defmethod make-typed-input 'U [value owner {type :type}]
86
  (om/component
87
    (dom/select nil
88
      (into-array
89
        (map (fn [[_ v]]
90
               (dom/option nil (str v)))
91
             (rest type))))))
92
82 93
(defmethod make-typed-input 'HMap [m owner {type :type}]
83 94
  (om/component
84 95
    (dom/div nil
@ -95,6 +106,10 @@
95 106
  (let [type '(HMap :mandatory
96 107
                    {:name {:type String :default "Paul"},
97 108
                     :age {:type Number, :default 10},
109
                     :language (U (Value :en)
110
                                  (Value :de)
111
                                  (Value :fr)
112
                                  (Value :jp))
98 113
                     :gender Keyword})]
99 114
    (atom
100 115
     {:type type