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
|
|
|
-}
|
|
|
|
|
2020-03-31 23:04:36 +03:00
|
|
|
import Accessibility.Styled as Html exposing (..)
|
2021-11-06 00:35:09 +03:00
|
|
|
import Accessibility.Styled.Key as Key
|
2020-03-24 03:33:42 +03:00
|
|
|
import Category exposing (Category(..))
|
2020-04-14 20:59:38 +03:00
|
|
|
import Css exposing (..)
|
2020-03-31 23:04:36 +03:00
|
|
|
import Debug.Control as Control exposing (Control)
|
2021-10-28 02:28:46 +03:00
|
|
|
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)
|
2020-06-20 00:45:32 +03:00
|
|
|
import KeyboardSupport exposing (Direction(..), Key(..))
|
2021-09-29 01:52:16 +03:00
|
|
|
import Nri.Ui.Colors.V1 as Colors
|
2019-07-23 17:58:23 +03:00
|
|
|
import Nri.Ui.Heading.V2 as Heading
|
2021-10-27 17:43:53 +03:00
|
|
|
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 ]
|
2020-06-20 00:45:32 +03:00
|
|
|
, keyboardSupport = []
|
2020-03-31 22:43:32 +03:00
|
|
|
, state = init
|
|
|
|
, update = update
|
2020-03-31 22:48:26 +03:00
|
|
|
, subscriptions = \_ -> Sub.none
|
2021-11-06 00:11:11 +03:00
|
|
|
, preview =
|
2021-11-06 00:35:09 +03:00
|
|
|
[ TextInput.view "Text Input"
|
|
|
|
[ TextInput.custom [ Key.tabbable False ]
|
|
|
|
]
|
2021-11-06 00:11:11 +03:00
|
|
|
, TextInput.view "Errored"
|
|
|
|
[ TextInput.value "invalid content"
|
|
|
|
, TextInput.errorIf True
|
2021-11-06 00:35:09 +03:00
|
|
|
, TextInput.custom [ Key.tabbable False ]
|
2021-11-06 00:11:11 +03:00
|
|
|
]
|
|
|
|
]
|
2020-03-31 22:43:32 +03:00
|
|
|
, view =
|
|
|
|
\state ->
|
2021-10-29 00:49:54 +03:00
|
|
|
[ Control.view UpdateControl state.control
|
|
|
|
|> Html.fromUnstyled
|
2022-01-10 22:34:55 +03:00
|
|
|
, viewExamples <|
|
|
|
|
( "readOnlyText"
|
|
|
|
, TextInput.view "Shareable Assignment Link"
|
|
|
|
[ TextInput.readOnlyText
|
|
|
|
, TextInput.value "noredink.com/s/blueprint-code"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
:: customizableExamples state
|
2020-03-31 22:43:32 +03:00
|
|
|
]
|
2018-05-25 21:16:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-10 22:34:55 +03:00
|
|
|
customizableExamples : State -> List ( String, Html Msg )
|
|
|
|
customizableExamples state =
|
|
|
|
let
|
|
|
|
exampleConfig =
|
|
|
|
Control.currentValue state.control
|
|
|
|
|
|
|
|
toExample { name, toString, inputType, onFocus, onBlur, onEnter } index =
|
|
|
|
( name
|
|
|
|
, TextInput.view exampleConfig.label
|
|
|
|
(exampleConfig.attributes
|
|
|
|
++ [ TextInput.id ("text-input__" ++ name ++ "-example")
|
|
|
|
, inputType (toString >> SetInput index)
|
|
|
|
|> TextInput.map toString identity
|
|
|
|
, TextInput.value (Maybe.withDefault "" (Dict.get index state.inputValues))
|
|
|
|
]
|
|
|
|
++ List.filterMap identity
|
|
|
|
[ if exampleConfig.onFocus then
|
|
|
|
Just (TextInput.onFocus (SetInput index onFocus))
|
|
|
|
|
|
|
|
else
|
|
|
|
Nothing
|
|
|
|
, if exampleConfig.onBlur then
|
|
|
|
Just (TextInput.onBlur (SetInput index onBlur))
|
|
|
|
|
|
|
|
else
|
|
|
|
Nothing
|
|
|
|
, if exampleConfig.onEnter then
|
|
|
|
Just (TextInput.onEnter (SetInput index onEnter))
|
|
|
|
|
|
|
|
else
|
|
|
|
Nothing
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
in
|
|
|
|
List.indexedMap (\i toView -> toView i)
|
|
|
|
[ toExample
|
|
|
|
{ name = "text"
|
|
|
|
, toString = identity
|
|
|
|
, inputType = TextInput.text
|
|
|
|
, onFocus = "Focused!!!"
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onEnter = "Entered!!!"
|
|
|
|
}
|
|
|
|
, toExample
|
|
|
|
{ name = "number"
|
|
|
|
, toString = Maybe.map String.fromInt >> Maybe.withDefault ""
|
|
|
|
, inputType = TextInput.number
|
|
|
|
, onFocus = "1234"
|
|
|
|
, onBlur = "10000000"
|
|
|
|
, onEnter = "20000000"
|
|
|
|
}
|
|
|
|
, toExample
|
|
|
|
{ name = "float"
|
|
|
|
, toString = Maybe.map String.fromFloat >> Maybe.withDefault ""
|
|
|
|
, inputType = TextInput.float
|
|
|
|
, onFocus = "123"
|
|
|
|
, onBlur = "1.00000001"
|
|
|
|
, onEnter = "100000001.1"
|
|
|
|
}
|
|
|
|
, toExample
|
|
|
|
{ name = "newPassword"
|
|
|
|
, toString = identity
|
|
|
|
, inputType =
|
|
|
|
\onInput ->
|
|
|
|
TextInput.newPassword
|
|
|
|
{ onInput = onInput
|
|
|
|
, showPassword = state.showPassword
|
|
|
|
, setShowPassword = SetShowPassword
|
|
|
|
}
|
|
|
|
, onFocus = "Focused!!!"
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onEnter = "Entered!!!"
|
|
|
|
}
|
|
|
|
, toExample
|
|
|
|
{ name = "email"
|
|
|
|
, toString = identity
|
|
|
|
, inputType = TextInput.email
|
|
|
|
, onFocus = "Focused!!!"
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onEnter = "Entered!!!"
|
|
|
|
}
|
|
|
|
, toExample
|
|
|
|
{ name = "search"
|
|
|
|
, toString = identity
|
|
|
|
, inputType = TextInput.search
|
|
|
|
, onFocus = "Focused!!!"
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onEnter = "Entered!!!"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-10-28 02:48:04 +03:00
|
|
|
{-| -}
|
|
|
|
type alias State =
|
2021-10-29 00:41:08 +03:00
|
|
|
{ inputValues : Dict Int String
|
2021-10-29 19:54:04 +03:00
|
|
|
, showPassword : Bool
|
2021-10-28 02:48:04 +03:00
|
|
|
, control : Control ExampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-25 21:16:54 +03:00
|
|
|
{-| -}
|
|
|
|
init : State
|
|
|
|
init =
|
2021-10-29 00:41:08 +03:00
|
|
|
{ inputValues = Dict.empty
|
2021-10-29 19:54:04 +03:00
|
|
|
, showPassword = False
|
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
|
2021-10-29 00:41:08 +03:00
|
|
|
, attributes : List (TextInput.Attribute String Msg)
|
2021-11-02 22:23:54 +03:00
|
|
|
, onFocus : Bool
|
2021-10-28 02:48:04 +03:00
|
|
|
, onBlur : Bool
|
|
|
|
, onEnter : Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
initControl : Control ExampleConfig
|
|
|
|
initControl =
|
|
|
|
Control.record ExampleConfig
|
|
|
|
|> Control.field "label" (Control.string "Assignment name")
|
|
|
|
|> Control.field "attributes" controlAttributes
|
2021-11-02 22:23:54 +03:00
|
|
|
|> Control.field "onFocus" (Control.bool False)
|
2021-10-28 02:51:44 +03:00
|
|
|
|> Control.field "onBlur" (Control.bool False)
|
|
|
|
|> Control.field "onEnter" (Control.bool False)
|
2021-10-28 02:48:04 +03:00
|
|
|
|
|
|
|
|
2021-10-29 00:41:08 +03:00
|
|
|
controlAttributes : Control (List (TextInput.Attribute value msg))
|
2021-10-28 02:28:46 +03:00
|
|
|
controlAttributes =
|
|
|
|
ControlExtra.list
|
2021-10-28 02:51:44 +03:00
|
|
|
|> ControlExtra.optionalListItem "placeholder"
|
2021-10-28 02:28:46 +03:00
|
|
|
(Control.map TextInput.placeholder <|
|
|
|
|
Control.string "Learning with commas"
|
|
|
|
)
|
2021-10-28 02:51:44 +03:00
|
|
|
|> ControlExtra.optionalListItem "hiddenLabel"
|
2021-10-28 02:28:46 +03:00
|
|
|
(Control.value TextInput.hiddenLabel)
|
2021-10-28 02:51:44 +03:00
|
|
|
|> ControlExtra.optionalListItem "errorIf"
|
2021-10-28 02:28:46 +03:00
|
|
|
(Control.map TextInput.errorIf <| Control.bool True)
|
2021-10-28 02:51:44 +03:00
|
|
|
|> ControlExtra.optionalListItem "errorMessage"
|
2021-10-29 21:14:49 +03:00
|
|
|
(Control.map (Just >> TextInput.errorMessage) <| Control.string "The statement must be true.")
|
|
|
|
|> ControlExtra.optionalListItem "guidance"
|
|
|
|
(Control.map TextInput.guidance <| Control.string "The statement must be true.")
|
2021-10-28 02:51:44 +03:00
|
|
|
|> ControlExtra.optionalListItem "disabled"
|
2021-10-28 02:28:46 +03:00
|
|
|
(Control.value TextInput.disabled)
|
2021-10-28 02:51:44 +03:00
|
|
|
|> ControlExtra.optionalListItem "loading"
|
2021-10-28 02:28:46 +03:00
|
|
|
(Control.value TextInput.loading)
|
2021-10-28 02:59:33 +03:00
|
|
|
|> ControlExtra.optionalListItem "writing"
|
|
|
|
(Control.value TextInput.writing)
|
2021-10-28 02:51:44 +03:00
|
|
|
|> ControlExtra.listItem "noMargin"
|
2021-10-28 02:28:46 +03:00
|
|
|
(Control.map TextInput.noMargin (Control.bool False))
|
2021-10-28 02:51:44 +03:00
|
|
|
|> ControlExtra.optionalListItem "css"
|
2021-10-28 02:50:07 +03:00
|
|
|
(Control.value (TextInput.css [ Css.backgroundColor Colors.azure ]))
|
2021-10-28 02:28:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
{-| -}
|
|
|
|
type Msg
|
2021-10-29 00:41:08 +03:00
|
|
|
= SetInput Int String
|
2021-10-29 19:54:04 +03:00
|
|
|
| SetShowPassword Bool
|
2021-10-28 02:28:46 +03:00
|
|
|
| UpdateControl (Control ExampleConfig)
|
|
|
|
|
|
|
|
|
2018-05-25 21:16:54 +03:00
|
|
|
{-| -}
|
|
|
|
update : Msg -> State -> ( State, Cmd Msg )
|
|
|
|
update msg state =
|
|
|
|
case msg of
|
2021-10-29 00:41:08 +03:00
|
|
|
SetInput id string ->
|
|
|
|
( { state | inputValues = Dict.insert id string state.inputValues }
|
|
|
|
, Cmd.none
|
|
|
|
)
|
2021-09-29 01:49:36 +03:00
|
|
|
|
2021-10-29 19:54:04 +03:00
|
|
|
SetShowPassword showPassword ->
|
|
|
|
( { state | showPassword = showPassword }
|
|
|
|
, Cmd.none
|
|
|
|
)
|
|
|
|
|
2020-03-31 23:04:36 +03:00
|
|
|
UpdateControl newControl ->
|
2021-10-29 00:41:08 +03:00
|
|
|
( { state | control = newControl }
|
|
|
|
, Cmd.none
|
|
|
|
)
|