joelburget / react-haskell

React bindings for Haskell
MIT License
351 stars 26 forks source link

finish event type definitions #2

Open joelburget opened 9 years ago

joelburget commented 9 years ago

What types of events are there? Currently mouse, keyboard, change, focus.

What properties do they all hold? How do properties map from js to haskell?

commandodev commented 9 years ago

You could have a look at @ocharles bindings to virtual-dom for some inspiration here. If a callback is JSRef Event -> IO () you can have a function like this one from virtual dom:

on :: MonadState HTMLElement m => JSString -> (JSRef Event -> IO ()) -> m ()

Giving you the following on* functions:

https://github.com/boothead/oHm/blob/master/src/Ohm/HTML.hs#L82

onInput :: MonadState HTMLElement m => DOMEvent String -> m ()
onInput chan = on "input" f
  where
  f evt = do
    t <- fromJSRef evt
    for_ t (\t' -> do
      t'' <- eventGetTarget t'
      for_ t''
        (htmlInputElementGetValue .
         castToHTMLInputElement >=> (channel chan)))

onKeyPress :: MonadState HTMLElement m => DOMEvent Int -> m ()
onKeyPress (DOMEvent chan) = on "keypress" f
  where
  f evt = do
    t <- fromJSRef evt
    for_ t
         (uiEventGetKeyCode .
          -- A little messy, but we're working with a dom-delegator 'KeyEvent' here.
          (unsafeCastGObject :: GObject -> UIEvent) .
           toGObject >=> chan)

DOMEvent is from here: https://github.com/boothead/oHm/blob/master/src/Ohm/DOMEvent.hs

I use DOMEvent as the entry point to stuff messages into a channel, with Contravariant instance use to convert the information coming out of the handler to an Event type.

Note that all the above requires dom-delegator.