module Form.Input
  ( input,
  )
where

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

input :: MisoString -> Form MisoString MisoString
input 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
        }