make element properties optional

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
jhe 2018-09-02 22:18:00 +02:00 committed by Julien Eluard
parent 7bd37d5021
commit 95e44c9eee
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
3 changed files with 21 additions and 10 deletions

View File

@ -27,10 +27,12 @@
{:data m}))
(defn hiccup-with-properties [h properties]
(if (vector? h)
(let [[tag props & children] h
{:keys [data]} (inject-properties props properties)]
(apply conj [tag data]
(if (vector? h)
(let [[tag & properties-children] h
[props children] (views/resolve-properties-children properties-children)
{:keys [data]} (when properties
(inject-properties props properties))]
(apply conj (if data [tag data] [tag])
(map #(hiccup-with-properties % properties) children)))
h))

View File

@ -70,6 +70,12 @@
:errors []}
properties))
(defn resolve-properties-children [[properties? & children]]
[(and (map? properties?) properties?)
(if (map? properties?)
children
(cons properties? children))])
(defn parse-hiccup-element [{:keys [capacities] :as opts} o]
(let [explain (spec/explain-data ::form o)]
(cond
@ -80,13 +86,17 @@
(or (symbol? o) (utils/primitive? o)) {:data o}
(vector? o)
(let [[element properties & children] o
component (resolve-element capacities element)
{:keys [data errors]} (resolve-element-properties capacities properties)]
(let [[element & properties-children] o
[properties children] (resolve-properties-children properties-children)
component (resolve-element capacities element)
{:keys [data errors]} (when properties
(resolve-element-properties capacities properties))]
(cond-> (let [m (parse-hiccup-children opts children)]
;; Reduce parsed children to a single map and wrap them in a hiccup element
;; whose component has been translated to the local platform
(update m :data #(apply conj [(or component element) data] %)))
(update m :data #(apply conj (if data [(or component element) data]
[(or component element)])
%)))
(nil? component) (errors/accumulate-errors [(errors/error ::errors/unknown-component element)])
(seq errors) (errors/accumulate-errors errors))))))

View File

@ -28,7 +28,6 @@
(is (= {:data [:text {} "Hello"]}
(views/parse {:capacities {:components {'text :text}}} ['text {} "Hello"])))
(is (= {:data [:view
{}
[:text {} "Hello"]
[blocks/let-block
{:env {'cond? '@queries/random-boolean}}
@ -38,7 +37,7 @@
[:text {:style {:color "red"}} "World?"]]]]}
(views/parse {:capacities {:components {'text :text
'view :view}}}
'[view {}
'[view
[text {} "Hello"]
(let [cond? @queries/random-boolean]
(if cond?