Skip to content

Commit 105c377

Browse files
committed
navbar item, custom html support
1 parent 5c5e6ce commit 105c377

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

‎project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject org.clojars.intception/om-widgets "0.3.34"
1+
(defproject org.clojars.intception/om-widgets "0.3.35"
22
:description "Widgets for OM/React"
33
:url "https://github.com/orgs/intception/"
44
:license {:name "Eclipse Public License"

‎src/examples/basic/state_example.cljs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
(ns examples.basic.state-example)
2-
1+
(ns examples.basic.state-example
2+
(:require [sablono.core :as html :refer-macros [html]]))
33

44
(def app-state
55
(atom
@@ -55,7 +55,17 @@
5555
:badge 6
5656
:text "Inbox"}]}]
5757
[{:text "Editable list"
58-
:id :editable-list}]]
58+
:id :editable-list}]
59+
[{:text (fn [] (html [:label "Current user"]))
60+
:className "profile-user"
61+
:id :profile
62+
:right-position true
63+
:items [{:id :profile
64+
:type :entry
65+
:text "Profile"}
66+
{:id :logout
67+
:type :entry
68+
:text "Logout"}]}]]
5969
:datepicker {:inline #inst "1991-01-25"
6070
:input-group-left #inst "1991-01-25"
6171
:input-group-right #inst "1991-01-25"

‎src/om_widgets/dropdown.cljs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
(ns om-widgets.dropdown
2-
(:require-macros [cljs.core.async.macros :refer [go go-loop]])
2+
(:require-macros [cljs.core.async.macros :refer [go go-loop]]
3+
[pallet.thread-expr :as th])
34
(:require [schema.core :as s :include-macros true]
45
[om.core :as om :include-macros true]
56
[om-widgets.utils :as u]
67
[cljs.core.async :refer [put! chan <! alts! timeout close!]]
7-
[sablono.core :as html :refer-macros [html]]))
8+
[sablono.core :as html :refer-macros [html]]
9+
[pallet.thread-expr :as th]))
810

911

1012
;; TODO support headings: <li class="dropdown-header">Nav header</li>
@@ -51,10 +53,11 @@
5153
;; Dropdown containers
5254

5355
(defn- build-dropdown-js-options
54-
[{:keys [size opened channel] :as state}]
56+
[{:keys [size opened channel className] :as state}]
5557
{:class ["om-widgets-dropdown dropdown btn-group"
5658
(str "btn-group-" (name (or size :md)))
57-
(when opened " open")]
59+
(when opened " open")
60+
(when className className)]
5861
;; tab index is set to 0 to force focus on the container,
5962
;; this way, the onBlur event will be called when the user
6063
;; clicks outside and we can close the dropdown.
@@ -111,11 +114,16 @@
111114
(render-state [_ {:keys [title items icon badge channel] :as state}]
112115
(html
113116
[:li (build-dropdown-js-options state)
114-
[:a {:class "dropdown-toggle" :title title}
117+
[:a (-> {:class "dropdown-toggle"}
118+
(th/when-> (string? title)
119+
(merge {:title title})))
115120
(when icon [:span {:class (u/glyph icon)}])
116-
[:span title]
117-
(when badge [:span.badge badge])
118-
[:span {:class "caret"}]]
121+
(if (fn? title)
122+
(title)
123+
[:div
124+
[:span title]
125+
[:span {:class "caret"}]])
126+
(when badge [:span.badge badge])]
119127
(om/build dropdown-menu cursor {:state {:channel channel
120128
:items items}})]))))
121129

@@ -143,7 +151,9 @@
143151
[:button {:class "btn btn-default dropdown-toggle"
144152
:type "button"}
145153
(when icon [:span {:class (u/glyph icon)}])
146-
[:span title]
154+
(if (fn? title)
155+
(title)
156+
[:span title])
147157
(when badge [:span.badge badge])
148158
[:span {:class "caret"}]])
149159
(om/build dropdown-menu cursor {:state {:channel channel
@@ -168,7 +178,7 @@
168178

169179
(def DropdownSchema
170180
{:items [(s/either EntrySchema DividerSchema)]
171-
:title s/Str
181+
:title (s/either s/Str (s/pred fn?))
172182
(s/optional-key :id) (s/either s/Keyword s/Str s/Int)
173183
(s/optional-key :korks) (s/either s/Any [s/Any])
174184
(s/optional-key :icon) s/Any

‎src/om_widgets/navbar.cljs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
om/IRenderState
2929
(render-state [_ {:keys [channel on-selection active-path]}]
3030
(html
31-
[:li {:class (when (= (get-in cursor [active-path])
32-
(:id entry))
33-
"active")}
31+
[:li {:class [(when (= (get-in cursor [active-path])
32+
(:id entry))
33+
"active")]}
3434

3535
(if (:items entry)
3636
(dropdown cursor (->> {:id (:id entry)
@@ -41,23 +41,27 @@
4141
(on-selection v)
4242
(when (om/get-state owner :collapsed?)
4343
(put! channel :toggle-menu)))
44-
:items (:items entry)}))
44+
:items (:items entry)}
45+
(th/when->> (:className entry)
46+
(merge {:className (:className entry)}))))
4547
[:a (->> {}
4648
(th/when->> (:className entry)
47-
(merge {:className (:className entry)}))
49+
(merge {:className (:className entry)}))
4850
(th/when->> (:url entry)
49-
(merge {:href (:url entry)}))
51+
(merge {:href (:url entry)}))
5052
(th/when->> on-selection
51-
(merge {:onClick (fn [e]
52-
(on-selection (:id @entry))
53-
(when (om/get-state owner :collapsed?)
54-
(put! channel :toggle-menu)))})))
53+
(merge {:onClick (fn [e]
54+
(on-selection (:id @entry))
55+
(when (om/get-state owner :collapsed?)
56+
(put! channel :toggle-menu)))})))
5557

5658
(when (or (:iconClassName entry) (:icon entry))
5759
[:span {:class (or (:iconClassName entry)
5860
(u/glyph (:icon entry)))}])
5961

60-
[:span (:text entry)]
62+
(if (fn? (:text entry))
63+
((:text entry))
64+
[:span (:text entry)])
6165

6266
(when (:badge entry)
6367
[:span.badge (:badge entry)])])]))))
@@ -166,7 +170,7 @@
166170
;; Schema
167171

168172
(def NavbarEntry
169-
{:text s/Str
173+
{:text (s/either s/Str (s/pred fn?))
170174
:id s/Keyword
171175
(s/optional-key :right-position) s/Bool
172176
(s/optional-key :url) s/Str
@@ -175,9 +179,10 @@
175179
(s/optional-key :icon) (s/either s/Keyword s/Str)})
176180

177181
(def NavbarDropdownEntry
178-
{:text s/Str
182+
{:text (s/either s/Str (s/pred fn?))
179183
:id s/Keyword
180184
:items [(s/either EntrySchema DividerSchema)]
185+
(s/optional-key :className) s/Str
181186
(s/optional-key :icon) (s/either s/Keyword s/Str)
182187
(s/optional-key :iconClassName) s/Str
183188
(s/optional-key :badge) (s/either s/Int s/Str)

0 commit comments

Comments
 (0)