aboutsummaryrefslogtreecommitdiff
path: root/client/src/Component/Input.hs
blob: c3864b4810b23df6b1aaaf9744274a0f39ae9b59 (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
{-# LANGUAGE OverloadedStrings #-}

module Component.Input
  ( InputIn(..)
  , InputOut(..)
  , input
  ) where

import           Data.Text  (Text)
import           Reflex.Dom (Dynamic, Event, MonadWidget, (&), (.~), (=:))
import qualified Reflex.Dom as R

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

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 = do
  let placeHolder = R.constDyn ("placeHolder" =: _inputIn_placeHolder inputIn)
  let value = fmap (const "") (_inputIn_reset inputIn)
  textInput <- R.textInput $ R.def & R.attributes .~ placeHolder
                                   & R.setValue .~ value
  let enter = fmap (const ()) $ R.ffilter ((==) 13) . R._textInput_keypress $ textInput
  return $ InputOut
    { _inputOut_value = R._textInput_value textInput
    , _inputOut_enter = enter
    }