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

Textfield should support more DOM event handlers #130

Closed
debois opened this issue Aug 1, 2016 · 11 comments
Closed

Textfield should support more DOM event handlers #130

debois opened this issue Aug 1, 2016 · 11 comments

Comments

@debois
Copy link
Owner

debois commented Aug 1, 2016

From @OvermindDL1 at https://groups.google.com/d/msg/elm-discuss/pNPVxrz_jlE/z_zaaattAwAJ :

Do not see an example or demonstration code that shows this, but how can I get an Enter keypress from a single line Textfield, and how can I distinguish it from a Shift+Enter keypress in a multiline Textfield, or how can I get the event data?

@OvermindDL1
Copy link
Contributor

Yeah there does not seem to be a way to set generic on handlers like in Html.Events, and it is playing havoc with my ability to get data...

@vipentti
Copy link
Collaborator

vipentti commented Aug 1, 2016

@OvermindDL1 I'm currently working on this; Are you simply looking for onEnter or would onKeyUp work? What kind of use case are you looking at ?

Currently thinking something like

-- Event
type alias KeyEvent =
  { shift : Bool
  , keyCode : Int
  }

-- In your own update Add event listener 
  | KeyUp Textfield.KeyEvent

-- To component as well 
   Textfield.onKeyUp KeyUp

-- Then in update 

  KeyUp { shift, keyCode } ->
      case keyCode of 
        13 -> if shift then "shift + enter" else "just enter"
        _ -> ""

This does not stopPropagation and does not preventDefaults so if you want to actually intercept enter and prevent that from adding new lines that does require some extra functionality.

@OvermindDL1
Copy link
Contributor

Something like onKeyUp would be perfect, I do have plans for some extra things later. I would especially love if the KeyEvent also had a selection range, something like:

type alias KeyEvent =
  { shift : Bool
  , keyCode : Int
  , selection_range : { start : Int, end : Int }
  }

Right now I have to get the selection range onChange via a port, which is hacky since I cannot make my own on events right now to do custom json decoding of the event (would love that!)

@debois
Copy link
Owner Author

debois commented Aug 1, 2016

@vipentti: Is there some way we can avoid duplicating large parts of Html.Event?

@OvermindDL1
Copy link
Contributor

If there is some way in the API to drop a lot of the Material specific html things and use Html.* itself that would be best. Making my own 'on' handlers again would be fantastic.

@vipentti
Copy link
Collaborator

vipentti commented Aug 8, 2016

Support for custom events should be available in the next (minor) version

@OvermindDL1
Copy link
Contributor

Having a way to have custom event handlers on any type would be fantastic though, like normal elm-html. I have to keep changing back to Html stuff and hacking in material things into it for my onScroll events for example. ^.^

@debois
Copy link
Owner Author

debois commented Aug 9, 2016

Fixed in 7.3.0; nice example use here (last example).

@OvermindDL1: Are you aware of Options.attribute? You can use it with Options.div et. al. to install arbitrary attributes on styled Html elements:

    Options.div 
      [ Options.attribute <| Html.onClick MyClickEvent ]
      [ text "I'll know if you click me!" ]

We can't support arbitrary handlers on all components, because user handlers could conflict with internal ones (i.e., buttons need to take care focus/blur internally; if the user sets his own blur handler, they stop working).

@OvermindDL1
Copy link
Contributor

Ah I was wondering what that was for. Is there no way to iterate over the attributes and remove user supplied 'onBlur' and such things, or to hoist them 'into' your callback so both get called? That way getting rid of the Options special stuff entirely?

@vipentti
Copy link
Collaborator

vipentti commented Aug 9, 2016

I had some ideas about adding support for user defined events on a larger scale but haven't had a time to experiment with it yet.

@debois
Copy link
Owner Author

debois commented Aug 9, 2016

That'd be awesome! I'd love to be part of discussing how to do that also, here or on slack :)

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

No branches or pull requests

3 participants