mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-27 05:44:17 +03:00
Replace requiredRadio with radio that can apply required option.
This commit is contained in:
parent
adc22cf493
commit
5b06fe51a4
@ -399,11 +399,9 @@ form user =
|
|||||||
|> Form.appendForm (|>)
|
|> Form.appendForm (|>)
|
||||||
(Form.succeed identity
|
(Form.succeed identity
|
||||||
|> Form.with
|
|> Form.with
|
||||||
(Form.requiredRadio
|
(Form.radio
|
||||||
"push-notifications"
|
"push-notifications"
|
||||||
{ missing = "Please select your notification preference."
|
"Invalid option"
|
||||||
, invalid = \_ -> "Invalid option"
|
|
||||||
}
|
|
||||||
( ( "PushAll", PushAll )
|
( ( "PushAll", PushAll )
|
||||||
, [ ( "PushEmail", PushEmail )
|
, [ ( "PushEmail", PushEmail )
|
||||||
, ( "PushNone", PushNone )
|
, ( "PushNone", PushNone )
|
||||||
|
118
src/Form.elm
118
src/Form.elm
@ -9,7 +9,7 @@ module Form exposing
|
|||||||
, withInitialValue
|
, withInitialValue
|
||||||
, checkbox, date, email, hidden, multiple, number, password, radio, range, telephone, text, url, floatRange, search
|
, checkbox, date, email, hidden, multiple, number, password, radio, range, telephone, text, url, floatRange, search
|
||||||
, submit
|
, submit
|
||||||
, required, requiredNumber, requiredRadio
|
, required
|
||||||
, validate
|
, validate
|
||||||
, withServerValidation
|
, withServerValidation
|
||||||
, withMax, withMaxDate, withMin, withMinDate
|
, withMax, withMaxDate, withMin, withMinDate
|
||||||
@ -84,7 +84,7 @@ A Date type can be entered with the native date picker UI of the user's browser,
|
|||||||
|
|
||||||
### Required
|
### Required
|
||||||
|
|
||||||
@docs required, requiredNumber, requiredRadio
|
@docs required
|
||||||
|
|
||||||
|
|
||||||
### Custom Client-Side Validations
|
### Custom Client-Side Validations
|
||||||
@ -775,15 +775,30 @@ hidden name value toHtmlFn =
|
|||||||
radio :
|
radio :
|
||||||
-- TODO inject the error type
|
-- TODO inject the error type
|
||||||
String
|
String
|
||||||
|
-> error
|
||||||
-> ( ( String, item ), List ( String, item ) )
|
-> ( ( String, item ), List ( String, item ) )
|
||||||
->
|
->
|
||||||
(item
|
(item
|
||||||
-> FieldRenderInfo error
|
-> FieldRenderInfo error
|
||||||
-> view
|
-> view
|
||||||
)
|
)
|
||||||
-> ({ errors : List error, submitStatus : SubmitStatus } -> List view -> view)
|
->
|
||||||
-> Field error (Maybe item) view {}
|
({ errors : List error
|
||||||
radio name nonEmptyItemMapping toHtmlFn wrapFn =
|
, submitStatus : SubmitStatus
|
||||||
|
, status : FieldStatus
|
||||||
|
}
|
||||||
|
-> List view
|
||||||
|
-> view
|
||||||
|
)
|
||||||
|
->
|
||||||
|
Field
|
||||||
|
error
|
||||||
|
(Maybe item)
|
||||||
|
view
|
||||||
|
{ required : ()
|
||||||
|
, wasMapped : No
|
||||||
|
}
|
||||||
|
radio name invalidError nonEmptyItemMapping toHtmlFn wrapFn =
|
||||||
let
|
let
|
||||||
itemMapping : List ( String, item )
|
itemMapping : List ( String, item )
|
||||||
itemMapping =
|
itemMapping =
|
||||||
@ -816,78 +831,6 @@ radio name nonEmptyItemMapping toHtmlFn wrapFn =
|
|||||||
, type_ = "radio"
|
, type_ = "radio"
|
||||||
, required = False
|
, required = False
|
||||||
, serverValidation = \_ -> DataSource.succeed []
|
, serverValidation = \_ -> DataSource.succeed []
|
||||||
, toHtml =
|
|
||||||
-- TODO use `toString` to set value
|
|
||||||
\formInfo _ fieldInfo info ->
|
|
||||||
items
|
|
||||||
|> List.map (\item -> toHtmlFn item (toRadioInputRecord formInfo name (toString item) info fieldInfo))
|
|
||||||
|> wrapFn { errors = info |> Maybe.map .errors |> Maybe.withDefault [], submitStatus = formInfo.submitStatus }
|
|
||||||
, decode =
|
|
||||||
\raw ->
|
|
||||||
Ok
|
|
||||||
-- TODO on failure, this should be an error
|
|
||||||
( raw |> Maybe.andThen fromString
|
|
||||||
, []
|
|
||||||
)
|
|
||||||
, properties = []
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
{-| -}
|
|
||||||
requiredRadio :
|
|
||||||
String
|
|
||||||
->
|
|
||||||
{ missing : error
|
|
||||||
, invalid : String -> error
|
|
||||||
}
|
|
||||||
-> ( ( String, item ), List ( String, item ) )
|
|
||||||
->
|
|
||||||
(item
|
|
||||||
-> FieldRenderInfo error
|
|
||||||
-> view
|
|
||||||
)
|
|
||||||
->
|
|
||||||
({ errors : List error
|
|
||||||
, submitStatus : SubmitStatus
|
|
||||||
, status : FieldStatus
|
|
||||||
}
|
|
||||||
-> List view
|
|
||||||
-> view
|
|
||||||
)
|
|
||||||
-> Field error item view {}
|
|
||||||
requiredRadio name toError nonEmptyItemMapping toHtmlFn wrapFn =
|
|
||||||
let
|
|
||||||
itemMapping : List ( String, item )
|
|
||||||
itemMapping =
|
|
||||||
nonEmptyItemMapping
|
|
||||||
|> List.NonEmpty.toList
|
|
||||||
|
|
||||||
toString : item -> String
|
|
||||||
toString targetItem =
|
|
||||||
case nonEmptyItemMapping |> List.NonEmpty.toList |> List.filter (\( string, item ) -> item == targetItem) |> List.head of
|
|
||||||
Just ( string, _ ) ->
|
|
||||||
string
|
|
||||||
|
|
||||||
Nothing ->
|
|
||||||
"Missing enum"
|
|
||||||
|
|
||||||
fromString : String -> Maybe item
|
|
||||||
fromString string =
|
|
||||||
itemMapping
|
|
||||||
|> Dict.fromList
|
|
||||||
|> Dict.get string
|
|
||||||
|
|
||||||
items : List item
|
|
||||||
items =
|
|
||||||
itemMapping
|
|
||||||
|> List.map Tuple.second
|
|
||||||
in
|
|
||||||
Field
|
|
||||||
{ name = name
|
|
||||||
, initialValue = Nothing
|
|
||||||
, type_ = "radio"
|
|
||||||
, required = True
|
|
||||||
, serverValidation = \_ -> DataSource.succeed []
|
|
||||||
, toHtml =
|
, toHtml =
|
||||||
\formInfo _ fieldInfo info ->
|
\formInfo _ fieldInfo info ->
|
||||||
items
|
items
|
||||||
@ -895,16 +838,21 @@ requiredRadio name toError nonEmptyItemMapping toHtmlFn wrapFn =
|
|||||||
|> wrapFn { errors = info |> Maybe.map .errors |> Maybe.withDefault [], submitStatus = formInfo.submitStatus, status = info |> Maybe.map .status |> Maybe.withDefault NotVisited }
|
|> wrapFn { errors = info |> Maybe.map .errors |> Maybe.withDefault [], submitStatus = formInfo.submitStatus, status = info |> Maybe.map .status |> Maybe.withDefault NotVisited }
|
||||||
, decode =
|
, decode =
|
||||||
\raw ->
|
\raw ->
|
||||||
raw
|
(if raw == Just "" then
|
||||||
|> validateRequiredField toError
|
Nothing
|
||||||
|> Result.mapError (\_ -> toError.missing)
|
|
||||||
|> Result.andThen
|
else
|
||||||
(\rawString ->
|
raw
|
||||||
rawString
|
)
|
||||||
|
|> Maybe.map
|
||||||
|
(\justValue ->
|
||||||
|
justValue
|
||||||
|> fromString
|
|> fromString
|
||||||
|> Result.fromMaybe (toError.invalid rawString)
|
|> Result.fromMaybe invalidError
|
||||||
|
|> Result.map (\decoded -> ( Just decoded, [] ))
|
||||||
|
|> Result.mapError List.singleton
|
||||||
)
|
)
|
||||||
|> toFieldResult
|
|> Maybe.withDefault (Ok ( Nothing, [] ))
|
||||||
, properties = []
|
, properties = []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user