Skip to content

Commit cde85bd

Browse files
committed
add switcher component
1 parent 63ddb9f commit cde85bd

File tree

6 files changed

+78
-14
lines changed

6 files changed

+78
-14
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.22"
1+
(defproject org.clojars.intception/om-widgets "0.3.23"
22
:description "Widgets for OM/React"
33
:url "https://github.com/orgs/intception/"
44
:license {:name "Eclipse Public License"

‎resources/public/examples/basic/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
<meta name="theme-color" content="#ffffff">
2222

2323
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
24-
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css" rel="stylesheet">
2524
<link href="css/datepicker.css" rel="stylesheet">
2625
<link href="css/om-widgets-base.css" rel="stylesheet">
2726

‎src/examples/basic/core.cljs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
(ns examples.basic.core
22
(:require [om.core :as om :include-macros true]
3-
[om.dom :as dom :include-macros true]
4-
[om-widgets.layouts :as layout :include-macros true]
53
[om-widgets.core :as w]
6-
[om-widgets.navbar :as navbar]
7-
[sablono.core :as html :refer-macros [html]]
8-
[om-widgets.grid :refer [row-builder]]
4+
[sablono.core :refer-macros [html]]
95
[examples.basic.state-example :as state]
106
[examples.basic.tab-example :refer [tab-example]]
117
[examples.basic.form-example :refer [form-example]]

‎src/examples/basic/form_example.cljs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
om/IRenderState
1515
(render-state [this state]
1616
(html
17-
[:div.panel.panel-default
18-
[:div.panel-heading "Form"]
19-
[:div.panel-body
20-
[:form
17+
[:div.panel.sdsdpanel-default
18+
[:div.sdpanel-body.row
19+
[:div.form-group.col-lg-3]
20+
[:form.col-lg-6
21+
2122
[:div.form-group
2223
[:label "Name"]
2324
(w/textinput app :name {:input-class "form-control"
@@ -110,6 +111,12 @@
110111
:checked-value true
111112
:unchecked-value false})]
112113

114+
[:div.form-group
115+
[:label "Switcher"]
116+
[:br]
117+
(w/switcher app :some-switch {:options [{:value 1 :text "1"}
118+
{:value 2 :text "2"}
119+
{:value 3 :text "3"}]})]
113120

114121
[:div.form-group
115122
[:label "Checks - Toggle values on Sets"]

‎src/om_widgets/core.cljs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
(ns om-widgets.core
2-
(:require [om.core :as om :include-macros true]
3-
[om.dom :as dom :include-macros true]
4-
[om-widgets.textinput]
2+
(:require [om-widgets.textinput]
53
[om-widgets.checkbox]
64
[om-widgets.dropdown]
75
[om-widgets.navbar]
@@ -15,6 +13,7 @@
1513
[om-widgets.page-switcher]
1614
[om-widgets.popover]
1715
[om-widgets.slider]
16+
[om-widgets.switcher]
1817
[om-widgets.editable-list-view]
1918
[om-widgets.utils]))
2019

@@ -25,6 +24,7 @@
2524
(def datepicker om-widgets.datepicker/datepicker)
2625
(def radiobutton om-widgets.radiobutton/radiobutton)
2726
(def radiobutton-group om-widgets.radiobutton/radiobutton-group)
27+
(def switcher om-widgets.switcher/switch)
2828
(def combobox om-widgets.combobox/combobox)
2929
(def button om-widgets.button/button)
3030
(def grid om-widgets.grid/grid)

‎src/om_widgets/switcher.cljs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
(ns om-widgets.switcher
2+
(:require [om.core :as om :include-macros true]
3+
[om.dom :as dom :include-macros true]
4+
[schema.core :as s :include-macros true]
5+
[sablono.core :as html :refer-macros [html]]
6+
[cljs.core.async :refer [put! chan <! alts! timeout close!]]
7+
[om-widgets.utils :as utils]))
8+
9+
10+
(defn- build-label-class [cursor cursor-path value]
11+
(str "btn btn-default" (when (= (get-in cursor [cursor-path]) value) " active")))
12+
13+
(defn item
14+
[cursor owner]
15+
(reify
16+
om/IRenderState
17+
(render-state
18+
[this {:keys [value text cursor-path channel] :as state}]
19+
(html
20+
[:label {:class (build-label-class cursor cursor-path value)}
21+
[:input {:type "radio"
22+
:key text
23+
:onChange (fn [e]
24+
(when channel
25+
(put! channel value))
26+
(om/update! cursor cursor-path value))}
27+
text]]))))
28+
29+
(defn switcher
30+
[cursor owner]
31+
(reify
32+
om/IRenderState
33+
(render-state
34+
[this {:keys [options cursor-path channel classes] :as state}]
35+
(html
36+
(utils/make-childs [:div {:class ["btn-group" (or classes "")]
37+
:data-toggle "buttons"}]
38+
(map #(om/build item
39+
cursor
40+
{:state {:value (:value %)
41+
:text (:text %)
42+
:channel channel
43+
:cursor-path cursor-path}})
44+
options))))))
45+
46+
;; ---------------------------------------------------------------------
47+
;; Schema
48+
49+
(def OptionsSchema
50+
[{:value s/Any
51+
:text s/Str}])
52+
53+
;; ---------------------------------------------------------------------
54+
;; Public
55+
56+
(defn switch
57+
[cursor cursor-path {:keys [options channel classes]}]
58+
(s/validate OptionsSchema options)
59+
(om/build switcher cursor {:state {:cursor-path cursor-path
60+
:options options
61+
:classes classes
62+
:channel channel}}))

0 commit comments

Comments
 (0)