aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Form/Input.hs
blob: e43651c70f05025af8fca454ee5cf637e8160f8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Form.Input
  ( input,
  )
where

import Data.Text qualified as T
import Form.Internal
import Miso
import Miso.String (fromMisoString, toMisoString)

input :: String -> Form T.Text T.Text
input label =
  let parse :: T.Text -> Either String T.Text
      parse i =
        let i' = T.strip i
         in if T.null i' then Left "required" else Right i'
   in Form
        { view = \i ->
            [ div_ [] $
                [ label_ [] $
                    [ text (toMisoString label),
                      div_ [] $
                        [ input_
                            [ type_ "text",
                              value_ (toMisoString i),
                              onInput fromMisoString
                            ],
                          div_ [] $
                            [either (text . toMisoString) (\_ -> text "") (parse i)]
                        ]
                    ]
                ]
            ],
          fill = parse
        }