diff --git a/shadow-cljs.edn b/shadow-cljs.edn index 81d3960..f492656 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -6,7 +6,8 @@ "src/test/resources"] :dependencies [[aero "1.1.6"] [orchestra "2021.01.01-1"] - [expound "0.8.9"]] + [expound "0.8.9"] + [hickory "0.7.1"]] :builds {:test {:target :node-test :output-to "target/node-tests.js" :autorun true diff --git a/src/main/cljs/dda/c4k_common/browser.cljs b/src/main/cljs/dda/c4k_common/browser.cljs index aedcc07..155c807 100644 --- a/src/main/cljs/dda/c4k_common/browser.cljs +++ b/src/main/cljs/dda/c4k_common/browser.cljs @@ -3,7 +3,8 @@ [clojure.string :as st] [clojure.spec.alpha :as s] [expound.alpha :as expound] - [orchestra.core :refer-macros [defn-spec]])) + [orchestra.core :refer-macros [defn-spec]] + [hickory.render :as hr])) (defn-spec print-debug string? [sth string?] @@ -64,4 +65,60 @@ (defn set-validated! [] (-> (get-element-by-id "form") (.-classList) - (.add "was-validated"))) \ No newline at end of file + (.add "was-validated"))) + +(defn replace-element-content + [name + content] + (-> (get-element-by-id name) + (.-innerHTML) + (set! (hr/hickory-to-html content)))) + +(defn generate-feedback-tag + [id] + {:type :element :attrs {:class "invalid-feedback"} :tag :div :content [{:type :element :attrs {:id (str id "-validation")} :tag :pre :content nil}]}) + +(defn generate-label + [id-for + label] + {:type :element :attrs {:for id-for :class "form-label"} :tag :label :content [label]}) + +(defn generate-input-field + [id + label + default-value] + (str (generate-label id label) + {:type :element :attrs {:class "form-control" :type "text" :name id :value default-value} :tag :input :content nil} + (generate-feedback-tag id))) + +(defn generate-text-area + [id + label + default-value + rows] + (str (generate-label id label) + {:type :element :attrs {:name id :id id :class "form-control" :rows rows} :tag :textarea :content [default-value]} + (generate-feedback-tag id))) + +(defn generate-button + [id + label] + {:type :element + :attrs {:type "button", :id id, :class "btn btn-primary"} + :tag :button + :content [label]}) + +(defn generate-br + [] + {:type :element, :attrs nil, :tag :br, :content nil}) + +(defn generate-output + [id + label + rows] + {:type :element, :attrs {:id id}, :tag :div, :content [{:type :element, :attrs {:for "output", :class "form-label"}, :tag :label, :content [label]} + {:type :element, :attrs {:name "output", :id "output", :class "form-control", :rows rows}, :tag :textarea, :content []}]}) + +(defn generate-needs-validation + [] + {:type :element, :attrs {:class "needs-validation", :id "form"}, :tag :form, :content []}) \ No newline at end of file