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

212 lines
7.4 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 Category exposing (Category(..))
2018-05-25 21:16:54 +03:00
import Dict exposing (Dict)
2018-06-01 00:43:24 +03:00
import Html.Styled as Html
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
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
2018-05-25 21:16:54 +03:00
, textInputValues : Dict Id String
2019-08-05 22:27:29 +03:00
, passwordInputValue : String
2018-05-25 21:16:54 +03:00
}
{-| -}
example : (Msg -> msg) -> State -> ModuleExample msg
example parentMessage state =
{ 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 []
[ TextInput.view
{ label = "Criterion"
, isInError = False
, placeholder = "For example, \"Something!!\""
, value = Maybe.withDefault "" <| Dict.get 1 state.textInputValues
, onInput = SetTextInput 1
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.text
, showLabel = True
}
, Html.br [] []
, TextInput.view
{ label = "Points"
, isInError = False
, placeholder = "enter a number"
, value = state.numberInputValue
, onInput = SetNumberInput
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.number
, showLabel = True
}
, Html.br [] []
2019-11-19 01:43:58 +03:00
, TextInput.view
{ label = "Points (decimal)"
, isInError = False
, placeholder = "enter a decimal"
, value = state.floatInputValue
, onInput = SetFloatInput
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.float
, showLabel = True
}
, Html.br [] []
2019-08-05 22:27:29 +03:00
, TextInput.view
{ label = "Password"
, isInError = False
, placeholder = ""
, value = state.passwordInputValue
, onInput = SetPassword
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.password
, showLabel = True
}
, Html.br [] []
, TextInput.view
{ label = "Error"
, isInError = True
, placeholder = "Wrong!"
, value = state.numberInputValue
, onInput = SetNumberInput
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.number
, showLabel = True
}
, Heading.h3 [] [ Html.text "invisible label" ]
, TextInput.view
2019-11-08 23:31:38 +03:00
{ label = "Invisible label text input"
, isInError = False
, placeholder = "For example, \"Something!!\""
, value = Maybe.withDefault "" <| Dict.get 2 state.textInputValues
, onInput = SetTextInput 2
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.text
, showLabel = False
}
, Html.br [] []
, TextInput.view
2019-11-08 23:31:38 +03:00
{ label = "Invisible label text input with error"
, placeholder = "Everything you type is wrong!"
, value = Maybe.withDefault "" <| Dict.get 3 state.textInputValues
, onInput = SetTextInput 3
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, isInError = True
, autofocus = False
, type_ = TextInput.text
, showLabel = False
}
, Heading.h3 [] [ Html.text "Writing Style" ]
, TextInput.writing
{ label = "Writing!"
, isInError = False
, placeholder = "Writing is good for me and my family"
, value = Maybe.withDefault "" <| Dict.get 4 state.textInputValues
, onInput = SetTextInput 4
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.text
, showLabel = True
}
, Html.br [] []
, TextInput.writing
{ label = "Writing with errors!"
, isInError = True
, placeholder = "Oopsie!"
, value = Maybe.withDefault "" <| Dict.get 5 state.textInputValues
, onInput = SetTextInput 5
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.text
, showLabel = True
}
, Html.br [] []
, TextInput.writing
{ label = "Writing without labels!"
, isInError = False
, placeholder = "No label on this writing input!"
, value = Maybe.withDefault "" <| Dict.get 6 state.textInputValues
, onInput = SetTextInput 6
2019-01-07 23:35:47 +03:00
, onBlur = Nothing
, autofocus = False
, type_ = TextInput.text
, showLabel = False
}
2019-01-07 23:35:47 +03:00
, Html.br [] []
, TextInput.writing
{ label = "Writing onBlur demonstration!"
, isInError = False
, placeholder = "Click away to blur!"
, value = Maybe.withDefault "" <| Dict.get 7 state.textInputValues
, onInput = SetTextInput 7
, onBlur = Just (SetTextInput 7 "Blurred!")
, autofocus = False
, type_ = TextInput.text
, showLabel = True
}
]
2018-05-25 21:16:54 +03:00
]
}
{-| -}
init : State
init =
{ numberInputValue = Nothing
2019-11-19 01:43:58 +03:00
, floatInputValue = Nothing
2018-05-25 21:16:54 +03:00
, textInputValues = Dict.empty
2019-08-05 22:27:29 +03:00
, passwordInputValue = ""
2018-05-25 21:16:54 +03:00
}
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
SetTextInput id textInputValue ->
( { state | textInputValues = Dict.insert id textInputValue state.textInputValues }, Cmd.none )
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 )
2018-05-25 21:16:54 +03:00
-- INTERNAL
type alias Id =
Int