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

191 lines
6.9 KiB
Elm
Raw Normal View History

2018-05-25 21:16:54 +03:00
module Examples.TextInput exposing (Msg, State, example, init, update)
2019-08-03 03:20:09 +03:00
{-|
@docs Msg, State, example, init, update
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)
import ModuleExample exposing (ModuleExample)
import Nri.Ui.Heading.V2 as Heading
2019-08-03 03:20:09 +03:00
import Nri.Ui.TextInput.V5 as TextInput
import Sort.Set as Set exposing (Set)
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 =
{ showLabel : Bool
, label : String
, isInError : Bool
, placeholder : String
2018-05-25 21:16:54 +03:00
}
{-| -}
example : (Msg -> msg) -> State -> ModuleExample msg
example parentMessage state =
let
exampleConfig =
Control.currentValue state.control
in
{ name = "Nri.Ui.TextInput.V5"
, categories = Set.fromList Category.sorter <| List.singleton Inputs
2018-05-25 21:16:54 +03:00
, content =
[ Html.map parentMessage <|
Html.div []
[ Control.view UpdateControl state.control
|> Html.fromUnstyled
, Heading.h3 [] [ text "TextInput.view { type_ = TextInput.text }" ]
, TextInput.view
2020-03-31 23:26:35 +03:00
{ label = exampleConfig.label ++ " (text)"
, isInError = exampleConfig.isInError
, placeholder = exampleConfig.placeholder
, showLabel = exampleConfig.showLabel
2020-03-31 23:19:56 +03:00
, value = Maybe.withDefault "" <| Dict.get 1 state.stringInputValues
, onInput = SetTextInput 1
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.text
}
, Heading.h3 [] [ text "... type_ = TextInput.number" ]
, TextInput.view
2020-03-31 23:26:35 +03:00
{ label = exampleConfig.label ++ " (number)"
, isInError = exampleConfig.isInError
, placeholder = exampleConfig.placeholder
, showLabel = exampleConfig.showLabel
, value = state.numberInputValue
, onInput = SetNumberInput
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.number
}
, Heading.h3 [] [ text "... type_ = TextInput.float" ]
2019-11-19 01:43:58 +03:00
, TextInput.view
2020-03-31 23:26:35 +03:00
{ label = exampleConfig.label ++ " (float)"
, isInError = exampleConfig.isInError
, placeholder = exampleConfig.placeholder
, showLabel = exampleConfig.showLabel
2019-11-19 01:43:58 +03:00
, value = state.floatInputValue
, onInput = SetFloatInput
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.float
}
, Heading.h3 [] [ text "... type_ = TextInput.password" ]
2019-08-05 22:27:29 +03:00
, TextInput.view
2020-03-31 23:26:35 +03:00
{ label = exampleConfig.label ++ " (password)"
, isInError = exampleConfig.isInError
, placeholder = exampleConfig.placeholder
, showLabel = exampleConfig.showLabel
2019-08-05 22:27:29 +03:00
, value = state.passwordInputValue
, onInput = SetPassword
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.password
}
2020-03-31 23:19:56 +03:00
, Heading.h3 [] [ text "... type_ = TextInput.email" ]
, TextInput.view
2020-03-31 23:26:35 +03:00
{ label = exampleConfig.label ++ " (email)"
2020-03-31 23:19:56 +03:00
, isInError = exampleConfig.isInError
, placeholder = exampleConfig.placeholder
, showLabel = exampleConfig.showLabel
, value = Maybe.withDefault "" <| Dict.get 2 state.stringInputValues
, onInput = SetTextInput 2
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.email
}
, Heading.h3 [] [ Html.text "TextInput.writing { type_ = TextInput.text }" ]
, TextInput.writing
2020-03-31 23:26:35 +03:00
{ label = exampleConfig.label ++ " (writing)"
, isInError = exampleConfig.isInError
, placeholder = exampleConfig.placeholder
2020-03-31 23:19:56 +03:00
, value = Maybe.withDefault "" <| Dict.get 4 state.stringInputValues
, onInput = SetTextInput 4
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.text
, showLabel = exampleConfig.showLabel
}
, Heading.h3 [] [ text "onBlur demonstration" ]
2019-01-07 23:35:47 +03:00
, TextInput.writing
2020-03-31 23:26:35 +03:00
{ label = exampleConfig.label ++ " (onBlur)"
, isInError = exampleConfig.isInError
, placeholder = exampleConfig.placeholder
2020-03-31 23:19:56 +03:00
, value = Maybe.withDefault "" <| Dict.get 7 state.stringInputValues
2019-01-07 23:35:47 +03:00
, onInput = SetTextInput 7
, onBlur = Just (SetTextInput 7 "Blurred!")
, autofocus = False
, type_ = TextInput.text
, showLabel = exampleConfig.showLabel
2019-01-07 23:35:47 +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 "showLabel" (Control.bool True)
|> Control.field "label" (Control.string "Assignment name")
|> Control.field "isInError" (Control.bool False)
|> Control.field "placeholder" (Control.string "Learning with commas")
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