Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement multi-select with tests #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/hiccup/form.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
:value value
:checked checked?}]))

(defn- selected? [x selected]
(boolean (cond
(sequential? selected) ((set selected) x)
(ifn? selected) (selected x)
:else (= x selected))))

(defelem select-options
"Creates a seq of option tags from a collection."
([coll] (select-options coll nil))
Expand All @@ -83,8 +89,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
Loading