react-minimalistic-use-form
Version:
Minimalistic react hook for handling forms without much pain.
24 lines (23 loc) • 1.86 kB
TypeScript
import { ISetNativeValue, IUseForm, IuseFormResponse } from './index';
/**
*
* This function is used on <resetForm>. The function updates the field value natively with initialValue
* provided either via initialValues or custom one in case of custom controlled input.
* Since Events (change | click) must be fired manually in order to call event handlers
* of parent component that controls the input we have to update value via setter function
* which will be picked up by input event handler attached as element attribute callback
*
* Why? Because React tracks when you set the value property on an input to keep track of the node's value.
* When you dispatch a change event, it checks it's last value against the current value
* (https://github.com/facebook/react/blob/dd5fad29616f706f484938663e93aaadd2a5e594/packages/react-dom/src/client/inputValueTracking.js#L129)
* and if they're the same it does not call any event handlers (as no change has taken place as far as react is concerned).
* So we have to set the value in a way that React's value setter function
* (https://github.com/facebook/react/blob/dd5fad29616f706f484938663e93aaadd2a5e594/packages/react-dom/src/client/inputValueTracking.js#L78-L81)
* will not be called, which is where the setNativeValue comes into play.
* This function was a team effort: https://github.com/facebook/react/issues/10135#issuecomment-401496776
* @param {Node} element
* @param {String} attributeToUpdate
* @param {String | Boolean} value
*/
export declare const setNativeValue: ({ element, attributeToUpdate, value, }: ISetNativeValue) => void;
export declare const useForm: ({ initialValues, errorClassName, touchedClassName, scrollToError, scrollToErrorOptions, validateOnInput, validateOnSubmit, validateOnMount, debounceValidation, debounceTime, plugins, }?: IUseForm) => IuseFormResponse;