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

204 lines
6.8 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(..))
2020-04-14 20:59:38 +03:00
import Css exposing (..)
import Debug.Control as Control exposing (Control)
import Debug.Control.Extra as ControlExtra
2018-05-25 21:16:54 +03:00
import Dict exposing (Dict)
2020-03-31 23:20:03 +03:00
import Example exposing (Example)
2020-04-14 20:59:38 +03:00
import Html.Styled.Attributes exposing (css)
import KeyboardSupport exposing (Direction(..), Key(..))
import Nri.Ui.Colors.V1 as Colors
import Nri.Ui.Heading.V2 as Heading
import Nri.Ui.Message.V3 as Message
2021-10-28 02:14:20 +03:00
import Nri.Ui.TextInput.V7 as TextInput
2021-10-28 02:40:03 +03:00
import ViewHelpers exposing (viewExamples)
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-09-09 21:43:10 +03:00
{ name = "TextInput"
2021-10-28 02:14:20 +03:00
, version = 7
2020-03-31 22:43:32 +03:00
, categories = [ Inputs ]
, keyboardSupport = []
2020-03-31 22:43:32 +03:00
, 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
2021-09-29 01:59:07 +03:00
toExample { name, toString, inputType, onBlur, onReset, onEnter } index =
( name
, TextInput.view exampleConfig.label
2021-10-29 01:00:14 +03:00
(exampleConfig.attributes
++ [ TextInput.id ("text-input__" ++ name ++ "-example")
, TextInput.map toString identity (SetInput index) inputType
, TextInput.onInput (SetInput index)
, TextInput.value (Maybe.withDefault "" (Dict.get index state.inputValues))
]
++ List.filterMap identity
[ if exampleConfig.onBlur then
Just (TextInput.onBlur (SetInput index onBlur))
else
Nothing
, if exampleConfig.onReset then
Just (TextInput.onReset (SetInput index onReset))
else
Nothing
, if exampleConfig.onEnter then
Just (TextInput.onEnter (SetInput index onEnter))
else
Nothing
]
)
)
in
[ Control.view UpdateControl state.control
|> Html.fromUnstyled
, (viewExamples << List.indexedMap (\i toView -> toView i))
[ toExample
{ name = "text"
, toString = identity
, inputType = TextInput.text
, onBlur = "Blurred!!!"
, onReset = ""
, onEnter = "Entered!!!"
}
, toExample
{ name = "number"
, toString = Maybe.map String.fromInt >> Maybe.withDefault ""
, inputType = TextInput.number
, onBlur = "10000000"
, onReset = ""
, onEnter = "20000000"
}
, toExample
{ name = "float"
, toString = Maybe.map String.fromFloat >> Maybe.withDefault ""
, inputType = TextInput.float
, onBlur = "1.00000001"
, onReset = ""
, onEnter = "100000001.1"
}
, toExample
{ name = "password"
, toString = identity
, inputType = TextInput.password
, onBlur = "Blurred!!!"
, onReset = ""
, onEnter = "Entered!!!"
}
, toExample
{ name = "email"
, toString = identity
, inputType = TextInput.email
, onBlur = "Blurred!!!"
, onReset = ""
, onEnter = "Entered!!!"
}
, toExample
{ name = "search"
, toString = identity
, inputType = TextInput.search
, onBlur = "Blurred!!!"
, onReset = ""
, onEnter = "Entered!!!"
}
2021-10-28 02:40:03 +03:00
]
2020-03-31 22:43:32 +03:00
]
2018-05-25 21:16:54 +03:00
}
2021-10-28 02:48:04 +03:00
{-| -}
type alias State =
{ inputValues : Dict Int String
2021-10-28 02:48:04 +03:00
, control : Control ExampleConfig
}
2018-05-25 21:16:54 +03:00
{-| -}
init : State
init =
{ inputValues = Dict.empty
2021-10-28 02:48:04 +03:00
, control = initControl
2018-05-25 21:16:54 +03:00
}
2021-10-28 02:48:04 +03:00
type alias ExampleConfig =
{ label : String
, attributes : List (TextInput.Attribute String Msg)
2021-10-28 02:48:04 +03:00
, onBlur : Bool
, onReset : Bool
, onEnter : Bool
}
initControl : Control ExampleConfig
initControl =
Control.record ExampleConfig
|> Control.field "label" (Control.string "Assignment name")
|> Control.field "attributes" controlAttributes
|> Control.field "onBlur" (Control.bool False)
|> Control.field "onReset" (Control.bool False)
|> Control.field "onEnter" (Control.bool False)
2021-10-28 02:48:04 +03:00
controlAttributes : Control (List (TextInput.Attribute value msg))
controlAttributes =
ControlExtra.list
|> ControlExtra.optionalListItem "placeholder"
(Control.map TextInput.placeholder <|
Control.string "Learning with commas"
)
|> ControlExtra.optionalListItem "hiddenLabel"
(Control.value TextInput.hiddenLabel)
|> ControlExtra.optionalListItem "errorIf"
(Control.map TextInput.errorIf <| Control.bool True)
|> ControlExtra.optionalListItem "errorMessage"
(Control.map TextInput.errorMessage <| Control.maybe True <| Control.string "The statement must be true.")
|> ControlExtra.optionalListItem "disabled"
(Control.value TextInput.disabled)
|> ControlExtra.optionalListItem "loading"
(Control.value TextInput.loading)
2021-10-28 02:59:33 +03:00
|> ControlExtra.optionalListItem "writing"
(Control.value TextInput.writing)
|> ControlExtra.listItem "noMargin"
(Control.map TextInput.noMargin (Control.bool False))
|> ControlExtra.optionalListItem "css"
2021-10-28 02:50:07 +03:00
(Control.value (TextInput.css [ Css.backgroundColor Colors.azure ]))
{-| -}
type Msg
= SetInput Int String
| UpdateControl (Control ExampleConfig)
2018-05-25 21:16:54 +03:00
{-| -}
update : Msg -> State -> ( State, Cmd Msg )
update msg state =
case msg of
SetInput id string ->
( { state | inputValues = Dict.insert id string state.inputValues }
, Cmd.none
)
2021-09-29 01:49:36 +03:00
UpdateControl newControl ->
( { state | control = newControl }
, Cmd.none
)