mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-11-24 15:12:01 +03:00
Wire up properties and field types to field renderer.
This commit is contained in:
parent
116e9b775d
commit
aa9c1d09d7
@ -273,6 +273,7 @@ form =
|
|||||||
|> FormParser.field "price"
|
|> FormParser.field "price"
|
||||||
(Field.int { invalid = \_ -> "Invalid int" }
|
(Field.int { invalid = \_ -> "Invalid int" }
|
||||||
|> Field.required "Required"
|
|> Field.required "Required"
|
||||||
|
|> Field.withMin (Form.Value.int 0)
|
||||||
|> Field.withInitialValue (\{ smoothie } -> Form.Value.int smoothie.price)
|
|> Field.withInitialValue (\{ smoothie } -> Form.Value.int smoothie.price)
|
||||||
)
|
)
|
||||||
|> FormParser.field "imageUrl"
|
|> FormParser.field "imageUrl"
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
module Pages.Field exposing (Field(..), FieldInfo, No(..), Yes(..), checkbox, exactValue, int, required, text, withClientValidation, withInitialValue, select)
|
module Pages.Field exposing
|
||||||
|
( Field(..), FieldInfo, No(..), Yes(..), checkbox, exactValue, int, required, text, withClientValidation, withInitialValue, select
|
||||||
|
, withMax, withMin, withStep
|
||||||
|
)
|
||||||
|
|
||||||
{-|
|
{-|
|
||||||
|
|
||||||
@ -280,7 +283,7 @@ int toError =
|
|||||||
( Nothing, [ toError.invalid string ] )
|
( Nothing, [ toError.invalid string ] )
|
||||||
, properties = []
|
, properties = []
|
||||||
}
|
}
|
||||||
(FieldRenderer.Input FieldRenderer.Text)
|
(FieldRenderer.Input FieldRenderer.Number)
|
||||||
|
|
||||||
|
|
||||||
{-| -}
|
{-| -}
|
||||||
@ -319,3 +322,32 @@ withInitialValue toInitialValue (Field field kind) =
|
|||||||
Just (toInitialValue >> Form.Value.toString)
|
Just (toInitialValue >> Form.Value.toString)
|
||||||
}
|
}
|
||||||
kind
|
kind
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Input Properties
|
||||||
|
|
||||||
|
|
||||||
|
{-| -}
|
||||||
|
withMin : Form.Value.Value valueType -> Field msg error value view { constraints | min : valueType } -> Field msg error value view constraints
|
||||||
|
withMin min field =
|
||||||
|
withStringProperty ( "min", Form.Value.toString min ) field
|
||||||
|
|
||||||
|
|
||||||
|
{-| -}
|
||||||
|
withMax : Form.Value.Value valueType -> Field msg error value view { constraints | max : valueType } -> Field msg error value view constraints
|
||||||
|
withMax max field =
|
||||||
|
withStringProperty ( "max", Form.Value.toString max ) field
|
||||||
|
|
||||||
|
|
||||||
|
{-| -}
|
||||||
|
withStep : Form.Value.Value valueType -> Field msg error value view { constraints | step : valueType } -> Field msg error value view constraints
|
||||||
|
withStep max field =
|
||||||
|
withStringProperty ( "step", Form.Value.toString max ) field
|
||||||
|
|
||||||
|
|
||||||
|
withStringProperty : ( String, String ) -> Field error parsed data kind constraints1 -> Field error parsed data kind constraints2
|
||||||
|
withStringProperty ( key, value ) (Field field kind) =
|
||||||
|
Field
|
||||||
|
{ field | properties = ( key, Encode.string value ) :: field.properties }
|
||||||
|
kind
|
||||||
|
@ -4,11 +4,60 @@ module Pages.FieldRenderer exposing (..)
|
|||||||
|
|
||||||
import Html exposing (Html)
|
import Html exposing (Html)
|
||||||
import Html.Attributes as Attr
|
import Html.Attributes as Attr
|
||||||
|
import Json.Encode as Encode
|
||||||
|
|
||||||
|
|
||||||
type InputType
|
type InputType
|
||||||
= Text
|
= Text
|
||||||
|
| Number
|
||||||
|
| Range
|
||||||
|
| Radio
|
||||||
|
-- TODO should submit be a special type, or an Input type?
|
||||||
|
-- TODO have an option for a submit with a name/value?
|
||||||
|
| Date
|
||||||
| Checkbox
|
| Checkbox
|
||||||
|
| Tel
|
||||||
|
| Search
|
||||||
|
| Password
|
||||||
|
| Email
|
||||||
|
| Url
|
||||||
|
|
||||||
|
|
||||||
|
inputTypeToString : InputType -> String
|
||||||
|
inputTypeToString inputType =
|
||||||
|
case inputType of
|
||||||
|
Text ->
|
||||||
|
"text"
|
||||||
|
|
||||||
|
Number ->
|
||||||
|
"number"
|
||||||
|
|
||||||
|
Range ->
|
||||||
|
"range"
|
||||||
|
|
||||||
|
Radio ->
|
||||||
|
"radio"
|
||||||
|
|
||||||
|
Date ->
|
||||||
|
"date"
|
||||||
|
|
||||||
|
Checkbox ->
|
||||||
|
"checkbox"
|
||||||
|
|
||||||
|
Tel ->
|
||||||
|
"tel"
|
||||||
|
|
||||||
|
Search ->
|
||||||
|
"search"
|
||||||
|
|
||||||
|
Password ->
|
||||||
|
"password"
|
||||||
|
|
||||||
|
Email ->
|
||||||
|
"email"
|
||||||
|
|
||||||
|
Url ->
|
||||||
|
"url"
|
||||||
|
|
||||||
|
|
||||||
type Input
|
type Input
|
||||||
@ -26,31 +75,28 @@ input :
|
|||||||
{ input
|
{ input
|
||||||
| value : Maybe String
|
| value : Maybe String
|
||||||
, name : String
|
, name : String
|
||||||
, kind : Input
|
, kind : ( Input, List ( String, Encode.Value ) )
|
||||||
}
|
}
|
||||||
-> Html msg
|
-> Html msg
|
||||||
input attrs rawField =
|
input attrs rawField =
|
||||||
case rawField.kind of
|
case rawField.kind of
|
||||||
Input inputType ->
|
( Input inputType, properties ) ->
|
||||||
case inputType of
|
Html.input
|
||||||
Text ->
|
(attrs
|
||||||
Html.input
|
++ toHtmlProperties properties
|
||||||
(attrs
|
++ [ (case inputType of
|
||||||
++ [ Attr.value (rawField.value |> Maybe.withDefault "") -- TODO is this an okay default?
|
Checkbox ->
|
||||||
, Attr.name rawField.name
|
Attr.checked ((rawField.value |> Maybe.withDefault "") == "on")
|
||||||
]
|
|
||||||
)
|
|
||||||
[]
|
|
||||||
|
|
||||||
Checkbox ->
|
_ ->
|
||||||
Html.input
|
Attr.value (rawField.value |> Maybe.withDefault "")
|
||||||
(attrs
|
-- TODO is this an okay default?
|
||||||
++ [ Attr.checked ((rawField.value |> Maybe.withDefault "") == "on")
|
)
|
||||||
, Attr.name rawField.name
|
, Attr.name rawField.name
|
||||||
, Attr.type_ "checkbox"
|
, inputType |> inputTypeToString |> Attr.type_
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
[]
|
[]
|
||||||
|
|
||||||
|
|
||||||
{-| -}
|
{-| -}
|
||||||
@ -67,13 +113,13 @@ select :
|
|||||||
{ input
|
{ input
|
||||||
| value : Maybe String
|
| value : Maybe String
|
||||||
, name : String
|
, name : String
|
||||||
, kind : Select parsed
|
, kind : ( Select parsed, List ( String, Encode.Value ) )
|
||||||
}
|
}
|
||||||
-> Html msg
|
-> Html msg
|
||||||
select selectAttrs enumToOption rawField =
|
select selectAttrs enumToOption rawField =
|
||||||
let
|
let
|
||||||
(Select parseValue possibleValues) =
|
(Select parseValue possibleValues) =
|
||||||
rawField.kind
|
rawField.kind |> Tuple.first
|
||||||
in
|
in
|
||||||
Html.select
|
Html.select
|
||||||
(selectAttrs
|
(selectAttrs
|
||||||
@ -104,3 +150,12 @@ select selectAttrs enumToOption rawField =
|
|||||||
Nothing
|
Nothing
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
toHtmlProperties : List ( String, Encode.Value ) -> List (Html.Attribute msg)
|
||||||
|
toHtmlProperties properties =
|
||||||
|
properties
|
||||||
|
|> List.map
|
||||||
|
(\( key, value ) ->
|
||||||
|
Attr.property key value
|
||||||
|
)
|
||||||
|
@ -11,6 +11,7 @@ import Dict.Extra
|
|||||||
import Html exposing (Html)
|
import Html exposing (Html)
|
||||||
import Html.Attributes as Attr
|
import Html.Attributes as Attr
|
||||||
import Html.Lazy
|
import Html.Lazy
|
||||||
|
import Json.Encode as Encode
|
||||||
import Pages.Field as Field exposing (Field(..))
|
import Pages.Field as Field exposing (Field(..))
|
||||||
import Pages.FieldRenderer
|
import Pages.FieldRenderer
|
||||||
import Pages.Form as Form
|
import Pages.Form as Form
|
||||||
@ -103,14 +104,14 @@ field name (Field fieldParser kind) (CombinedParser definitions parseFn toInitia
|
|||||||
{ name = name
|
{ name = name
|
||||||
, value = Just info.value
|
, value = Just info.value
|
||||||
, status = info.status
|
, status = info.status
|
||||||
, kind = kind
|
, kind = ( kind, fieldParser.properties )
|
||||||
}
|
}
|
||||||
|
|
||||||
Nothing ->
|
Nothing ->
|
||||||
{ name = name
|
{ name = name
|
||||||
, value = Maybe.map2 (|>) maybeData fieldParser.initialValue
|
, value = Maybe.map2 (|>) maybeData fieldParser.initialValue
|
||||||
, status = Form.NotVisited
|
, status = Form.NotVisited
|
||||||
, kind = kind
|
, kind = ( kind, fieldParser.properties )
|
||||||
}
|
}
|
||||||
|
|
||||||
myFn :
|
myFn :
|
||||||
@ -192,14 +193,14 @@ hiddenField name (Field fieldParser kind) (CombinedParser definitions parseFn to
|
|||||||
{ name = name
|
{ name = name
|
||||||
, value = Just info.value
|
, value = Just info.value
|
||||||
, status = info.status
|
, status = info.status
|
||||||
, kind = ()
|
, kind = ( (), [] )
|
||||||
}
|
}
|
||||||
|
|
||||||
Nothing ->
|
Nothing ->
|
||||||
{ name = name
|
{ name = name
|
||||||
, value = Maybe.map2 (|>) maybeData fieldParser.initialValue
|
, value = Maybe.map2 (|>) maybeData fieldParser.initialValue
|
||||||
, status = Form.NotVisited
|
, status = Form.NotVisited
|
||||||
, kind = ()
|
, kind = ( (), [] )
|
||||||
}
|
}
|
||||||
|
|
||||||
myFn :
|
myFn :
|
||||||
@ -276,14 +277,14 @@ hiddenKind ( name, value ) error_ (CombinedParser definitions parseFn toInitialV
|
|||||||
{ name = name
|
{ name = name
|
||||||
, value = Just info.value
|
, value = Just info.value
|
||||||
, status = info.status
|
, status = info.status
|
||||||
, kind = ()
|
, kind = ( (), [] )
|
||||||
}
|
}
|
||||||
|
|
||||||
Nothing ->
|
Nothing ->
|
||||||
{ name = name
|
{ name = name
|
||||||
, value = Maybe.map2 (|>) maybeData fieldParser.initialValue
|
, value = Maybe.map2 (|>) maybeData fieldParser.initialValue
|
||||||
, status = Form.NotVisited
|
, status = Form.NotVisited
|
||||||
, kind = ()
|
, kind = ( (), [] )
|
||||||
}
|
}
|
||||||
|
|
||||||
myFn :
|
myFn :
|
||||||
@ -795,7 +796,7 @@ type alias RawField kind =
|
|||||||
{ name : String
|
{ name : String
|
||||||
, value : Maybe String
|
, value : Maybe String
|
||||||
, status : Form.FieldStatus
|
, status : Form.FieldStatus
|
||||||
, kind : kind
|
, kind : ( kind, List ( String, Encode.Value ) )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user