-
-
Notifications
You must be signed in to change notification settings - Fork 147
/
tabs.cljs
310 lines (291 loc) · 16 KB
/
tabs.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(ns re-com.tabs
(:require-macros
[re-com.core :refer [handler-fn at]]
[re-com.validate :refer [validate-args-macro]])
(:require
[re-com.config :refer [include-args-desc?]]
[re-com.debug :refer [->attr]]
[re-com.util :refer [deref-or-value]]
[re-com.box :refer [flex-child-style]]
[re-com.validate :refer [css-style? html-attr? parts? vector-of-maps?
position? position-options-list]]
[re-com.popover :refer [popover-tooltip]]
[reagent.core :as reagent]))
;;--------------------------------------------------------------------------------------------------
;; Component: horizontal-tabs
;;--------------------------------------------------------------------------------------------------
(def horizontal-tabs-parts-desc
(when include-args-desc?
[{:name :wrapper :level 0 :class "rc-tabs" :impl "[:ul]"
:notes [:span "Only " [:code ":style"] " is supported in " [:code ":parts :wrapper"] ". Otherwise, use top level " [:code ":class"] " and " [:code ":attr"] " arguments."]}
{:name :tab :level 1 :class "rc-tab" :impl "[:li]"}
{:name :anchor :level 2 :class "rc-tab-anchor" :impl "[:a]"
:notes [:span "Only " [:code ":class"] " and " [:code ":attr"] " are supported in " [:code ":parts :anchor"] ". Otherwise, use top level " [:code ":style"] " argument."]}]))
(def horizontal-tabs-parts
(when include-args-desc?
(-> (map :name horizontal-tabs-parts-desc) set)))
(def horizontal-tabs-args-desc
(when include-args-desc?
[{:name :model :required true :type "unique-id | r/atom" :description "the unique identifier of the currently selected tab"}
{:name :disabled? :required false :type "boolean | r/atom" :description "disables all tabs."}
{:name :tabs :required true :type "vector of tabs | r/atom" :validate-fn vector-of-maps? :description "one element in the vector for each tab. Typically, each element is a map with :id and :label keys"}
{:name :on-change :required true :type "unique-id -> nil" :validate-fn fn? :description "called when user alters the selection. Passed the unique identifier of the selection"}
{:name :id-fn :required false :default :id :type "tab -> anything" :validate-fn ifn? :description [:span "given an element of " [:code ":tabs"] ", returns its unique identifier (aka id)"]}
{:name :label-fn :required false :default :label :type "tab -> string | hiccup" :validate-fn ifn? :description [:span "given an element of " [:code ":tabs"] ", returns its displayable label"]}
{:name :class :required false :type "string" :validate-fn string? :description "CSS class names, space separated (applies to the outer container)"}
{:name :style :required false :type "CSS style map" :validate-fn css-style? :description [:span "CSS styles to add or override (aplies to " [:span.bold "each individual tab"] " rather than the container)"]}
{:name :attr :required false :type "HTML attr map" :validate-fn html-attr? :description [:span "HTML attributes, like " [:code ":on-mouse-move"] [:br] "No " [:code ":class"] " or " [:code ":style"] "allowed (applies to the outer container)"]}
{:name :parts :required false :type "map" :validate-fn (parts? horizontal-tabs-parts) :description "See Parts section below."}
{:name :src :required false :type "map" :validate-fn map? :description [:span "Used in dev builds to assist with debugging. Source code coordinates map containing keys" [:code ":file"] "and" [:code ":line"] ". See 'Debugging'."]}
{:name :debug-as :required false :type "map" :validate-fn map? :description [:span "Used in dev builds to assist with debugging, when one component is used implement another component, and we want the implementation component to masquerade as the original component in debug output, such as component stacks. A map optionally containing keys" [:code ":component"] "and" [:code ":args"] "."]}]))
(defn horizontal-tabs
[& {:keys [model tabs on-change id-fn label-fn class style attr parts disabled?]
:or {id-fn :id label-fn :label}
:as args}]
(or
(validate-args-macro horizontal-tabs-args-desc args)
(let [current (deref-or-value model)
tabs (deref-or-value tabs)
disabled? (deref-or-value disabled?)
_ (assert (not-empty (filter #(= current (id-fn %)) tabs)) "model not found in tabs vector")]
[:ul
(merge {:class (str "nav nav-tabs noselect rc-tabs " class)
:style (merge (flex-child-style "none")
(get-in parts [:wrapper :style]))}
(->attr args)
attr)
(for [t tabs]
(let [id (id-fn t)
label (label-fn t)
disabled? (or disabled? (:disabled? t))
selected? (= id current)] ;; must use current instead of @model to avoid reagent warnings
[:li
(merge
{:class (str (when disabled? "disabled ")
(when selected? "active rc-tab ")
(get-in parts [:tab :class]))
:style (get-in parts [:tab :style])
:key (str id)}
(get-in parts [:tab :attr]))
[:a
(merge
{:class (str "rc-tab-anchor " (get-in parts [:anchor :class]))
:style (merge {:cursor "pointer"}
style)
:on-click (when (and on-change (not selected?) (not disabled?)) (handler-fn (on-change id)))}
(get-in parts [:anchor :attr]))
label]]))])))
;;--------------------------------------------------------------------------------------------------
;; Component: horizontal-bar-tabs
;;--------------------------------------------------------------------------------------------------
(def bar-tabs-parts-desc
(when include-args-desc?
[{:name :wrapper :level 0 :class "rc-tabs" :impl "[:div]"
:notes [:span "Only " [:code ":style"] " is supported in " [:code ":parts :wrapper"] ". Otherwise, use top level " [:code ":class"] " and " [:code ":attr"] " arguments."]}
{:name :tooltip :level 1 :class "rc-tabs-tooltip" :impl "[popover-tooltip]"}
{:name :button :level 2 :class "rc-tabs-btn" :impl "[:button]"
:notes [:span "Only " [:code ":class"] " and " [:code ":attr"] " are supported in " [:code ":parts :anchor"] ". Otherwise, use top level " [:code ":style"] " argument."]}]))
(def bar-tabs-parts
(when include-args-desc?
(-> (map :name horizontal-tabs-parts-desc) set)))
(def bar-tabs-args-desc
(when include-args-desc?
(->
(remove #(= :parts (:name %)) horizontal-tabs-args-desc)
(vec)
(conj
{:name :tooltip-fn :required false :default :tooltip :type "tab -> string | hiccup" :validate-fn ifn? :description [:span "[horizontal-bar-tabs only] given an element of " [:code ":tabs"] ", returns its tooltip"]}
{:name :tooltip-position :required false :default :below-center :type "keyword" :validate-fn position? :description [:span "[horizontal-bar-tabs only] relative to this anchor. One of " position-options-list]}
{:name :validate? :required false :default true :type "boolean" :description [:span "Validate " [:code ":model"] " against " [:code ":tabs"]]}
{:name :parts :required false :type "map" :validate-fn (parts? bar-tabs-parts) :description "See Parts section below."}))))
(defn- bar-tabs
[& {:keys [model tabs on-change id-fn label-fn tooltip-fn tooltip-position vertical? class style attr parts validate? disabled?] :as args}]
(let [showing (reagent/atom nil)]
(fn [& {:keys [model tabs disabled?]}]
(let [current (deref-or-value model)
tabs (deref-or-value tabs)
disabled? (deref-or-value disabled?)
_ (assert (or (not validate?) (not-empty (filter #(= current (id-fn %)) tabs))) "model not found in tabs vector")]
(into [:div
(merge
{:class (str "noselect btn-group" (when vertical? "-vertical") " rc-tabs " class)
:style (merge (flex-child-style "none")
(get-in parts [:wrapper :style]))}
(->attr args)
attr)]
(for [t tabs]
(let [id (id-fn t)
label (label-fn t)
tooltip (when tooltip-fn (tooltip-fn t))
selected? (= id current)
disabled? (or disabled? (:disabled? t))
the-button
[:button
(merge
{:type "button"
:key (str id)
:class (str "btn btn-default "
(when disabled? "disabled ")
(when selected? "active ")
"rc-tabs-btn "
(get-in parts [:button :class]))
:style style
:on-click (when (and on-change (not selected?) (not disabled?))
(handler-fn (println (gensym)) (on-change id)))}
(when tooltip
{:on-mouse-over (handler-fn (reset! showing id))
:on-mouse-out (handler-fn (swap! showing #(when-not (= id %) %)))})
(get-in parts [:button :attr]))
label]]
(if tooltip
[popover-tooltip
:src (at)
:label tooltip
:position (or tooltip-position :below-center)
:showing? (reagent/track #(= id @showing))
:anchor the-button
:class (str "rc-tabs-tooltip " (get-in parts [:tooltip :class]))
:style (get-in parts [:tooltip :style])
:attr (get-in parts [:tooltip :attr])]
the-button))))))))
(defn horizontal-bar-tabs
[& {:keys [model tabs on-change id-fn label-fn tooltip-fn tooltip-position class style attr parts src debug-as validate? disabled?]
:or {id-fn :id label-fn :label tooltip-fn :tooltip}
:as args}]
(or
(validate-args-macro bar-tabs-args-desc args)
(bar-tabs
:model model
:tabs tabs
:disabled? disabled?
:on-change on-change
:id-fn id-fn
:label-fn label-fn
:tooltip-fn tooltip-fn
:tooltip-position tooltip-position
:vertical? false
:class class
:style style
:attr attr
:parts parts
:src src
:debug-as debug-as
:validate? validate?)))
(defn vertical-bar-tabs
[& {:keys [model tabs on-change id-fn label-fn class style attr parts src debug-as validate? disabled?]
:or {id-fn :id label-fn :label}
:as args}]
(or
(validate-args-macro bar-tabs-args-desc args)
(bar-tabs
:model model
:tabs tabs
:disabled? disabled?
:on-change on-change
:id-fn id-fn
:label-fn label-fn
:vertical? true
:class class
:style style
:attr attr
:parts parts
:src src
:debug-as debug-as
:validate? validate?)))
;;--------------------------------------------------------------------------------------------------
;; Component: pill-tabs
;;--------------------------------------------------------------------------------------------------
(def pill-tabs-parts-desc
(when include-args-desc?
[{:name :wrapper :level 0 :class "rc-tabs" :impl "[:ul]"
:notes [:span "Only " [:code ":style"] " is supported in " [:code ":parts :wrapper"] ". Otherwise, use top level " [:code ":class"] " and " [:code ":attr"] " arguments."]}
{:name :tab :level 1 :class "rc-tabs-pill" :impl "[:li]"}
{:name :anchor :level 2 :class "rc-tabs-anchor" :impl "[:a]"
:notes [:span "Only " [:code ":class"] " and " [:code ":attr"] " are supported in " [:code ":parts :anchor"] ". Otherwise, use top level " [:code ":style"] " argument."]}]))
(def pill-tabs-parts
(when include-args-desc?
(-> (map :name horizontal-tabs-parts-desc) set)))
(def pill-tabs-args-desc
(when include-args-desc?
(->
(remove #(= :parts (:name %)) horizontal-tabs-args-desc)
(vec)
(conj
{:name :parts :required false :type "map" :validate-fn (parts? pill-tabs-parts) :description "See Parts section below."}))))
(defn- pill-tabs ;; tabs-like in action
[& {:keys [model tabs on-change id-fn label-fn vertical? class style attr parts src disabled?] :as args}]
(let [current (deref-or-value model)
tabs (deref-or-value tabs)
disabled? (deref-or-value disabled?)
_ (assert (not-empty (filter #(= current (id-fn %)) tabs)) "model not found in tabs vector")]
[:ul
(merge
{:class (str "rc-tabs noselect nav nav-pills" (when vertical? " nav-stacked") " " class)
:style (merge (flex-child-style "none")
(get-in parts [:wrapper :style]))
:role "tabslist"}
(->attr args)
attr)
(for [t tabs]
(let [id (id-fn t)
label (label-fn t)
disabled? (or disabled? (:disabled? t))
selected? (= id current)] ;; must use 'current' instead of @model to avoid reagent warnings
[:li
(merge
{:class (str "rc-tabs-pill "
(when disabled? "disabled ")
(when selected? "active ")
(get-in parts [:tab :class]))
:style (get-in parts [:tab :style])
:key (str id)}
(get-in parts [:tab :attr]))
[:a
(merge
{:class (str "rc-tabs-anchor " (get-in parts [:anchor :class]))
:style (merge {:cursor "pointer"}
style)
:on-click (when (and on-change (not selected?) (not disabled?))
(handler-fn (on-change id)))}
(get-in parts [:anchor :attr]))
label]]))]))
(defn horizontal-pill-tabs
[& {:keys [model tabs on-change id-fn label-fn class style attr parts src debug-as disabled?]
:or {id-fn :id label-fn :label}
:as args}]
(or
(validate-args-macro pill-tabs-args-desc args)
(pill-tabs
:model model
:tabs tabs
:disabled? disabled?
:on-change on-change
:id-fn id-fn
:label-fn label-fn
:vertical? false
:class class
:style style
:attr attr
:parts parts
:src src
:debug-as debug-as)))
(defn vertical-pill-tabs
[& {:keys [model tabs on-change id-fn label-fn class style attr parts src debug-as disabled?]
:or {id-fn :id label-fn :label}
:as args}]
(or
(validate-args-macro pill-tabs-args-desc args)
(pill-tabs
:model model
:tabs tabs
:disabled? disabled?
:on-change on-change
:id-fn id-fn
:label-fn label-fn
:vertical? true
:class class
:style style
:attr attr
:parts parts
:src src
:debug-as debug-as)))