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
|
module Component.Button
( In(..)
, Out(..)
, view
, defaultIn
) where
import qualified Data.Map as M
import Data.Maybe (catMaybes)
import Data.Text (Text)
import qualified Data.Text as T
import Reflex.Dom (Dynamic, Event, MonadWidget)
import qualified Reflex.Dom as R
import qualified View.Icon as Icon
data In t m = In
{ _in_class :: Dynamic t Text
, _in_content :: m ()
, _in_waiting :: Event t Bool
, _in_tabIndex :: Maybe Int
, _in_submit :: Bool
}
defaultIn :: forall t m. MonadWidget t m => m () -> In t m
defaultIn content = In
{ _in_class = R.constDyn ""
, _in_content = content
, _in_waiting = R.never
, _in_tabIndex = Nothing
, _in_submit = False
}
data Out t = Out
{ _out_clic :: Event t ()
}
view :: forall t m. MonadWidget t m => In t m -> m (Out t)
view input = do
dynWaiting <- R.holdDyn False $ _in_waiting input
let attr = do
buttonClass <- _in_class input
waiting <- dynWaiting
return . M.fromList . catMaybes $
[ Just ("type", if _in_submit input then "submit" else "button")
, (\i -> ("tabindex", T.pack . show $ i)) <$> _in_tabIndex input
, Just ("class", T.intercalate " " [ buttonClass, if waiting then "waiting" else "" ])
]
(e, _) <- R.elDynAttr' "button" attr $ do
Icon.loading
R.divClass "content" $ _in_content input
return $ Out
{ _out_clic = R.domEvent R.Click e
}
|