module Form.Input ( inputText, inputNumber, ) where import Form.Internal import Miso import Miso.String (MisoString, fromMisoString, null, strip, toMisoString) inputNumber :: MisoString -> Form MisoString Double inputNumber label = let parse :: MisoString -> Either MisoString Double parse i = let i' = strip i in if Miso.String.null i' then Left "required" else Right (read (fromMisoString i')) in Form { view = \i -> [ div_ [] $ [ label_ [] $ [ text label, div_ [] $ [ input_ [ type_ "number", value_ (toMisoString (show i)), onInput id ], div_ [] $ [either text (\_ -> text "") (parse i)] ] ] ] ], fill = parse } inputText :: MisoString -> Form MisoString MisoString inputText label = let parse :: MisoString -> Either MisoString MisoString parse i = let i' = strip i in if Miso.String.null i' then Left "required" else Right i' in Form { view = \i -> [ div_ [] $ [ label_ [] $ [ text label, div_ [] $ [ input_ [ type_ "text", value_ i, onInput id ], div_ [] $ [either text (\_ -> text "") (parse i)] ] ] ] ], fill = parse }