final-form / react-final-form

🏁 High performance subscription-based form state management for React
https://final-form.org/react
MIT License
7.39k stars 481 forks source link

useFieldSubscription #808

Open theKashey opened 4 years ago

theKashey commented 4 years ago

Are you submitting a bug report or a feature request?

Feature request

What is the current behavior?

The current expectation, not a behaviour, is that Field or useField would be used with some "real" DOM element.

That's working for the majority of cases, but not for all. For example, I could have a "composite" input without a fully "DOM-compatible" on change event:

What is the expected behavior?

A good solution would be a useFieldSubscription hook (which useField could use underneath), to provide a real "low level" API to the final-form, without all the React and DOM sugar added by this package, but still with a proper initialization.

πŸ‘‰ useFieldSubscription is already written, here it is - https://github.com/final-form/react-final-form/blob/master/src/useField.js#L53-L131 It just has to be extracted to a separate hook.

erikras commented 4 years ago

Surely it would be called useFieldSubscription, right?

What is the shape of what it returns?

theKashey commented 4 years ago

πŸ˜… sure it's about field, and the shape is the same as useField, just without "react taste"

export type FieldSubscriptionProps<T=string> = {
  name: string,
  onBlur: () => void,
  onChange: (?T) => void,
  onFocus: () => void,
  value: any,
  type?: string,
  checked?: boolean,
  multiple?: boolean
}
export type FieldSubscriptionRenderProps = {
  input: FieldSubscriptionProps,
  meta: {
    /* is the same */
  }
}

In this case useField could utilise useFieldSubscription and just shadow onChange with the own "event payload unwrapper".