Add float field.

This commit is contained in:
Dillon Kearns 2022-06-23 09:36:22 -07:00
parent 7a655336f1
commit fabbec5d80
4 changed files with 79 additions and 6 deletions

File diff suppressed because one or more lines are too long

View File

@ -7,6 +7,11 @@ module Form.Value exposing
@docs Value, date, float, int, string, toString
## Comparison
@docs compare
-}
import Date exposing (Date)
@ -61,6 +66,8 @@ string string_ =
|> Value StringValue
{-| You probably don't need this helper as it's mostly useful for internal implementation.
-}
compare : String -> Value value -> Order
compare a (Value kind rawValue) =
case kind of
@ -83,5 +90,9 @@ compare a (Value kind rawValue) =
|> Result.withDefault LT
FloatValue ->
-- TODO
LT
case ( String.toFloat a, String.toFloat rawValue ) of
( Just parsedA, Just parsedB ) ->
Basics.compare parsedA parsedB
_ ->
LT

View File

@ -1,13 +1,13 @@
module Pages.Field exposing
( text, checkbox, int
( text, checkbox, int, float
, select, range
, date
, Field(..), FieldInfo, exactValue
, required, withClientValidation, withInitialValue
, email, password, search, telephone, url, textarea
, withMax, withMin, withStep
, withMinChecked, withMaxChecked
, No(..), Yes(..)
, withMaxChecked, withMinChecked
)
{-|
@ -15,7 +15,7 @@ module Pages.Field exposing
## Base Fields
@docs text, checkbox, int
@docs text, checkbox, int, float
## Multiple Choice Fields
@ -48,6 +48,11 @@ module Pages.Field exposing
@docs withMax, withMin, withStep
## Temporary Names
@docs withMinChecked, withMaxChecked
## Phantom Options
@docs No, Yes
@ -358,6 +363,47 @@ int toError =
(FieldRenderer.Input FieldRenderer.Number)
{-| -}
float :
{ invalid : String -> error }
->
Field
error
(Maybe Float)
data
Input
{ min : Float
, max : Float
, required : ()
, wasMapped : No
, initial : Float
}
float toError =
Field
{ initialValue = Nothing
, required = False
, serverValidation = \_ -> DataSource.succeed []
, decode =
\rawString ->
case rawString of
Nothing ->
( Just Nothing, [] )
Just "" ->
( Just Nothing, [] )
Just string ->
case string |> String.toFloat of
Just parsedFloat ->
( Just (Just parsedFloat), [] )
Nothing ->
( Nothing, [ toError.invalid string ] )
, properties = []
}
(FieldRenderer.Input FieldRenderer.Number)
{-| -}
telephone :
Field error parsed data Input { constraints | plainText : () }

View File

@ -56,6 +56,22 @@ all =
, ( Just "100", Ok 100 )
, ( Just "1.23", Err [ "Invalid" ] )
]
, test "required float with range" <|
\() ->
Field.float { invalid = \_ -> "Invalid" }
|> Field.required "Required"
|> Field.withMinChecked (Value.float 100) "Must be at least 100"
|> Field.withMaxChecked (Value.float 200) "Too large"
|> expect
[ ( Just "", Err [ "Required" ] )
, ( Nothing, Err [ "Required" ] )
, ( Just "1", Err [ "Must be at least 100" ] )
, ( Just "100.1", Ok 100.1 )
, ( Just "200", Ok 200 )
, ( Just "200.1", Err [ "Too large" ] )
, ( Just "201", Err [ "Too large" ] )
, ( Just "99.9", Err [ "Must be at least 100" ] )
]
, test "required date with range" <|
\() ->
Field.date { invalid = \_ -> "Invalid" }