module View.Form exposing ( textInput , radioInputs , hiddenSubmit ) import Html exposing (..) import Html.Attributes exposing (..) import Html.Events exposing (..) import FontAwesome import View.Color as Color import Form exposing (Form, FieldState) import Form.Input as Input import Form.Error as FormError exposing (Error(..)) import Form.Field as Field import Msg exposing (Msg) import LoggedData exposing (LoggedData) import Model.Translations as Translations exposing (Translations) import Utils.Maybe exposing (isJust) textInput : Translations -> Form String a -> String -> String -> Html Form.Msg textInput translations form formName fieldName = let field = Form.getFieldAsString fieldName form in div [ classList [ ("textInput", True) , ("error", isJust field.liveError) ] ] [ Input.textInput field [ id (formName ++ fieldName) , classList [ ("filled", isJust field.value) ] ] , label [ for (formName ++ fieldName) ] [ text (Translations.getMessage translations (formName ++ fieldName)) ] , button [ type' "button" , onClick (Form.Input fieldName Field.EmptyField) , tabindex -1 ] [ FontAwesome.times Color.silver 15 ] , case field.liveError of Just error -> formError translations error Nothing -> text "" ] radioInputs : Translations -> Form String a -> String -> String -> List String -> Html Form.Msg radioInputs translations form formName radioName fieldNames = let field = Form.getFieldAsString radioName form in div [ classList [ ("radioGroup", True) , ("error", isJust field.liveError) ] ] [ div [ class "title" ] [ text (Translations.getMessage translations (formName ++ radioName) ) ] , div [ class "radioInputs" ] (List.map (radioInput translations field formName) fieldNames) , case field.liveError of Just error -> formError translations error Nothing -> text "" ] radioInput : Translations -> FieldState String String -> String -> String -> Html Form.Msg radioInput translations field formName fieldName = div [ class "radioInput" ] [ Input.radioInput field.path field [ id (formName ++ fieldName) , value fieldName , checked (field.value == Just fieldName) ] , label [ for (formName ++ fieldName) ] [ text (Translations.getMessage translations (formName ++ fieldName)) ] ] formError : Translations -> FormError.Error String -> Html msg formError translations error = let errorElement error params = div [ class "errorMessage" ] [ text (Translations.getParamMessage params translations error) ] in case error of CustomError key -> errorElement key [] SmallerIntThan n -> errorElement "SmallerIntThan" [toString n] GreaterIntThan n -> errorElement "GreaterIntThan" [toString n] error -> errorElement (toString error) [] hiddenSubmit : msg -> Html msg hiddenSubmit msg = button [ style [ ("display", "none") ] , onClick msg ] []