module Dingo.Widget.Input ( Input
, mkInput
, getValue
, onChange
, setValue
) where
import Control.Monad (mzero)
import Data.Aeson.Types
import Data.Text (Text)
import Data.Typeable (Typeable)
import Dingo.Callback
import Dingo.Event
import Dingo.Widget
import Text.Blaze ((!), toValue)
import Text.Julius (julius)
import qualified Text.Blaze.Html4.Strict as H
import qualified Text.Blaze.Html4.Strict.Attributes as A
data Input = Input { inputId :: WidgetId
}
deriving (Show, Typeable)
data InputState = InputState { inputValue :: Text }
deriving (Show, Typeable)
instance ToJSON InputState where
toJSON (InputState v) = String v
instance FromJSON InputState where
parseJSON (String s) = return $ InputState s
parseJSON _ = mzero
instance Widget Input InputState where
getWidgetId = inputId
renderWidget w =
H.input ! A.id (toValue $ inputId w)
showWidget w s =
show w ++ "->" ++ show s
encodeClientStateJs _ =
[julius| function() { return $(this).val(); } |]
decodeClientStateJs _ =
[julius| function(s) { $(this).val(s); } |]
mkInput :: Widget w s => w -> CallbackM Input
mkInput = flip addWidget (\i -> return (Input i, InputState ""))
getValue :: Input -> CallbackM Text
getValue w = do
ms <- getWidgetState w
case ms of
Nothing -> return ""
Just s -> return $ inputValue s
setValue :: Input -> Text -> CallbackM ()
setValue w v = setWidgetState w (InputState v)
onChange :: Input -> CallbackM () -> CallbackM ()
onChange i = onEvent (widgetSelector i) OnChange