ghcjs / ghcjs-vdom

bindings for virtual-dom
MIT License
33 stars 16 forks source link

A way to access form element values #10

Open eamsden opened 9 years ago

eamsden commented 9 years ago

There is presently no way to access the value of a form element in ghcjs-vdom.

Some ideas considered:

Bundle the value with the change event

This is promising but requires some work to ensure that the value accessor on the event type is referentially transparent. I.e. if a second change event were to fire and the accessor were then called on the Event value handed to the callback the first time, the accessor would return the new value, unless some caching were implemented.

In addition, this requires users of ghcjs-vdom to save the values from change events so that the value can be accessed when desired

Use a Hook to get access to the actual DOM element

virtual-dom allows VNodes to contain Hooks, which are callbacks that are called with the actual DOM element when the VNode is rendered. The main issue with this approach is the question of how to tie a reference to this element back into a change event handler. The change event handler must be attached to the VNode prior to rendering (and a VNode may appear multiple places in the virtual DOM and thus produce multiple elements when patched) while the Hook does not run until patch time. Some form of wormholing (e.g. setting up an IORef where the DOM element will be used and writing to it in the hook) will probably be necessary.

eamsden commented 9 years ago

One thing to do with a Hook would be to build a function

customForm :: forall a c . (Attributes a, Children c) => JSString -> a -> c -> (JSRef () -> IO ()) -> VNode

where the hook would set up the callback to be fired when the element was created, and thereafter on each 'change' event, with the current value of the value attribute of the element.