Adds onFocus helper

This commit is contained in:
Tessa Kelly 2021-11-02 12:23:54 -07:00
parent 5557a43013
commit 79468439ba
2 changed files with 29 additions and 4 deletions

View File

@ -2,7 +2,7 @@ module Nri.Ui.TextInput.V7 exposing
( view, generateId ( view, generateId
, number, float, text, newPassword, currentPassword, email, search , number, float, text, newPassword, currentPassword, email, search
, value, map , value, map
, onBlur, onEnter , onFocus, onBlur, onEnter
, Attribute, placeholder, hiddenLabel, autofocus , Attribute, placeholder, hiddenLabel, autofocus
, css, custom, nriDescription, id, testId, noMargin , css, custom, nriDescription, id, testId, noMargin
, disabled, loading, errorIf, errorMessage, guidance , disabled, loading, errorIf, errorMessage, guidance
@ -35,7 +35,7 @@ module Nri.Ui.TextInput.V7 exposing
### Event handlers ### Event handlers
@docs onBlur, onEnter @docs onFocus, onBlur, onEnter
## Attributes ## Attributes
@ -328,6 +328,13 @@ hiddenLabel =
\config -> { config | hideLabel = True } \config -> { config | hideLabel = True }
{-| Causes the TextInput to produce the given `msg` when the field is focused.
-}
onFocus : msg -> Attribute value msg
onFocus msg =
Attribute { emptyEventsAndValues | onFocus = Just msg } identity
{-| Causes the TextInput to produce the given `msg` when the field is blurred. {-| Causes the TextInput to produce the given `msg` when the field is blurred.
-} -}
onBlur : msg -> Attribute value msg onBlur : msg -> Attribute value msg
@ -423,6 +430,7 @@ type alias EventsAndValues value msg =
, toString : Maybe (value -> String) , toString : Maybe (value -> String)
, fromString : Maybe (String -> value) , fromString : Maybe (String -> value)
, onInput : Maybe (String -> msg) , onInput : Maybe (String -> msg)
, onFocus : Maybe msg
, onBlur : Maybe msg , onBlur : Maybe msg
, onEnter : Maybe msg , onEnter : Maybe msg
, floatingContent : Maybe (FloatingContentConfig msg -> Html msg) , floatingContent : Maybe (FloatingContentConfig msg -> Html msg)
@ -434,6 +442,7 @@ emptyEventsAndValues =
{ currentValue = Nothing { currentValue = Nothing
, toString = Nothing , toString = Nothing
, fromString = Nothing , fromString = Nothing
, onFocus = Nothing
, onBlur = Nothing , onBlur = Nothing
, onEnter = Nothing , onEnter = Nothing
, onInput = Nothing , onInput = Nothing
@ -448,6 +457,7 @@ map f toString (Attribute eventsAndValues configF) =
{ currentValue = Maybe.map f eventsAndValues.currentValue { currentValue = Maybe.map f eventsAndValues.currentValue
, toString = Just toString , toString = Just toString
, fromString = Maybe.map (\from -> from >> f) eventsAndValues.fromString , fromString = Maybe.map (\from -> from >> f) eventsAndValues.fromString
, onFocus = eventsAndValues.onFocus
, onBlur = eventsAndValues.onBlur , onBlur = eventsAndValues.onBlur
, onEnter = eventsAndValues.onEnter , onEnter = eventsAndValues.onEnter
, onInput = eventsAndValues.onInput , onInput = eventsAndValues.onInput
@ -528,6 +538,7 @@ applyEvents =
{ currentValue = orExisting .currentValue eventsAndValues existing { currentValue = orExisting .currentValue eventsAndValues existing
, toString = orExisting .toString eventsAndValues existing , toString = orExisting .toString eventsAndValues existing
, fromString = orExisting .fromString eventsAndValues existing , fromString = orExisting .fromString eventsAndValues existing
, onFocus = orExisting .onFocus eventsAndValues existing
, onBlur = orExisting .onBlur eventsAndValues existing , onBlur = orExisting .onBlur eventsAndValues existing
, floatingContent = orExisting .floatingContent eventsAndValues existing , floatingContent = orExisting .floatingContent eventsAndValues existing
, onEnter = orExisting .onEnter eventsAndValues existing , onEnter = orExisting .onEnter eventsAndValues existing
@ -654,6 +665,7 @@ view label attributes =
, Attributes.value stringValue , Attributes.value stringValue
, Attributes.disabled disabled_ , Attributes.disabled disabled_
, maybeAttr Events.onInput eventsAndValues.onInput , maybeAttr Events.onInput eventsAndValues.onInput
, maybeAttr Events.onFocus eventsAndValues.onFocus
, maybeAttr Events.onBlur eventsAndValues.onBlur , maybeAttr Events.onBlur eventsAndValues.onBlur
, Attributes.autofocus config.autofocus , Attributes.autofocus config.autofocus
, maybeAttr type_ config.fieldType , maybeAttr type_ config.fieldType

View File

@ -38,7 +38,7 @@ example =
exampleConfig = exampleConfig =
Control.currentValue state.control Control.currentValue state.control
toExample { name, toString, inputType, onBlur, onEnter } index = toExample { name, toString, inputType, onFocus, onBlur, onEnter } index =
( name ( name
, TextInput.view exampleConfig.label , TextInput.view exampleConfig.label
(exampleConfig.attributes (exampleConfig.attributes
@ -48,7 +48,12 @@ example =
, TextInput.value (Maybe.withDefault "" (Dict.get index state.inputValues)) , TextInput.value (Maybe.withDefault "" (Dict.get index state.inputValues))
] ]
++ List.filterMap identity ++ List.filterMap identity
[ if exampleConfig.onBlur then [ if exampleConfig.onFocus then
Just (TextInput.onFocus (SetInput index onFocus))
else
Nothing
, if exampleConfig.onBlur then
Just (TextInput.onBlur (SetInput index onBlur)) Just (TextInput.onBlur (SetInput index onBlur))
else else
@ -69,6 +74,7 @@ example =
{ name = "text" { name = "text"
, toString = identity , toString = identity
, inputType = TextInput.text , inputType = TextInput.text
, onFocus = "Focused!!!"
, onBlur = "Blurred!!!" , onBlur = "Blurred!!!"
, onEnter = "Entered!!!" , onEnter = "Entered!!!"
} }
@ -76,6 +82,7 @@ example =
{ name = "number" { name = "number"
, toString = Maybe.map String.fromInt >> Maybe.withDefault "" , toString = Maybe.map String.fromInt >> Maybe.withDefault ""
, inputType = TextInput.number , inputType = TextInput.number
, onFocus = "1234"
, onBlur = "10000000" , onBlur = "10000000"
, onEnter = "20000000" , onEnter = "20000000"
} }
@ -83,6 +90,7 @@ example =
{ name = "float" { name = "float"
, toString = Maybe.map String.fromFloat >> Maybe.withDefault "" , toString = Maybe.map String.fromFloat >> Maybe.withDefault ""
, inputType = TextInput.float , inputType = TextInput.float
, onFocus = "123"
, onBlur = "1.00000001" , onBlur = "1.00000001"
, onEnter = "100000001.1" , onEnter = "100000001.1"
} }
@ -96,6 +104,7 @@ example =
, showPassword = state.showPassword , showPassword = state.showPassword
, setShowPassword = SetShowPassword , setShowPassword = SetShowPassword
} }
, onFocus = "Focused!!!"
, onBlur = "Blurred!!!" , onBlur = "Blurred!!!"
, onEnter = "Entered!!!" , onEnter = "Entered!!!"
} }
@ -103,6 +112,7 @@ example =
{ name = "email" { name = "email"
, toString = identity , toString = identity
, inputType = TextInput.email , inputType = TextInput.email
, onFocus = "Focused!!!"
, onBlur = "Blurred!!!" , onBlur = "Blurred!!!"
, onEnter = "Entered!!!" , onEnter = "Entered!!!"
} }
@ -110,6 +120,7 @@ example =
{ name = "search" { name = "search"
, toString = identity , toString = identity
, inputType = TextInput.search , inputType = TextInput.search
, onFocus = "Focused!!!"
, onBlur = "Blurred!!!" , onBlur = "Blurred!!!"
, onEnter = "Entered!!!" , onEnter = "Entered!!!"
} }
@ -138,6 +149,7 @@ init =
type alias ExampleConfig = type alias ExampleConfig =
{ label : String { label : String
, attributes : List (TextInput.Attribute String Msg) , attributes : List (TextInput.Attribute String Msg)
, onFocus : Bool
, onBlur : Bool , onBlur : Bool
, onEnter : Bool , onEnter : Bool
} }
@ -148,6 +160,7 @@ initControl =
Control.record ExampleConfig Control.record ExampleConfig
|> Control.field "label" (Control.string "Assignment name") |> Control.field "label" (Control.string "Assignment name")
|> Control.field "attributes" controlAttributes |> Control.field "attributes" controlAttributes
|> Control.field "onFocus" (Control.bool False)
|> Control.field "onBlur" (Control.bool False) |> Control.field "onBlur" (Control.bool False)
|> Control.field "onEnter" (Control.bool False) |> Control.field "onEnter" (Control.bool False)