aboutsummaryrefslogtreecommitdiff
path: root/client/src/Component/Input.hs
blob: 92f8ec939dff5fef69768ad257fd0621c618077d (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
58
59
60
61
62
63
64
65
66
67
68
69
module Component.Input
  ( InputIn(..)
  , InputOut(..)
  , input
  , defaultInputIn
  ) where

import qualified Data.Map         as M
import           Data.Text        (Text)
import qualified Data.Text        as T
import           Reflex.Dom       (Dynamic, Event, MonadWidget, Reflex, (&),
                                   (.~))
import qualified Reflex.Dom       as R

import           Component.Button (ButtonIn (..), ButtonOut (..))
import qualified Component.Button as Button
import qualified Icon

data InputIn t a b = InputIn
  { _inputIn_reset        :: Event t a
  , _inputIn_label        :: Text
  , _inputIn_initialValue :: Text
  }

defaultInputIn :: (Reflex t) => InputIn t a b
defaultInputIn = InputIn
  { _inputIn_reset        = R.never
  , _inputIn_label        = ""
  , _inputIn_initialValue = ""
  }

data InputOut t = InputOut
  { _inputOut_value :: Dynamic t Text
  , _inputOut_enter :: Event t ()
  }

input :: forall t m a b. MonadWidget t m => InputIn t a b -> m (InputOut t)
input inputIn =
  R.divClass "textInput" $ do
    rec
      let resetValue = R.leftmost
            [ fmap (const "") (_inputIn_reset inputIn)
            , fmap (const "") (_buttonOut_clic reset)
            ]

          attributes = R.ffor value (\v ->
            if T.null v then M.empty else M.singleton "class" "filled")

          value =  R._textInput_value textInput

      textInput <- R.textInput $ R.def
        & R.attributes .~ attributes
        & R.setValue .~ resetValue
        & R.textInputConfig_initialValue .~ (_inputIn_initialValue inputIn)

      R.el "label" $ R.text (_inputIn_label inputIn)

      reset <- Button.button $
        (Button.defaultButtonIn Icon.cross)
          { _buttonIn_class   = R.constDyn "reset"
          , _buttonIn_tabIndex = Just (-1)
          }

    let enter = fmap (const ()) $ R.ffilter ((==) 13) . R._textInput_keypress $ textInput

    return $ InputOut
      { _inputOut_value = value
      , _inputOut_enter = enter
      }