Allow custom onsubmit in stateless form.

This commit is contained in:
Dillon Kearns 2022-05-13 11:17:50 -07:00
parent 6f6db5c1bf
commit 2be5d1c07e
3 changed files with 12 additions and 4 deletions

View File

@ -275,6 +275,6 @@ view maybeUrl sharedModel static =
[]
[ Html.text <| "Edit profile " ++ user.first ++ " " ++ user.last ]
, form user
|> Form.toStatelessHtml Html.form (static.action |> Maybe.map .errors |> Maybe.withDefault (Form.init (form user)))
|> Form.toStatelessHtml Nothing Html.form (static.action |> Maybe.map .errors |> Maybe.withDefault (Form.init (form user)))
]
}

View File

@ -271,6 +271,7 @@ view maybeUrl sharedModel model static =
[ Html.text item.description
, deleteItemForm item.id
|> Form.toStatelessHtml
(Just (DeleteFormSubmitted item.id))
Html.form
(Form.init (deleteItemForm item.id))
]
@ -279,6 +280,7 @@ view maybeUrl sharedModel model static =
, errorsView static.action
, newItemForm model.submitting
|> Form.toStatelessHtml
(Just FormSubmitted)
Html.form
(Form.init (newItemForm model.submitting))
]

View File

@ -1889,18 +1889,24 @@ performing a client-side submit onSubmit (usually with Effect.Perform).
-}
toStatelessHtml :
(List (Html.Attribute (Pages.Msg.Msg msg)) -> List view -> view)
Maybe (List ( String, String ) -> msg)
-> (List (Html.Attribute (Pages.Msg.Msg msg)) -> List view -> view)
-> Model
-> Form (Pages.Msg.Msg msg) String value view
-> view
toStatelessHtml toForm serverValidationErrors (Form fields _ _ _ config) =
toStatelessHtml customOnSubmit toForm serverValidationErrors (Form fields _ _ _ config) =
toForm
-- TODO get method from config
[ config.method
|> Maybe.withDefault "POST"
|> Attr.method
, Attr.novalidate True
, Pages.Msg.onSubmit
, customOnSubmit
|> Maybe.map
(\onSubmit ->
FormDecoder.formDataOnSubmit |> Attr.map (onSubmit >> Pages.Msg.UserMsg)
)
|> Maybe.withDefault Pages.Msg.onSubmit
]
(renderedFields Nothing serverValidationErrors fields)