aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Form/Internal.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2024-06-06 22:42:44 +0200
committerLibravatar Alexander Foremny <aforemny@posteo.de>2024-06-06 22:42:44 +0200
commitb1a4822d5954fe02b82f2c525403c74b3920befe (patch)
tree6a284f7ec64dd0bb3bc7ff3e1812139b3a2f3932 /frontend/app/Form/Internal.hs
parent612da78d17c575cd5ade1de62dc1a3c514129de0 (diff)
support optional fields
Diffstat (limited to 'frontend/app/Form/Internal.hs')
-rw-r--r--frontend/app/Form/Internal.hs15
1 files changed, 15 insertions, 0 deletions
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
+ }