From 63f37f4f04b0e935014ea7a55a986b6109d8fd48 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Tue, 2 Oct 2018 10:15:28 +0200 Subject: [PATCH 1/2] add the ability to have multiple options marked as selected This way, multi selectes can be realized where the select tag hast the attribute multiple="multiple" together with size="x" and users can ctrl-click multiple options --- src/hiccup/form.clj | 12 ++++++++++-- test/hiccup/form_test.clj | 10 +++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/hiccup/form.clj b/src/hiccup/form.clj index 7c60fae..1abe445 100644 --- a/src/hiccup/form.clj +++ b/src/hiccup/form.clj @@ -74,6 +74,14 @@ :value value :checked checked?}])) +(defn- selected? + "If selected is sequential, return whether selected contains x, + else return if x is equal to selected" + [x selected] + (if (sequential? selected) + (or (some (partial = x) selected) false) + (= x selected))) + (defelem select-options "Creates a seq of option tags from a collection." ([coll] (select-options coll nil)) @@ -83,8 +91,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 `