noredink-ui/styleguide-app/Examples/TextInput.elm

196 lines
7.2 KiB
Elm
Raw Normal View History

2020-03-31 22:43:32 +03:00
module Examples.TextInput exposing (Msg, State, example)
2018-05-25 21:16:54 +03:00
2019-08-03 03:20:09 +03:00
{-|
2020-03-31 22:43:32 +03:00
@docs Msg, State, example
2019-08-03 03:20:09 +03:00
2018-05-25 21:16:54 +03:00
-}
import Accessibility.Styled as Html exposing (..)
import Category exposing (Category(..))
import Debug.Control as Control exposing (Control)
2018-05-25 21:16:54 +03:00
import Dict exposing (Dict)
2020-03-31 23:20:03 +03:00
import Example exposing (Example)
import Nri.Ui.Heading.V2 as Heading
2020-04-14 00:28:15 +03:00
import Nri.Ui.TextInput.V6 as TextInput
2018-05-25 21:16:54 +03:00
{-| -}
type Msg
= SetTextInput Id String
| SetNumberInput (Maybe Int)
2019-11-19 01:43:58 +03:00
| SetFloatInput (Maybe Float)
2019-08-05 22:27:29 +03:00
| SetPassword String
| UpdateControl (Control ExampleConfig)
2018-05-25 21:16:54 +03:00
{-| -}
type alias State =
{ numberInputValue : Maybe Int
2019-11-19 01:43:58 +03:00
, floatInputValue : Maybe Float
2020-03-31 23:19:56 +03:00
, stringInputValues : Dict Id String
2019-08-05 22:27:29 +03:00
, passwordInputValue : String
, control : Control ExampleConfig
}
type alias ExampleConfig =
2020-04-14 19:00:25 +03:00
{ label : String
, maybePlaceholderAttribute : Maybe (TextInput.Attribute Msg)
, maybeErrorAttribute : Maybe (TextInput.Attribute Msg)
, maybeShowLabelAttribute : Maybe (TextInput.Attribute Msg)
2018-05-25 21:16:54 +03:00
}
{-| -}
2020-03-31 23:20:03 +03:00
example : Example State Msg
2020-03-31 22:43:32 +03:00
example =
2020-04-14 00:28:15 +03:00
{ name = "Nri.Ui.TextInput.V6"
2020-03-31 22:43:32 +03:00
, categories = [ Inputs ]
, state = init
, update = update
2020-03-31 22:48:26 +03:00
, subscriptions = \_ -> Sub.none
2020-03-31 22:43:32 +03:00
, view =
\state ->
let
exampleConfig =
Control.currentValue state.control
in
2020-03-31 22:43:32 +03:00
[ Html.div []
[ Control.view UpdateControl state.control
|> Html.fromUnstyled
, Heading.h3 [] [ text "TextInput.view { type_ = TextInput.text }" ]
, TextInput.view (exampleConfig.label ++ " (text)")
(TextInput.text (SetTextInput 1))
2020-04-14 19:00:25 +03:00
(List.filterMap identity
[ exampleConfig.maybeErrorAttribute
, exampleConfig.maybePlaceholderAttribute
, exampleConfig.maybeShowLabelAttribute
]
)
(Maybe.withDefault "" <| Dict.get 1 state.stringInputValues)
, Heading.h3 [] [ text "... type_ = TextInput.number" ]
, TextInput.view (exampleConfig.label ++ " (number)")
(TextInput.number SetNumberInput)
2020-04-14 19:00:25 +03:00
(List.filterMap identity
[ exampleConfig.maybeErrorAttribute
, exampleConfig.maybePlaceholderAttribute
, exampleConfig.maybeShowLabelAttribute
]
)
state.numberInputValue
, Heading.h3 [] [ text "... type_ = TextInput.float" ]
, TextInput.view (exampleConfig.label ++ " (float)")
(TextInput.float SetFloatInput)
2020-04-14 19:00:25 +03:00
(List.filterMap identity
[ exampleConfig.maybeErrorAttribute
, exampleConfig.maybePlaceholderAttribute
, exampleConfig.maybeShowLabelAttribute
]
)
state.floatInputValue
, Heading.h3 [] [ text "... type_ = TextInput.password" ]
, TextInput.view (exampleConfig.label ++ " (password)")
(TextInput.password SetPassword)
2020-04-14 19:00:25 +03:00
(List.filterMap identity
[ exampleConfig.maybeErrorAttribute
, exampleConfig.maybePlaceholderAttribute
, exampleConfig.maybeShowLabelAttribute
]
)
state.passwordInputValue
2020-03-31 23:19:56 +03:00
, Heading.h3 [] [ text "... type_ = TextInput.email" ]
, TextInput.view (exampleConfig.label ++ " (email)")
(TextInput.email (SetTextInput 2))
2020-04-14 19:00:25 +03:00
(List.filterMap identity
[ exampleConfig.maybeErrorAttribute
, exampleConfig.maybePlaceholderAttribute
, exampleConfig.maybeShowLabelAttribute
]
)
(Maybe.withDefault "" <| Dict.get 2 state.stringInputValues)
, Heading.h3 [] [ Html.text "TextInput.writing { type_ = TextInput.text }" ]
, TextInput.view (exampleConfig.label ++ " (writing)")
(TextInput.text (SetTextInput 4))
2020-04-14 19:00:25 +03:00
(List.filterMap identity
[ Just TextInput.writing
, exampleConfig.maybeErrorAttribute
, exampleConfig.maybePlaceholderAttribute
, exampleConfig.maybeShowLabelAttribute
]
)
(Maybe.withDefault "" <| Dict.get 4 state.stringInputValues)
, Heading.h3 [] [ text "onBlur demonstration" ]
, TextInput.view (exampleConfig.label ++ " (onBlur)")
(TextInput.text (SetTextInput 7))
2020-04-14 19:00:25 +03:00
(List.filterMap identity
[ Just TextInput.writing
, Just (TextInput.onBlur (SetTextInput 7 "Blurred!"))
, exampleConfig.maybeErrorAttribute
, exampleConfig.maybePlaceholderAttribute
, exampleConfig.maybeShowLabelAttribute
]
)
(Maybe.withDefault "" <| Dict.get 7 state.stringInputValues)
]
2020-03-31 22:43:32 +03:00
]
2018-05-25 21:16:54 +03:00
}
{-| -}
init : State
init =
{ numberInputValue = Nothing
2019-11-19 01:43:58 +03:00
, floatInputValue = Nothing
2020-03-31 23:19:56 +03:00
, stringInputValues = Dict.empty
2019-08-05 22:27:29 +03:00
, passwordInputValue = ""
, control =
Control.record ExampleConfig
|> Control.field "label" (Control.string "Assignment name")
2020-04-14 19:00:25 +03:00
|> Control.field "placeholder"
(Control.maybe True <|
Control.map TextInput.placeholder <|
Control.string "Learning with commas"
)
|> Control.field "show label"
(Control.choice
[ ( "default (visible)", Control.value Nothing )
, ( "TextInput.hiddenLabel", Control.value (Just TextInput.hiddenLabel) )
]
)
|> Control.field "error state"
(Control.choice
[ ( "default (no error)", Control.value Nothing )
, ( "TextInput.errorIf", Control.map (Just << TextInput.errorIf) <| Control.bool False )
]
)
2018-05-25 21:16:54 +03:00
}
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
SetTextInput id textInputValue ->
2020-03-31 23:19:56 +03:00
( { state | stringInputValues = Dict.insert id textInputValue state.stringInputValues }, Cmd.none )
2018-05-25 21:16:54 +03:00
SetNumberInput numberInputValue ->
( { state | numberInputValue = numberInputValue }, Cmd.none )
2019-11-19 01:43:58 +03:00
SetFloatInput floatInputValue ->
( { state | floatInputValue = floatInputValue }, Cmd.none )
2019-08-05 22:27:29 +03:00
SetPassword password ->
( { state | passwordInputValue = password }, Cmd.none )
UpdateControl newControl ->
( { state | control = newControl }, Cmd.none )
2018-05-25 21:16:54 +03:00
-- INTERNAL
type alias Id =
Int