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 (..)
|
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)
|
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
|
2020-04-14 00:28:15 +03:00
|
|
|
import Nri.Ui.TextInput.V6 as TextInput
|
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
|
2021-09-29 01:49:36 +03:00
|
|
|
| SetSearchTerm 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
|
2021-09-29 01:49:36 +03:00
|
|
|
, searchInputValue : String
|
2020-03-31 23:04:36 +03:00
|
|
|
, control : Control ExampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type alias ExampleConfig =
|
2020-04-14 19:00:25 +03:00
|
|
|
{ label : String
|
|
|
|
, maybePlaceholderAttribute : Maybe (TextInput.Attribute Msg)
|
2020-04-14 23:09:38 +03:00
|
|
|
, maybeErrorAttribute1 : Maybe (TextInput.Attribute Msg)
|
|
|
|
, maybeErrorAttribute2 : Maybe (TextInput.Attribute Msg)
|
2020-04-14 19:00:25 +03:00
|
|
|
, maybeShowLabelAttribute : Maybe (TextInput.Attribute Msg)
|
2020-04-14 21:58:59 +03:00
|
|
|
, maybeDisabledAttribute : Maybe (TextInput.Attribute Msg)
|
|
|
|
, maybeLoadingAttribute : Maybe (TextInput.Attribute Msg)
|
2021-09-29 02:08:07 +03:00
|
|
|
, onBlur : Bool
|
2021-09-29 02:10:31 +03:00
|
|
|
, onReset : Bool
|
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"
|
|
|
|
, version = 6
|
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
|
2020-03-31 22:43:32 +03:00
|
|
|
, view =
|
|
|
|
\state ->
|
2020-04-11 01:07:27 +03:00
|
|
|
let
|
|
|
|
exampleConfig =
|
|
|
|
Control.currentValue state.control
|
2021-09-29 01:59:07 +03:00
|
|
|
|
2021-09-29 02:10:31 +03:00
|
|
|
attributes { setField, onBlur, onReset } =
|
2021-09-29 01:59:07 +03:00
|
|
|
List.filterMap identity
|
|
|
|
[ exampleConfig.maybeErrorAttribute1
|
|
|
|
, exampleConfig.maybeErrorAttribute2
|
|
|
|
, exampleConfig.maybePlaceholderAttribute
|
|
|
|
, exampleConfig.maybeShowLabelAttribute
|
|
|
|
, exampleConfig.maybeDisabledAttribute
|
|
|
|
, exampleConfig.maybeLoadingAttribute
|
2021-09-29 02:08:07 +03:00
|
|
|
, if exampleConfig.onBlur then
|
|
|
|
Just (TextInput.onBlur (setField onBlur))
|
|
|
|
|
2021-09-29 02:10:31 +03:00
|
|
|
else
|
|
|
|
Nothing
|
|
|
|
, if exampleConfig.onReset then
|
|
|
|
Just (TextInput.onReset (setField onReset))
|
|
|
|
|
2021-09-29 02:08:07 +03:00
|
|
|
else
|
|
|
|
Nothing
|
2021-09-29 01:59:07 +03:00
|
|
|
]
|
2020-04-11 01:07:27 +03:00
|
|
|
in
|
2020-04-14 20:59:38 +03:00
|
|
|
[ Control.view UpdateControl state.control
|
|
|
|
|> Html.fromUnstyled
|
|
|
|
, Html.div
|
|
|
|
[ css
|
|
|
|
[ property "display" "grid"
|
|
|
|
, property "grid-template-columns" "auto 1fr"
|
|
|
|
, property "grid-gap" "10px"
|
|
|
|
]
|
|
|
|
]
|
|
|
|
[ Heading.h3 [] [ text "TextInput.text" ]
|
2020-04-14 02:43:07 +03:00
|
|
|
, TextInput.view (exampleConfig.label ++ " (text)")
|
|
|
|
(TextInput.text (SetTextInput 1))
|
2021-09-29 02:10:31 +03:00
|
|
|
(attributes
|
|
|
|
{ setField = SetTextInput 1
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onReset = ""
|
|
|
|
}
|
|
|
|
)
|
2020-04-14 02:43:07 +03:00
|
|
|
(Maybe.withDefault "" <| Dict.get 1 state.stringInputValues)
|
2020-04-14 20:59:38 +03:00
|
|
|
, Heading.h3 [] [ text "TextInput.number" ]
|
2020-04-14 02:43:07 +03:00
|
|
|
, TextInput.view (exampleConfig.label ++ " (number)")
|
|
|
|
(TextInput.number SetNumberInput)
|
2021-09-29 02:08:07 +03:00
|
|
|
(TextInput.id "hey-this-is-a-test-id"
|
2021-09-29 02:10:31 +03:00
|
|
|
:: attributes
|
|
|
|
{ setField = SetNumberInput
|
|
|
|
, onBlur = Just 10000000
|
|
|
|
, onReset = Nothing
|
|
|
|
}
|
2021-09-29 02:08:07 +03:00
|
|
|
)
|
2020-04-14 02:43:07 +03:00
|
|
|
state.numberInputValue
|
2020-04-14 20:59:38 +03:00
|
|
|
, Heading.h3 [] [ text "TextInput.float" ]
|
2020-04-14 02:43:07 +03:00
|
|
|
, TextInput.view (exampleConfig.label ++ " (float)")
|
|
|
|
(TextInput.float SetFloatInput)
|
2021-09-29 02:10:31 +03:00
|
|
|
(attributes
|
|
|
|
{ setField = SetFloatInput
|
|
|
|
, onBlur = Just 1.00000001
|
|
|
|
, onReset = Nothing
|
|
|
|
}
|
|
|
|
)
|
2020-04-14 02:43:07 +03:00
|
|
|
state.floatInputValue
|
2020-04-14 20:59:38 +03:00
|
|
|
, Heading.h3 [] [ text "TextInput.password" ]
|
2020-04-14 02:43:07 +03:00
|
|
|
, TextInput.view (exampleConfig.label ++ " (password)")
|
|
|
|
(TextInput.password SetPassword)
|
2021-09-29 02:10:31 +03:00
|
|
|
(attributes
|
|
|
|
{ setField = SetPassword
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onReset = ""
|
|
|
|
}
|
|
|
|
)
|
2020-04-14 02:43:07 +03:00
|
|
|
state.passwordInputValue
|
2020-04-14 20:59:38 +03:00
|
|
|
, Heading.h3 [] [ text "TextInput.email" ]
|
2020-04-14 02:43:07 +03:00
|
|
|
, TextInput.view (exampleConfig.label ++ " (email)")
|
|
|
|
(TextInput.email (SetTextInput 2))
|
2021-09-29 02:10:31 +03:00
|
|
|
(attributes
|
|
|
|
{ setField = SetTextInput 2
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onReset = ""
|
|
|
|
}
|
|
|
|
)
|
2020-04-14 02:43:07 +03:00
|
|
|
(Maybe.withDefault "" <| Dict.get 2 state.stringInputValues)
|
2020-04-14 20:59:38 +03:00
|
|
|
, Heading.h3 [] [ Html.text "TextInput.writing" ]
|
2020-04-14 02:43:07 +03:00
|
|
|
, TextInput.view (exampleConfig.label ++ " (writing)")
|
|
|
|
(TextInput.text (SetTextInput 4))
|
2021-09-29 02:08:07 +03:00
|
|
|
(TextInput.writing
|
2021-09-29 02:10:31 +03:00
|
|
|
:: attributes
|
|
|
|
{ setField = SetTextInput 4
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onReset = ""
|
|
|
|
}
|
2021-09-29 02:08:07 +03:00
|
|
|
)
|
2020-04-14 02:43:07 +03:00
|
|
|
(Maybe.withDefault "" <| Dict.get 4 state.stringInputValues)
|
2021-09-29 01:49:36 +03:00
|
|
|
, Heading.h3 [] [ Html.text "TextInput.search" ]
|
|
|
|
, TextInput.view (exampleConfig.label ++ " (search)")
|
|
|
|
(TextInput.search SetSearchTerm)
|
2021-09-29 02:10:31 +03:00
|
|
|
(attributes
|
|
|
|
{ setField = SetSearchTerm
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onReset = ""
|
|
|
|
}
|
|
|
|
)
|
2021-09-29 01:49:36 +03:00
|
|
|
state.searchInputValue
|
2020-04-14 23:23:55 +03:00
|
|
|
, Heading.h3 [] [ text "TextInput.css" ]
|
|
|
|
, TextInput.view (exampleConfig.label ++ " (custom CSS)")
|
|
|
|
(TextInput.text (SetTextInput 8))
|
2021-09-29 01:59:07 +03:00
|
|
|
(TextInput.css [ Css.backgroundColor Colors.azure ]
|
2021-09-29 02:10:31 +03:00
|
|
|
:: attributes
|
|
|
|
{ setField = SetTextInput 8
|
|
|
|
, onBlur = "Blurred!!!"
|
|
|
|
, onReset = ""
|
|
|
|
}
|
2020-04-14 23:23:55 +03:00
|
|
|
)
|
|
|
|
(Maybe.withDefault "" <| Dict.get 8 state.stringInputValues)
|
2018-10-23 19:55:30 +03:00
|
|
|
]
|
2020-03-31 22:43:32 +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 = ""
|
2021-09-29 01:49:36 +03:00
|
|
|
, searchInputValue = ""
|
2020-03-31 23:04:36 +03:00
|
|
|
, control =
|
|
|
|
Control.record ExampleConfig
|
|
|
|
|> Control.field "label" (Control.string "Assignment name")
|
2020-04-14 23:09:38 +03:00
|
|
|
|> Control.field "TextInput.placeholder"
|
2020-04-14 19:00:25 +03:00
|
|
|
(Control.maybe True <|
|
|
|
|
Control.map TextInput.placeholder <|
|
|
|
|
Control.string "Learning with commas"
|
|
|
|
)
|
2020-04-14 23:09:38 +03:00
|
|
|
|> Control.field "TextInput.hiddenLabel"
|
|
|
|
(Control.maybe False (Control.value TextInput.hiddenLabel))
|
|
|
|
|> Control.field "TextInput.errorIf"
|
|
|
|
(Control.maybe False (Control.map TextInput.errorIf <| Control.bool True))
|
|
|
|
|> Control.field "TextInput.errorMessage"
|
|
|
|
(Control.maybe False (Control.map TextInput.errorMessage <| Control.maybe True <| Control.string "The statement must be true."))
|
|
|
|
|> Control.field "TextInput.disabled"
|
|
|
|
(Control.maybe False (Control.value TextInput.disabled))
|
|
|
|
|> Control.field "TextInput.loading"
|
|
|
|
(Control.maybe False (Control.value TextInput.loading))
|
2021-09-29 02:08:07 +03:00
|
|
|
|> Control.field "TextInput.onBlur"
|
|
|
|
(Control.bool False)
|
2021-09-29 02:10:31 +03:00
|
|
|
|> Control.field "TextInput.onReset"
|
|
|
|
(Control.bool False)
|
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 )
|
|
|
|
|
2021-09-29 01:49:36 +03:00
|
|
|
SetSearchTerm searchInputValue ->
|
|
|
|
( { state | searchInputValue = searchInputValue }, 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
|