Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input can't have onBlur event #148

Open
andrejsm opened this issue Apr 9, 2018 · 2 comments
Open

Input can't have onBlur event #148

andrejsm opened this issue Apr 9, 2018 · 2 comments

Comments

@andrejsm
Copy link

andrejsm commented Apr 9, 2018

The big picture of what I'm trying to do

Setting an onBlur event on an input to perform single field validation and give feedback to a user as early as possible is considered good UX. Unfortunately, currently, Text style variation msg does not allow setting onBlur attribute.

For now, the simples approach seems to add optional onBlur:

 type alias Text style variation msg =
     { onChange : String -> msg
+    , onBlur: Maybe (String -> msg)
     , value : String
     , label : Label style variation msg
     , options : List (Option style variation msg)
     }

If this is ok I can create a PR.

Versions

  • style-elements: 4.3.0
@felixakiragreen
Copy link

felixakiragreen commented Apr 10, 2018

Why don't you try creating your own function that wraps Text?

createTextWithEvents : style -> (String -> msg)
  -> msg
  -> String
  -> Element style variation msg
createTextWithEvents style inputEvent blurEvent value =
  Input.text
    style
    [ onInput inputEvent
    , onBlur blurEvent
    ]
    ( Input.Text
      inputEvent
      value
      (Input.hiddenLabel "")
      []
    )

-- then just use it by:
( createTextWithEvents
  TextInputStyle
  InputEvent
  BlurEvent
  "heyheyhey"
)

I tested it out and I get both input and blur events. Good luck.

@andrejsm
Copy link
Author

andrejsm commented Apr 10, 2018

Thanks, @DUBERT, but unfortunately, that's not what I need. onBlur must be of type String -> msg.
But luckily I stumbled upon on. Didn't see that initially.

Managed to get what I need:

onBlur : (String -> msg) -> Attribute variation msg
onBlur msg =
    on "blur" (JD.map msg targetValue)


-- Usage example:
Input.text None [ onBlur (MyMsg "field")] opts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants