aboutsummaryrefslogtreecommitdiff
path: root/client/src/Component/Button.hs
blob: 46c0afa0089fa7b7091ec2a67cb8a7b806bd37e2 (plain)
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
  ( ButtonIn(..)
  , ButtonOut(..)
  , button
  , defaultButtonIn
  ) 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 Icon

data ButtonIn t m = ButtonIn
  { _buttonIn_class    :: Dynamic t Text
  , _buttonIn_content  :: m ()
  , _buttonIn_waiting  :: Event t Bool
  , _buttonIn_tabIndex :: Maybe Int
  , _buttonIn_submit   :: Bool
  }

defaultButtonIn :: MonadWidget t m => m () -> ButtonIn t m
defaultButtonIn content = ButtonIn
  { _buttonIn_class    = R.constDyn ""
  , _buttonIn_content  = content
  , _buttonIn_waiting  = R.never
  , _buttonIn_tabIndex = Nothing
  , _buttonIn_submit   = False
  }

data ButtonOut t = ButtonOut
  { _buttonOut_clic :: Event t ()
  }

button :: forall t m. MonadWidget t m => ButtonIn t m -> m (ButtonOut t)
button buttonIn = do
  dynWaiting <- R.holdDyn False $ _buttonIn_waiting buttonIn

  let attr = do
        buttonClass <- _buttonIn_class buttonIn
        waiting <- dynWaiting
        return . M.fromList . catMaybes $
          [ Just ("type", if _buttonIn_submit buttonIn then "submit" else "button")
          , (\i -> ("tabindex", T.pack . show $ i)) <$> _buttonIn_tabIndex buttonIn
          , Just ("class", T.intercalate " " [ buttonClass, if waiting then "waiting" else "" ])
          ]

  (e, _) <- R.elDynAttr' "button" attr $ do
    Icon.loading
    R.divClass "content" $ _buttonIn_content buttonIn

  return $ ButtonOut
    { _buttonOut_clic = R.domEvent R.Click e
    }