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 = InputIn { _inputIn_hasResetButton :: Bool , _inputIn_label :: Text , _inputIn_initialValue :: Text , _inputIn_inputType :: Text } defaultInputIn :: InputIn defaultInputIn = InputIn { _inputIn_hasResetButton = True , _inputIn_label = "" , _inputIn_initialValue = "" , _inputIn_inputType = "text" } data InputOut t = InputOut { _inputOut_value :: Dynamic t Text , _inputOut_enter :: Event t () } input :: forall t m a b. MonadWidget t m => InputIn -> Event t a -- reset -> m (InputOut t) input inputIn reset = R.divClass "textInput" $ do rec let resetValue = R.leftmost [ fmap (const "") reset , fmap (const "") resetClic ] attributes = R.ffor value (\v -> if T.null v && _inputIn_inputType inputIn /= "date" 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.textInputConfig_inputType .~ (_inputIn_inputType inputIn) R.el "label" $ R.text (_inputIn_label inputIn) resetClic <- if _inputIn_hasResetButton inputIn then _buttonOut_clic <$> (Button.button $ (Button.defaultButtonIn Icon.cross) { _buttonIn_class = R.constDyn "reset" , _buttonIn_tabIndex = Just (-1) }) else return R.never let enter = fmap (const ()) $ R.ffilter ((==) 13) . R._textInput_keypress $ textInput return $ InputOut { _inputOut_value = value , _inputOut_enter = enter }