From d5478db009f0dce35b2779eccb34bb9b63a1a64f Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Sun, 16 Jan 2022 14:31:08 -0800 Subject: [PATCH] Add time input. --- src/Form.elm | 62 +++++++++++++++++++++++++++++++++++++++++++++ src/Form/Value.elm | 0 tests/FormTests.elm | 41 ++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 src/Form/Value.elm diff --git a/src/Form.elm b/src/Form.elm index 8b0b5fcb..cec4cbc6 100644 --- a/src/Form.elm +++ b/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 diff --git a/src/Form/Value.elm b/src/Form/Value.elm new file mode 100644 index 00000000..e69de29b diff --git a/tests/FormTests.elm b/tests/FormTests.elm index c6525801..5bd17d5b 100644 --- a/tests/FormTests.elm +++ b/tests/FormTests.elm @@ -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