aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/app/Form/Input.hs
blob: 76193266f646be36ead9478a256490af043da722 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module Form.Input
  ( inputText,
    inputNumber,
  )
where

import Form.Internal
import Miso
import Miso.String (MisoString, fromMisoString, null, strip, toMisoString)

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

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