{-# LANGUAGE
OverloadedStrings
, ScopedTypeVariables
, TypeFamilies
#-}
module Text.Reform.Lucid.Common where
import Data.Monoid (mconcat, mempty, (<>))
import Lucid
import Data.Text (Text)
import Text.Reform.Backend
import Text.Reform.Core
import Text.Reform.Generalized as G
import Text.Reform.Result (FormId, Result(Ok), unitRange)
import Web.PathPieces
import Data.Foldable (traverse_)
import qualified Text.Read
import qualified Data.Text as T
instance PathPiece FormId where
toPathPiece fid = T.pack (show fid)
fromPathPiece fidT = Nothing
inputText :: (Monad m, FormError error, PathPiece text, Applicative f) =>
(input -> Either error text)
-> text
-> Form m input error (HtmlT f ()) () text
inputText getInput initialValue = G.input getInput inputField initialValue
where
inputField i a = input_ [type_ "text", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)]
inputPassword :: (Monad m, FormError error, PathPiece text, Applicative f) =>
(input -> Either error text)
-> text
-> Form m input error (HtmlT f ()) () text
inputPassword getInput initialValue = G.input getInput inputField initialValue
where
inputField i a = input_ [type_ "password", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)]
inputSubmit :: (Monad m, FormError error, PathPiece text, Applicative f) =>
(input -> Either error text)
-> text
-> Form m input error (HtmlT f ()) () (Maybe text)
inputSubmit getInput initialValue = G.inputMaybe getInput inputField initialValue
where
inputField i a = input_ [type_ "submit", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)]
inputReset :: (Monad m, FormError error, PathPiece text, Applicative f) =>
text
-> Form m input error (HtmlT f ()) () ()
inputReset lbl = G.inputNoData inputField lbl
where
inputField i a = input_ [type_ "submit", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)]
inputHidden :: (Monad m, FormError error, PathPiece text, Applicative f) =>
(input -> Either error text)
-> text
-> Form m input error (HtmlT f ()) () text
inputHidden getInput initialValue = G.input getInput inputField initialValue
where
inputField i a = input_ [type_ "hidden", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)]
inputButton :: (Monad m, FormError error, PathPiece text, Applicative f) =>
text
-> Form m input error (HtmlT f ()) () ()
inputButton label = G.inputNoData inputField label
where
inputField i a = input_ [type_ "button", id_ (toPathPiece i), name_ (toPathPiece i), value_ (toPathPiece a)]
textarea :: (Monad m, FormError error, ToHtml text, Monad f) =>
(input -> Either error text)
-> Int -- ^ cols
-> Int -- ^ rows
-> text -- ^ initial text
-> Form m input error (HtmlT f ()) () text
textarea getInput cols rows initialValue = G.input getInput textareaView initialValue
where
textareaView i txt =
textarea_ [ rows_ (toPathPiece rows)
, cols_ (toPathPiece cols)
, id_ (toPathPiece i)
, name_ (toPathPiece i)
] $ toHtml txt
-- | Create an @\@ element
--
-- This control may succeed even if the user does not actually select a file to upload. In that case the uploaded name will likely be \"\" and the file contents will be empty as well.
inputFile :: (Monad m, FormError error, FormInput input, ErrorInputType error ~ input, Applicative f) =>
Form m input error (HtmlT f ()) () (FileType input)
inputFile = G.inputFile fileView
where
fileView i = input_ [type_ "file", id_ (toPathPiece i), name_ (toPathPiece i)]
-- | Create a @\