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
|
|
|
-}
|
|
|
|
|
2020-03-31 23:04:36 +03:00
|
|
|
import Accessibility.Styled as Html exposing (..)
|
2020-03-24 03:33:42 +03:00
|
|
|
import Category exposing (Category(..))
|
2020-03-31 23:04:36 +03:00
|
|
|
import Debug.Control as Control exposing (Control)
|
2018-05-25 21:16:54 +03:00
|
|
|
import Dict exposing (Dict)
|
2020-03-24 03:33:42 +03:00
|
|
|
import ModuleExample exposing (ModuleExample)
|
2019-07-23 17:58:23 +03:00
|
|
|
import Nri.Ui.Heading.V2 as Heading
|
2019-08-03 03:20:09 +03:00
|
|
|
import Nri.Ui.TextInput.V5 as TextInput
|
2020-03-24 03:48:53 +03:00
|
|
|
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
|
2020-03-31 23:04:36 +03:00
|
|
|
| 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
|
2020-03-31 23:04:36 +03:00
|
|
|
, 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 =
|
2020-03-31 23:04:36 +03:00
|
|
|
let
|
|
|
|
exampleConfig =
|
|
|
|
Control.currentValue state.control
|
|
|
|
in
|
2019-11-19 23:03:09 +03:00
|
|
|
{ name = "Nri.Ui.TextInput.V5"
|
2020-03-24 03:48:53 +03:00
|
|
|
, categories = Set.fromList Category.sorter <| List.singleton Inputs
|
2018-05-25 21:16:54 +03:00
|
|
|
, content =
|
2018-10-23 19:55:30 +03:00
|
|
|
[ Html.map parentMessage <|
|
|
|
|
Html.div []
|
2020-03-31 23:04:36 +03:00
|
|
|
[ 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)"
|
2020-03-31 23:04:36 +03:00
|
|
|
, 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
|
2018-10-23 19:55:30 +03:00
|
|
|
, onInput = SetTextInput 1
|
2019-01-07 23:35:47 +03:00
|
|
|
, onBlur = Nothing
|
2018-10-23 19:55:30 +03:00
|
|
|
, autofocus = False
|
|
|
|
, type_ = TextInput.text
|
|
|
|
}
|
2020-03-31 23:04:36 +03:00
|
|
|
, Heading.h3 [] [ text "... type_ = TextInput.number" ]
|
2018-10-23 19:55:30 +03:00
|
|
|
, TextInput.view
|
2020-03-31 23:26:35 +03:00
|
|
|
{ label = exampleConfig.label ++ " (number)"
|
2020-03-31 23:04:36 +03:00
|
|
|
, isInError = exampleConfig.isInError
|
|
|
|
, placeholder = exampleConfig.placeholder
|
|
|
|
, showLabel = exampleConfig.showLabel
|
2018-10-23 19:55:30 +03:00
|
|
|
, value = state.numberInputValue
|
|
|
|
, onInput = SetNumberInput
|
2019-01-07 23:35:47 +03:00
|
|
|
, onBlur = Nothing
|
2018-10-23 19:55:30 +03:00
|
|
|
, autofocus = False
|
|
|
|
, type_ = TextInput.number
|
|
|
|
}
|
2020-03-31 23:04:36 +03:00
|
|
|
, 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)"
|
2020-03-31 23:04:36 +03:00
|
|
|
, 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
|
|
|
|
}
|
2020-03-31 23:04:36 +03:00
|
|
|
, 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)"
|
2020-03-31 23:04:36 +03:00
|
|
|
, 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
|
2018-10-23 19:55:30 +03:00
|
|
|
}
|
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
|
|
|
|
}
|
2020-03-31 23:04:36 +03:00
|
|
|
, Heading.h3 [] [ Html.text "TextInput.writing { type_ = TextInput.text }" ]
|
2018-10-23 19:55:30 +03:00
|
|
|
, TextInput.writing
|
2020-03-31 23:26:35 +03:00
|
|
|
{ label = exampleConfig.label ++ " (writing)"
|
2020-03-31 23:04:36 +03:00
|
|
|
, isInError = exampleConfig.isInError
|
|
|
|
, placeholder = exampleConfig.placeholder
|
2020-03-31 23:19:56 +03:00
|
|
|
, value = Maybe.withDefault "" <| Dict.get 4 state.stringInputValues
|
2018-10-23 19:55:30 +03:00
|
|
|
, onInput = SetTextInput 4
|
2019-01-07 23:35:47 +03:00
|
|
|
, onBlur = Nothing
|
2018-10-23 19:55:30 +03:00
|
|
|
, autofocus = False
|
|
|
|
, type_ = TextInput.text
|
2020-03-31 23:04:36 +03:00
|
|
|
, showLabel = exampleConfig.showLabel
|
2018-10-23 19:55:30 +03:00
|
|
|
}
|
2020-03-31 23:04:36 +03:00
|
|
|
, 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)"
|
2020-03-31 23:04:36 +03:00
|
|
|
, 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
|
2020-03-31 23:04:36 +03:00
|
|
|
, showLabel = exampleConfig.showLabel
|
2019-01-07 23:35:47 +03:00
|
|
|
}
|
2018-10-23 19:55:30 +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 = ""
|
2020-03-31 23:04:36 +03:00
|
|
|
, 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 )
|
|
|
|
|
2020-03-31 23:04:36 +03:00
|
|
|
UpdateControl newControl ->
|
|
|
|
( { state | control = newControl }, Cmd.none )
|
|
|
|
|
2018-05-25 21:16:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- INTERNAL
|
|
|
|
|
|
|
|
|
|
|
|
type alias Id =
|
|
|
|
Int
|