purescript-halogen-formless icon indicating copy to clipboard operation
purescript-halogen-formless copied to clipboard

Migrate to Halogen Hooks version

Open JordanMartinez opened this issue 5 years ago • 2 comments

... and potentially fix #61, #62, and #64 along the way.

I think the hook version of this library could be broken up into two parts: various hooks for fields and a hook for the form itself

A field hook would have the following things:

  • a validator
  • an optional debouncer (not all require this)
  • an optional default value (i.e. fix #61)
  • two variants: one for single-elements and another for multi-elements (i.e. fix #62) where one debouncer is used for each one (i.e. fix #64), but the same validation and default value would be used across all of them

I'm not sure what a form hook would have as I can't recall all the things this library provides.

JordanMartinez avatar Apr 06 '20 13:04 JordanMartinez

So, here's a problem.

A given field has the following pieces of state:

  • input
  • touched
  • output (i.e. the value when successfully validated)
  • dirty

If I want to render a dynamically-sized array of fields, I would idealy write something like this:

allFields <- for arrayOfItems \item -> useField item

However, for/traverse and indexed monads don't play well together.

So, the only workaround I can think of is to create a variant of useField that does something like this:

useManyFields array = Hooks.do
  inputs /\ tInputs <- useState (map (_.input) array)
  toucheds /\ tToucheds <- useState (map (\_ -> false) array)
  valids /\ tValids <- useState (map (\_ -> NotValidated) array)
  dirtys /\ tDirtys <- useState (map (\_ -> false) array)

Getting. setting, or modifying the corresponding value in the corresponding array will require using lenses to prevent verbosity

JordanMartinez avatar Apr 08 '20 03:04 JordanMartinez

While I am open to a Hooks version of Formless, I'm not currently planning to work on this. I did implement a working Hooks version of Formless, but I never felt satisfied with its ergonomics. You can see that work here:

https://github.com/thomashoneyman/purescript-halogen-formless/tree/hooks

In addition, others have updated that code for 0.14:

https://github.com/thomashoneyman/purescript-halogen-formless/pull/71 https://github.com/thomashoneyman/purescript-halogen-formless/pull/72

If someone would like to continue on this direction, please do! But I am not personally planning to work on it.

thomashoneyman avatar Mar 03 '22 21:03 thomashoneyman