Support for select with multiple flag

This commit is contained in:
Tomas Drencak
2023-07-21 11:57:57 +02:00
committed by Nikita Prokopov
parent 72c9535a29
commit 1e1fb15c65
2 changed files with 50 additions and 2 deletions

View File

@ -9,6 +9,7 @@
(= :rum/nothing (first element))))
(def ^:dynamic *select-value*)
(def ^:dynamic *multiple?*)
(defn append!
([^StringBuilder sb s0] (.append sb s0))
@ -390,7 +391,15 @@
(append! sb " type=\"" (escape-html (to-str type)) "\""))
(when (and (= "option" tag)
(= (get-value attrs) *select-value*))
(or (= (get-value attrs) *select-value*)
(and *multiple?*
(set? *select-value*)
(contains? *select-value* (get-value attrs)))
(and *multiple?*
(sequential? *select-value*)
(->> *select-value*
(filter #(= (get-value attrs) %))
(seq)))))
(append! sb " selected=\"\""))
(when id
@ -407,7 +416,8 @@
(vreset! *state :state/tag-open))
(if (= "select" tag)
(binding [*select-value* (get-value attrs)]
(binding [*select-value* (get-value attrs)
*multiple?* (:multiple attrs)]
(render-content! tag attrs children *state sb))
(render-content! tag attrs children *state sb)))))))

View File

@ -84,3 +84,41 @@
'([a] [a b] [a b c])))
(is (= (:arglists (meta #'comp-arglists-1))
'([a] [a b] [a b c]))))
(deftest test-select-multiple
(testing "Set value"
(is (= (rum/render-static-markup [:select {:value #{"v1" "v2"}
:multiple true}
[:option {:value "v1"}]
[:option {:value "v2"}]
[:option {:value "v3"}]
])
(str "<select multiple=\"\">"
"<option selected=\"\" value=\"v1\"></option>"
"<option selected=\"\" value=\"v2\"></option>"
"<option value=\"v3\"></option>"
"</select>"))))
(testing "Vector value"
(is (= (rum/render-static-markup [:select {:value ["v1" "v2"]
:multiple true}
[:option {:value "v1"}]
[:option {:value "v2"}]
[:option {:value "v3"}]])
(str "<select multiple=\"\">"
"<option selected=\"\" value=\"v1\"></option>"
"<option selected=\"\" value=\"v2\"></option>"
"<option value=\"v3\"></option>"
"</select>"))))
(testing "List value"
(is (= (rum/render-static-markup [:select {:value (list "v1" "v2")
:multiple true}
[:option {:value "v1"}]
[:option {:value "v2"}]
[:option {:value "v3"}]])
(str "<select multiple=\"\">"
"<option selected=\"\" value=\"v1\"></option>"
"<option selected=\"\" value=\"v2\"></option>"
"<option value=\"v3\"></option>"
"</select>")))))