mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-26 13:21:42 +03:00
Add time input.
This commit is contained in:
parent
a769a3e569
commit
d5478db009
62
src/Form.elm
62
src/Form.elm
@ -15,6 +15,7 @@ module Form exposing
|
||||
, withMax, withMaxDate, withMin, withMinDate
|
||||
, withStep, withFloatStep
|
||||
, hasErrors2, rawValues, runClientValidations, withClientValidation, withClientValidation2
|
||||
, time
|
||||
)
|
||||
|
||||
{-|
|
||||
@ -1104,6 +1105,67 @@ date name toError toHtmlFn =
|
||||
}
|
||||
|
||||
|
||||
type alias TimeOfDay =
|
||||
{ hours : Int, minutes : Int }
|
||||
|
||||
|
||||
{-| -}
|
||||
time :
|
||||
String
|
||||
-> { invalid : String -> error }
|
||||
->
|
||||
(FieldRenderInfo error
|
||||
-> view
|
||||
)
|
||||
->
|
||||
Field
|
||||
error
|
||||
(Maybe TimeOfDay)
|
||||
view
|
||||
{ --min : Date
|
||||
--, max : Date,
|
||||
required : ()
|
||||
, wasMapped : No
|
||||
}
|
||||
time name toError toHtmlFn =
|
||||
Field
|
||||
{ name = name
|
||||
, initialValue = Nothing
|
||||
, type_ = "date"
|
||||
, required = False
|
||||
, serverValidation = \_ -> DataSource.succeed []
|
||||
, toHtml =
|
||||
\formInfo _ fieldInfo info ->
|
||||
toHtmlFn (toInputRecord formInfo name Nothing info fieldInfo)
|
||||
, decode =
|
||||
\rawString ->
|
||||
(if (rawString |> Maybe.withDefault "") == "" then
|
||||
Ok Nothing
|
||||
|
||||
else
|
||||
rawString
|
||||
|> Maybe.withDefault ""
|
||||
|> parseTimeOfDay
|
||||
|> Result.mapError (\_ -> toError.invalid (rawString |> Maybe.withDefault ""))
|
||||
|> Result.map Just
|
||||
)
|
||||
|> toFieldResult
|
||||
, properties = []
|
||||
}
|
||||
|
||||
|
||||
parseTimeOfDay rawTimeOfDay =
|
||||
case rawTimeOfDay |> String.split ":" |> List.map String.toInt of
|
||||
[ Just hours, Just minutes ] ->
|
||||
Ok
|
||||
{ hours = hours
|
||||
, minutes = minutes
|
||||
}
|
||||
|
||||
_ ->
|
||||
Err ()
|
||||
|
||||
|
||||
validateRequiredField : { toError | missing : error } -> Maybe String -> Result error String
|
||||
validateRequiredField toError maybeRaw =
|
||||
if (maybeRaw |> Maybe.withDefault "") == "" then
|
||||
|
0
src/Form/Value.elm
Normal file
0
src/Form/Value.elm
Normal file
@ -133,6 +133,23 @@ all =
|
||||
|> expectErrorsAfterUpdates
|
||||
[ ( "name", [ "Required" ] )
|
||||
]
|
||||
, test "parses time" <|
|
||||
\() ->
|
||||
let
|
||||
form =
|
||||
Form.succeed identity
|
||||
|> Form.with
|
||||
(Form.time "checkin-time"
|
||||
{ invalid = \_ -> "Invalid time" }
|
||||
toInput
|
||||
|> Form.required "Required"
|
||||
)
|
||||
in
|
||||
form
|
||||
|> expectDecodeNoErrors2 [ ( "checkin-time", "08:45" ) ]
|
||||
{ hours = 8
|
||||
, minutes = 45
|
||||
}
|
||||
, test "no duplicate validation errors from update call" <|
|
||||
\() ->
|
||||
Form.succeed identity
|
||||
@ -236,6 +253,19 @@ all =
|
||||
]
|
||||
|
||||
|
||||
expectDecodeNoErrors2 : List ( String, String ) -> decoded -> Form.Form String decoded view -> Expect.Expectation
|
||||
expectDecodeNoErrors2 updates expected form =
|
||||
let
|
||||
formModel =
|
||||
form
|
||||
|> Form.init
|
||||
|> updateFieldsWithValues updates form
|
||||
in
|
||||
Form.runClientValidations formModel form
|
||||
|> Expect.equal
|
||||
(Ok ( expected, [] ))
|
||||
|
||||
|
||||
expectDecodeNoErrors : decoded -> Result error ( decoded, List b ) -> Expect.Expectation
|
||||
expectDecodeNoErrors decoded actual =
|
||||
actual
|
||||
@ -274,6 +304,17 @@ updateAllFields fields form model =
|
||||
model
|
||||
|
||||
|
||||
updateFieldsWithValues : List ( String, String ) -> Form.Form String value view -> Form.Model -> Form.Model
|
||||
updateFieldsWithValues fields form model =
|
||||
fields
|
||||
|> List.foldl
|
||||
(\( fieldName, fieldValue ) modelSoFar ->
|
||||
modelSoFar
|
||||
|> updateField form ( fieldName, fieldValue )
|
||||
)
|
||||
model
|
||||
|
||||
|
||||
expectErrorsAfterUpdates : List ( String, List String ) -> Form.Form String value view -> Expect.Expectation
|
||||
expectErrorsAfterUpdates expected form =
|
||||
let
|
||||
|
Loading…
Reference in New Issue
Block a user