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

Add the ability to have multiple options marked as selected #152

Open
wants to merge 2 commits 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]
(cond (or (list? selected) (vector? selected))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about:

(or (and (sequential? selected) ((set selected) x))
    (and (ifn? selected) (selected x))
    (= x selected))

(or (some (partial = x) selected) false)
(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
18 changes: 17 additions & 1 deletion test/hiccup/form_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +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>")))



Expand Down