Skip to content

Commit

Permalink
Implement multi-select with tests
Browse files Browse the repository at this point in the history
Allow multiple selected options inside <select>s with the
`multiple` attribute.  Instead of checking equality using
`=`, a `selected?` function is implemented that checks
whether the current value being rendered is found inside
`selected`, which can be a sequence, a function, or a single
value.
  • Loading branch information
Xuan Wu committed Feb 4, 2024
1 parent 92f18b7 commit eac8da7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/hiccup/form.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@
:value value
:checked checked?}]))

;; According to the HTML spec, the value inside the boolean `selected` attribute
;; can only be "" or "selected" (case insensitive). Hence, the coercion to
;; boolean is required (otherwise, the attribute value will be `x` itself).
;; We also need the `(not (sequential? selected))` requirement in the
;; second `and` because if `selected` is a vector where `x` is not an element,
;; the first `and` will fail, and the second will try to run something
;; like `([a b] c)`, and can easily lead to java.lang.IndexOutOfBoundsException.
(defn- selected? [x selected]
(or
(and (sequential? selected) (boolean ((set selected) x)))
(and (not (sequential? selected)) (ifn? selected) (selected x))
(= x selected)))

(defelem select-options
"Creates a seq of option tags from a collection."
([coll] (select-options coll nil))
Expand All @@ -83,8 +96,8 @@
(let [[text val] x]
(if (sequential? val)
[:optgroup {:label text} (select-options val selected)]
[:option {:value val :selected (= val selected)} text]))
[:option {:selected (= x selected)} x]))))
[:option {:value val :selected (selected? val selected)} text]))
[:option {:selected (selected? x selected)} x]))))

(defelem drop-down
"Creates a drop-down box using the `<select>` tag."
Expand Down
20 changes: 17 additions & 3 deletions test/hiccup/form_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,23 @@
"<option value=\"2\">baz</option></optgroup>")
(select-options [["Foo" [1 2]]] 2)
(str "<optgroup label=\"Foo\"><option>1</option>"
"<option selected=\"selected\">2</option></optgroup>")))


"<option selected=\"selected\">2</option></optgroup>")
(select-options ["foo" "bar" "baz"] ["foo" "bar"])
(str "<option selected=\"selected\">foo</option>"
"<option selected=\"selected\">bar</option>"
"<option>baz</option>")
(select-options ["foo" "bar" "baz"] '("foo" "bar"))
(str "<option selected=\"selected\">foo</option>"
"<option selected=\"selected\">bar</option>"
"<option>baz</option>")
(select-options [["Foo" 1] ["Bar" 2] ["Baz" 3]] [1 3])
(str "<option selected=\"selected\" value=\"1\">Foo</option>"
"<option value=\"2\">Bar</option>"
"<option selected=\"selected\" value=\"3\">Baz</option>")
(select-options ["foo" "bar" "baz"] #(= \b (nth % 0)))
(str "<option>foo</option>"
"<option selected=\"selected\">bar</option>"
"<option selected=\"selected\">baz</option>")))

(deftest test-drop-down
(let [options ["op1" "op2"]
Expand Down

0 comments on commit eac8da7

Please sign in to comment.