Use builder pattern for rendering forms.

This commit is contained in:
Dillon Kearns 2022-07-08 10:57:22 -07:00
parent e2505843e0
commit 70b1423bd2
5 changed files with 44 additions and 55 deletions

View File

@ -262,13 +262,8 @@ view maybeUrl sharedModel static =
, Html.h1
[]
[ Html.text <| "Edit profile " ++ user.first ++ " " ++ user.last ]
, Form.renderHtml
{ method = Form.Post
, submitStrategy = Form.TransitionStrategy
, name = Just "test1"
}
static
defaultUser
form
, form
|> Form.toDynamicTransition "test1"
|> Form.renderHtml static defaultUser
]
}

View File

@ -195,7 +195,10 @@ view maybeUrl sharedModel model static =
{ title = "Search"
, body =
[ Html.h2 [] [ Html.text "Search" ]
, Form.renderHtml { method = Form.Get, submitStrategy = Form.TransitionStrategy, name = Just "test1" } static () form
, form
|> Form.toDynamicTransition "test1"
|> Form.withGetMethod
|> Form.renderHtml static ()
, static.data.results
|> Maybe.map resultsView
|> Maybe.withDefault (Html.div [] [])

View File

@ -264,7 +264,9 @@ view maybeUrl sharedModel model static =
Html.text ""
]
, flashView static.data.flashMessage
, Form.renderHtml { method = Form.Post, submitStrategy = Form.TransitionStrategy, name = Just "test1" } static () form
, form
|> Form.toDynamicTransition "test1"
|> Form.renderHtml static ()
]
}

View File

@ -340,14 +340,9 @@ view maybeUrl sharedModel model static =
[ Html.text item.description
-- TODO should the (List Html.Attribute) be passed in to renderHtml instead of the `( List Html, List Attr)` in Form.init?
, Form.renderHtml
{ submitStrategy = Form.TransitionStrategy
, method = Form.Post
, name = Just "test1"
}
static
item.id
deleteForm
, deleteForm
|> Form.toDynamicTransition "test1"
|> Form.renderHtml static item.id
]
)
)
@ -364,13 +359,8 @@ view maybeUrl sharedModel model static =
]
)
)
, Form.renderHtml
{ submitStrategy = Form.TransitionStrategy
, method = Form.Post
, name = Just "test2"
}
static
()
createForm
, createForm
|> Form.toDynamicTransition "test2"
|> Form.renderHtml static ()
]
}

View File

@ -5,7 +5,7 @@ module Pages.Form exposing
, andThen
, Context, ViewField
, renderHtml, renderStyledHtml
, Method(..), SubmitStrategy(..)
, FinalForm, withGetMethod, toDynamicTransition, toDynamicFetcher
, parse, runOneOfServerSide, runServerSide
, dynamic, HtmlSubForm
, runOneOfServerSideWithServerValidations
@ -13,8 +13,6 @@ module Pages.Form exposing
, AppContext
, RenderOptions
-- subGroup
, toDynamicFetcher
, toDynamicTransition
)
@ -47,10 +45,7 @@ module Pages.Form exposing
@docs renderHtml, renderStyledHtml
### Rendering Options
@docs Method, SubmitStrategy
@docs FinalForm, withGetMethod, toDynamicTransition, toDynamicFetcher
## Running Parsers
@ -859,11 +854,10 @@ runOneOfServerSideWithServerValidations rawFormData parsers =
{-| -}
renderHtml :
RenderOptions
-> AppContext app
AppContext app
-> data
->
Form
FinalForm
error
(Validation error parsed)
data
@ -871,8 +865,8 @@ renderHtml :
-> ( List (Html.Attribute (Pages.Msg.Msg msg)), List (Html (Pages.Msg.Msg msg)) )
)
-> Html (Pages.Msg.Msg msg)
renderHtml options app data combinedParser =
Html.Lazy.lazy4 renderHelper options app data combinedParser
renderHtml app data (FinalForm options a b c) =
Html.Lazy.lazy4 renderHelper options app data (Form a b c)
{-| -}
@ -933,6 +927,11 @@ toDynamicTransition name (Form a b c) =
FinalForm options a b c
withGetMethod : FinalForm error parsed data view -> FinalForm error parsed data view
withGetMethod (FinalForm options a b c) =
FinalForm { options | method = Get } a b c
{-| -}
renderStyledHtml :
List (Html.Styled.Attribute (Pages.Msg.Msg msg))
@ -1118,27 +1117,27 @@ renderStyledHelperNew attrs maybe options formState data (Form fieldDefinitions
part2 : Dict String Form.FieldState
part2 =
--formState.pageFormState
-- |> Dict.get formId
-- |> Maybe.withDefault
-- (maybe
-- |> Maybe.map
-- (\{ fields } ->
-- { fields =
-- fields
-- |> List.map (Tuple.mapSecond (\value -> { value = value, status = Form.NotVisited }))
-- |> Dict.fromList
-- , submitAttempted = True
-- }
-- )
-- |> Maybe.withDefault initFormState
-- )
-- |> .fields
formState.pageFormState
|> Dict.get formId
|> Maybe.withDefault
(maybe
|> Maybe.map
(\{ fields } ->
{ fields =
fields
|> List.map (Tuple.mapSecond (\value -> { value = value, status = Form.NotVisited }))
|> Dict.fromList
, submitAttempted = True
}
)
|> Maybe.withDefault initFormState
)
|> .fields
--formState.pageFormState
-- |> Dict.get formId
-- |> Maybe.withDefault initFormState
-- |> .fields
fullFormState : Dict String Form.FieldState
fullFormState =
initialValues