aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Form
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/app/Form')
-rw-r--r--frontend/app/Form/Input.hs6
-rw-r--r--frontend/app/Form/Internal.hs15
2 files changed, 18 insertions, 3 deletions
diff --git a/frontend/app/Form/Input.hs b/frontend/app/Form/Input.hs
index 4b1eac8..80044ec 100644
--- a/frontend/app/Form/Input.hs
+++ b/frontend/app/Form/Input.hs
@@ -1,5 +1,5 @@
module Form.Input
- ( string,
+ ( input,
)
where
@@ -8,8 +8,8 @@ import Form.Internal
import Miso
import Miso.String (fromMisoString, toMisoString)
-string :: String -> Form T.Text T.Text
-string label =
+input :: String -> Form T.Text T.Text
+input label =
Form
{ view = \i ->
[ div_ [] $
diff --git a/frontend/app/Form/Internal.hs b/frontend/app/Form/Internal.hs
index 8c9935f..2274c63 100644
--- a/frontend/app/Form/Internal.hs
+++ b/frontend/app/Form/Internal.hs
@@ -2,9 +2,11 @@ module Form.Internal
( Form (..),
mapValues,
runForm,
+ optional,
)
where
+import Data.Text qualified as T
import Miso
data Form i o = Form
@@ -57,3 +59,16 @@ runForm form i =
form_ [onSubmit (either (\_ -> Left i) (Right) (form.fill i))] $
(fmap Left <$> form.view i)
<> [button_ [type_ "submit"] [text "submit"]]
+
+class IsEmpty i where
+ isEmpty :: i -> Bool
+
+instance IsEmpty T.Text where
+ isEmpty = T.null . T.strip
+
+optional :: (IsEmpty i) => Form i o -> Form i (Maybe o)
+optional form =
+ Form
+ { view = \i -> form.view i,
+ fill = \i -> if isEmpty i then Right Nothing else Just <$> form.fill i
+ }