-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.cljs
36 lines (31 loc) · 1.09 KB
/
components.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
(ns snow.ui.components
(:require [reagent.core :as r]
[re-frame.core :as rf]))
(defn get-value [e]
(-> e .-target .-value))
(defn input [{:keys [state placeholder type class]}]
[:div.field>div.control [:input.input
{:placeholder placeholder
:class class
:value @state
:type (or type "text")
:on-change #(reset! state (-> % get-value))}]])
(rf/reg-sub
::input
(fn [db [_ k]]
(if (vector? k)
(get-in db k)
(get db k))))
(rf/reg-event-db
::set-input
(fn [db [_ k v]]
(if (vector? k)
(assoc-in db k v)
(assoc db k v))))
(defn rx-input [{:keys [db-key placeholder type class]}]
[:div.field>div.control [:input.input
{:placeholder placeholder
:class class
:value @(rf/subscribe [::input db-key])
:type (or type "text")
:on-change #(rf/dispatch [::set-input db-key (-> % get-value)])}]])