support optional fields

This commit is contained in:
Alexander Foremny
2024-06-06 22:42:44 +02:00
parent 612da78d17
commit b1a4822d59
4 changed files with 37 additions and 10 deletions

View File

@@ -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
}