gform-react 2.5.3 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/cjs/gform-react.development.js +97 -82
  2. package/dist/cjs/gform-react.development.js.map +1 -1
  3. package/dist/cjs/gform-react.production.js +1 -1
  4. package/dist/cjs/gform-react.production.js.map +1 -1
  5. package/dist/cjs/index.js +8 -7
  6. package/dist/esm/GForm.development.js +160 -0
  7. package/dist/esm/GForm.development.js.map +1 -0
  8. package/dist/esm/GForm.production.js +1 -1
  9. package/dist/esm/GForm.production.js.map +1 -1
  10. package/dist/esm/GInput.development.js +105 -0
  11. package/dist/esm/GInput.development.js.map +1 -0
  12. package/dist/esm/GInput.production.js +1 -1
  13. package/dist/esm/GInput.production.js.map +1 -1
  14. package/dist/esm/GValidator.development.js +118 -0
  15. package/dist/esm/GValidator.development.js.map +1 -0
  16. package/dist/esm/GValidator.production.js +1 -1
  17. package/dist/esm/GValidator.production.js.map +1 -1
  18. package/dist/esm/index.development.js +4 -761
  19. package/dist/esm/index.js +4 -4
  20. package/dist/esm/shared.development.js +411 -0
  21. package/dist/esm/shared.development.js.map +1 -0
  22. package/dist/esm/shared.production.js +1 -1
  23. package/dist/esm/shared.production.js.map +1 -1
  24. package/dist/esm/useFormSelector.development.js +6 -0
  25. package/dist/esm/useFormSelector.development.js.map +1 -0
  26. package/dist/esm/useFormSelector.production.js +2 -0
  27. package/dist/esm/useFormSelector.production.js.map +1 -0
  28. package/dist/index.d.ts +260 -290
  29. package/native/dist/cjs/{gform-react-native.development.js → gform-react.development.js} +94 -79
  30. package/native/dist/cjs/gform-react.development.js.map +1 -0
  31. package/native/dist/cjs/gform-react.production.js +2 -0
  32. package/native/dist/cjs/gform-react.production.js.map +1 -0
  33. package/native/dist/cjs/index.js +8 -7
  34. package/native/dist/esm/RNGForm.development.js +105 -0
  35. package/native/dist/esm/RNGForm.development.js.map +1 -0
  36. package/native/dist/esm/RNGForm.production.js +1 -1
  37. package/native/dist/esm/RNGForm.production.js.map +1 -1
  38. package/native/dist/esm/RNGInput.development.js +83 -0
  39. package/native/dist/esm/RNGInput.development.js.map +1 -0
  40. package/native/dist/esm/RNGInput.production.js +1 -1
  41. package/native/dist/esm/RNGInput.production.js.map +1 -1
  42. package/native/dist/esm/index.development.js +2 -592
  43. package/native/dist/esm/index.js +2 -2
  44. package/native/dist/esm/shared.development.js +431 -0
  45. package/native/dist/esm/shared.development.js.map +1 -0
  46. package/native/dist/esm/shared.production.js +1 -1
  47. package/native/dist/esm/shared.production.js.map +1 -1
  48. package/native/dist/index.d.ts +183 -73
  49. package/package.json +10 -7
  50. package/dist/esm/index.development.js.map +0 -1
  51. package/native/dist/cjs/gform-react-native.development.js.map +0 -1
  52. package/native/dist/cjs/gform-react-native.production.js +0 -2
  53. package/native/dist/cjs/gform-react-native.production.js.map +0 -1
  54. package/native/dist/esm/index.development.js.map +0 -1
  55. package/native/package.json +0 -15
package/dist/index.d.ts CHANGED
@@ -1,290 +1,260 @@
1
- import type {
2
- Dispatch,
3
- HTMLAttributes,
4
- ReactNode,
5
- SetStateAction,
6
- HTMLInputTypeAttribute,
7
- FocusEvent,
8
- InvalidEvent,
9
- FormEventHandler,
10
- ChangeEventHandler,
11
- FocusEventHandler,
12
- EventHandler,
13
- SyntheticEvent,
14
- ChangeEvent,
15
- FormEvent,
16
- InputHTMLAttributes,
17
- FC,
18
- ClipboardEvent,
19
- RefObject, DetailedHTMLProps, FormHTMLAttributes, Ref
20
- } from "react";
21
-
22
- export type RawData<T> = {
23
- [key in keyof T]: T[key];
24
- };
25
-
26
- export type ToRawDataOptions<T> = ToURLSearchParamsOptions<T>;
27
- export type ToFormDataOptions<T> = ToURLSearchParamsOptions<T>;
28
- export type ToURLSearchParamsOptions<T> = {
29
- exclude?: (keyof T)[];
30
- include?: (keyof T)[];
31
- transform?: {
32
- [key in keyof T]?: (value: GFormState<T>[key]['value']) => any;
33
- };
34
- };
35
-
36
- export type GConstraintValidator = (input: GInputState<any>) => string;
37
- export type GConstraintValidatorHandler = (input: GInputState<any>, validityKey: keyof ValidityState | undefined) => boolean;
38
- export type GCustomValidatorHandler<T> = (input: GInputState<any>, fields: IForm<T>) => RegExp | boolean;
39
- export type GCustomValidatorHandlerAsync<T> = (input: GInputState<any>, fields: IForm<T>) => Promise<ReturnType<GCustomValidatorHandler<T>>>;
40
- export type GInputValidator<T> = GValidator<T> | {
41
- handlers: GCustomValidatorHandler<T>[];
42
- constraintHandlers: GConstraintValidatorHandler[];
43
- asyncHandlers: GCustomValidatorHandlerAsync<T>[];
44
- };
45
- export type GValidators<T = any> = {
46
- [key in keyof T]?: GInputValidator<T>;
47
- } & {
48
- [key: string]: GInputValidator<T> | undefined;
49
- };
50
-
51
- export type BaseGenericFieldProps = {
52
- /** the key which is used to identify the input */
53
- formKey: string;
54
- /** refer to another input validator */
55
- validatorKey?: string;
56
- type?: HTMLInputTypeAttribute | undefined;
57
-
58
- required?: boolean;
59
- max?: number;
60
- maxLength?: number;
61
- min?: number;
62
- minLength?: number;
63
- checked?: boolean;
64
- step?: number;
65
- pattern?: string | RegExp;
66
- };
67
-
68
- export type GElementProps<T> = Omit<InputHTMLAttributes<any>, 'color' | 'size' | 'onChange' | 'min' | 'max' | 'step'> & {
69
- value: T;
70
- step?: number
71
- max?: number;
72
- min?: number;
73
- };
74
-
75
- type PartialPick<T, P extends keyof T> = Omit<T, P> & Partial<Pick<T, P>>;
76
-
77
- export type PartialForm<T> = Partial<{ [key in keyof T]: Partial<GInputStateMutable<T[key]>> }>;
78
-
79
- export type IForm<T=any> = {
80
- [key in keyof T]: PartialPick<GInputState<T[key]>, 'checkValidity'>;
81
- };
82
-
83
- export type GDOMElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
84
-
85
- export type GFormEvent<T = GDOMElement> = FormEvent<T>;
86
- export type GChangeEvent<T = GDOMElement> = ChangeEvent<T> & {value?: unknown};
87
- export type GFocusEvent<T = GDOMElement> = FocusEvent<T>;
88
- export type GInvalidEvent <T = GDOMElement> = InvalidEvent<T>;
89
-
90
- export type GFormEventHandler<T = GDOMElement> = FormEventHandler<T>;
91
- export type GChangeEventHandler<T = GDOMElement> = ChangeEventHandler<T>;
92
- export type GFocusEventHandler<T = GDOMElement> = FocusEventHandler<T>;
93
- export type GInvalidEventHandler<T = GDOMElement> = EventHandler<SyntheticEvent<T>>;
94
-
95
- export type GInputInitialState = BaseGenericFieldProps & {
96
- [key: string]: any;
97
- gid: string;
98
- dirty: boolean;
99
- touched: boolean;
100
- };
101
-
102
- export type GInputState<T = string | number | boolean> = GInputInitialState & {
103
- value: T;
104
- error: boolean;
105
- errorText: string;
106
- checkValidity(): boolean;
107
- dispatchChanges<C>(changes: Partial<GInputState | C>): void;
108
- debounce?: number;
109
- };
110
-
111
- export type GInputStateMutable<T = any> = GInputState<T> & {
112
- max?: number;
113
- maxLength?: number;
114
- min?: number;
115
- minLength?: number;
116
- checked?: boolean;
117
- step?: number;
118
- pattern?: string | RegExp;
119
- [key: string]: any;
120
- };
121
-
122
- type GInputElementHandler<T> = (input: GInputStateMutable<T>, props: GElementProps<T>) => JSX.Element;
123
-
124
- export type GInputProps = BaseGenericFieldProps & Omit<InputHTMLAttributes<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>, 'value' | 'step' | 'min' | 'max' | 'minLength' | 'maxLength'> & {
125
- defaultChecked?: boolean;
126
- defaultValue?: string | number;
127
- checked?: boolean;
128
- /**a function that gets called once the input has initialized and may have deps (see `fetchDeps`) */
129
- fetch?: (state: GInputStateMutable, fields: IForm & { [key: string]: GInputStateMutable }) => void | Partial<GInputStateMutable> | Promise<void | Partial<GInputStateMutable>>;
130
- /**an array of input keys that once one the values have changed `fetch` will re-run */
131
- fetchDeps?: string[];
132
- /**
133
- * specify the debounce amount for validations in milliseconds
134
- * @default 300
135
- * */
136
- debounce?: number;
137
- optimized?: boolean;
138
- } & ({
139
- type?: 'text' | 'password' | 'email' | 'tel' | 'url' | 'search';
140
- minLength?: number;
141
- maxLength?: number;
142
- pattern?: string;
143
- value?: string;
144
- defaultValue?: string;
145
- /**a handler fer rendering the input
146
- * @param input the current state of the input
147
- * @param props the props of the input should be spread to the dom element
148
- * @example <input {...props} /> //native
149
- * <InputText {...props} /> //Prime React
150
- * <TextField inputProps={props} /> //MUI
151
- */
152
- element?: GInputElementHandler<string>;
153
- }
154
- |
155
- {
156
- type: 'checkbox';
157
- checked?: boolean;
158
- value?: boolean;
159
- defaultChecked?: boolean;
160
- element?: GInputElementHandler<boolean>;
161
- }
162
- |
163
- {
164
- type: 'color';
165
- value?: string;
166
- defaultValue?: string;
167
- element?: GInputElementHandler<string>;
168
- }
169
- |
170
- {
171
- type: 'number' | 'range';
172
- value?: number;
173
- defaultValue?: number;
174
- min?: number;
175
- max?: number;
176
- step?: number;
177
- element?: GInputElementHandler<number>;
178
- }
179
- |
180
- {
181
- type: 'time' | 'week' | 'date' | 'month' | 'datetime-local';
182
- value?: string;
183
- defaultValue?: string;
184
- min?: number;
185
- max?: number;
186
- step?: number;
187
- element?: GInputElementHandler<string>;
188
- }
189
- |
190
- {
191
- type: 'file';
192
- defaultValue?: string;
193
- value?: string;
194
- element?: GInputElementHandler<string>;
195
- });
196
-
197
- export declare const GInput: FC<GInputProps & {ref?: Ref<HTMLElement | null>}>;
198
-
199
- export type GFormState<T> = IForm<T>
200
- &
201
- {
202
- /**indicates whether the form is valid */
203
- isValid: boolean;
204
- /**indicates whether the form is invalid */
205
- isInvalid: boolean;
206
- /**returns an object with key value pairs represents the form*/
207
- toRawData(options?: ToRawDataOptions<T>): RawData<T>;
208
- /**returns `FormData` instance represents the form*/
209
- toFormData(options?: ToFormDataOptions<T>): FormData;
210
- /**returns `URLSearchParams` instance represents the form*/
211
- toURLSearchParams(options?: ToURLSearchParamsOptions<T>): URLSearchParams;
212
- /**update the validity state (invokes all defined validators) */
213
- checkValidity(): boolean;
214
- /**manually dispatch any changes to input(s) */
215
- dispatchChanges: (changes: PartialForm<T> & { [key: string]: Partial<GInputState<any>> }) => void;
216
- };
217
-
218
- /**a class for handling validations for input(s)
219
- * @example
220
- * const baseValidator = new GValidator().withRequiredMessage('this field is required');
221
- *
222
- * const validators: GValidators<SignInForm> = {
223
- * username: new GValidator(baseValidator).withMinLengthMessage('...'),
224
- * '*': baseValidator // a default validator for all other fields in the form
225
- * };
226
- */
227
- export declare class GValidator<T = any> {
228
- get handlers(): GCustomValidatorHandler<T>[];
229
- get constraintHandlers(): GConstraintValidatorHandler[];
230
- get asyncHandlers(): GCustomValidatorHandlerAsync<T>[];
231
- constructor(baseValidator?: GValidator<T>);
232
- /**register a `valueMissing` violation handler (use this with `required` attribute) */
233
- withRequiredMessage(message: string | GConstraintValidator): GValidator<T>;
234
- /**register a `tooLong` violation handler (use this with `maxLength` attribute) */
235
- withMaxLengthMessage(message: string | GConstraintValidator): GValidator<T>;
236
- /**register a `tooShort` violation handler (use this with `minLength` attribute)*/
237
- withMinLengthMessage(message: string | GConstraintValidator): GValidator<T>;
238
- /**register a `patternMismatch` violation handler (use this with `pattern` attribute)*/
239
- withPatternMismatchMessage(message: string | GConstraintValidator): GValidator<T>;
240
- /**register a `badInput` violation handler */
241
- withBadInputMessage(message: string | GConstraintValidator): GValidator<T>;
242
- /**register a `rangeUnderflow` violation handler (use this with `min` attribute) */
243
- withRangeUnderflowMessage(message: string | GConstraintValidator): GValidator<T>;
244
- /**register a `rangeOverflow` violation handler (use this with `max` attribute) */
245
- withRangeOverflowMessage(message: string | GConstraintValidator): GValidator<T>;
246
- /**
247
- * register a `typeMismatch` violation handler<br />
248
- * if its possible use `pattern` attribute (and `withPatternMismatchMessage`) or `custom validation` instead.<br/>
249
- * use the `type` attribute to set the input's keyboard (for example type `'tel'` will show on mobile phones only numpads)
250
- * and then with `pattern` or `custom validation` you can validate it.<br/>
251
- * the reason for that is `type` is not a solid validation and likely will be replaced anyway.<br />
252
- * if `pattern` or `custom` are used, then `withTypeMismatchMessage` is ignored
253
- * */
254
- withTypeMismatchMessage(message: string | GConstraintValidator): GValidator<T>;
255
- /**register a `stepMismatch` violation handler (use this with `step` attribute)*/
256
- withStepMismatchMessage(message: string | GConstraintValidator): GValidator<T>;
257
- /**register a custom validation handler */
258
- withCustomValidation(handler: GCustomValidatorHandler<T>): GValidator<T>;
259
- /**register a custom validation async handler */
260
- withCustomValidationAsync(handler: GCustomValidatorHandlerAsync<T>): GValidator<T>;
261
- }
262
-
263
- export type GFormProps<T> = Omit<DetailedHTMLProps<FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, 'onSubmit' | 'onPaste' | 'onChange' | 'children'> & {
264
- children?: ReactNode | ReactNode[] | ((state: GFormState<T>) => ReactNode | ReactNode[]);
265
- /** @param stateRef - pass a ref which will points to the current state of the form (optional). */
266
- stateRef?: RefObject<GFormState<T> | undefined>;
267
- /** @param onSubmit - a handler for the form submission (optional). */
268
- onSubmit?: (state: GFormState<T>, e: FormEvent<HTMLFormElement>) => void;
269
- /** @param onChange - register onChange handler (optional). */
270
- onChange?: (state: GFormState<T>, e: FormEvent<HTMLFormElement>) => void;
271
- /** @param onChange - register onChange handler (optional). */
272
- onPaste?: (state: GFormState<T>, e: ClipboardEvent<HTMLFormElement>) => void;
273
- /** @param validators - an object for handling validations (optional). */
274
- validators?: GValidators<T>;
275
- /** @param onInit - execute a handler once the form has initialized (optional). */
276
- onInit?: (state: GFormState<T>) => void | PartialForm<T> | Promise<void | PartialForm<T>>;
277
- /** @param optimized - enable optimization by registering the required handlers on the form itself.
278
- * @see {@link https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_delegation|EventDelegation}
279
- * @optional
280
- */
281
- optimized?: boolean;
282
- ref?: Ref<HTMLFormElement | null>;
283
- };
284
-
285
- /**
286
- * build dynamic forms with validations.
287
- * @link Docs - https://gform-react.onrender.com
288
- * @link Npm - https://www.npmjs.com/package/gform-react
289
- */
290
- export const GForm: <T extends any>({stateRef,onSubmit,onChange,children,validators,onInit,optimized,...rest}: GFormProps<T>) => JSX.Element;
1
+ import React, { HTMLInputTypeAttribute, InputHTMLAttributes, ReactNode, DetailedHTMLProps, FormHTMLAttributes, RefObject, FormEvent, ClipboardEvent, KeyboardEvent } from 'react';
2
+
3
+ declare const GInput: React.NamedExoticComponent<GInputProps & React.RefAttributes<HTMLInputElement>>;
4
+
5
+ type BaseGenericFieldProps = {
6
+ /** the key which is used to identify the input */
7
+ formKey: string;
8
+ /** refer to another input validator */
9
+ validatorKey?: string;
10
+ type?: HTMLInputTypeAttribute | undefined;
11
+ required?: boolean;
12
+ };
13
+ type GElementProps<T> = Omit<InputHTMLAttributes<any>, 'color' | 'size' | 'onChange' | 'min' | 'max' | 'step'> & {
14
+ value: T;
15
+ step?: number;
16
+ max?: number;
17
+ min?: number;
18
+ };
19
+ type GInputInitialState = BaseGenericFieldProps & {
20
+ [key: string]: any;
21
+ gid: string;
22
+ dirty: boolean;
23
+ touched: boolean;
24
+ };
25
+ type GInputState<T = string | number | boolean> = GInputInitialState & {
26
+ value: T;
27
+ error: boolean;
28
+ errorText: string;
29
+ checkValidity(): boolean;
30
+ dispatchChanges<C>(changes: Partial<GInputState | C>): void;
31
+ debounce?: number;
32
+ };
33
+ type GInputStateMutable<T = any> = GInputState<T> & {
34
+ max?: number;
35
+ maxLength?: number;
36
+ min?: number;
37
+ minLength?: number;
38
+ checked?: boolean;
39
+ step?: number;
40
+ pattern?: string | RegExp;
41
+ [key: string]: any;
42
+ };
43
+ type GInputElementHandler<T> = (input: GInputState<T>, props: GElementProps<T>) => ReactNode;
44
+ type GInputProps = BaseGenericFieldProps & Omit<InputHTMLAttributes<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>, 'value' | 'step' | 'min' | 'max' | 'minLength' | 'maxLength'> & {
45
+ defaultChecked?: boolean;
46
+ defaultValue?: string | number;
47
+ checked?: boolean;
48
+ /**a function that gets called once the input has initialized and may have deps (see `fetchDeps`) */
49
+ fetch?: (state: GInputStateMutable, fields: IForm & {
50
+ [key: string]: GInputStateMutable;
51
+ }) => void | Partial<GInputStateMutable> | Promise<void | Partial<GInputStateMutable>>;
52
+ /**an array of input keys that once one the values have changed `fetch` will re-run */
53
+ fetchDeps?: string[];
54
+ /**
55
+ * specify the debounce amount for validations in milliseconds
56
+ * @default 300
57
+ * */
58
+ debounce?: number;
59
+ optimized?: boolean;
60
+ } & ({
61
+ type?: 'text' | 'password' | 'email' | 'tel' | 'url' | 'search';
62
+ minLength?: number;
63
+ maxLength?: number;
64
+ pattern?: string;
65
+ value?: string;
66
+ defaultValue?: string;
67
+ /**a handler fer rendering the input
68
+ * @param input the current state of the input
69
+ * @param props the props of the input should be spread to the dom element
70
+ * @example <input {...props} /> //native
71
+ * <InputText {...props} /> //Prime React
72
+ * <TextField inputProps={props} /> //MUI
73
+ */
74
+ element?: GInputElementHandler<string>;
75
+ } | {
76
+ type: 'checkbox';
77
+ checked?: boolean;
78
+ value?: boolean;
79
+ defaultChecked?: boolean;
80
+ element?: GInputElementHandler<boolean>;
81
+ } | {
82
+ type: 'color';
83
+ value?: string;
84
+ defaultValue?: string;
85
+ element?: GInputElementHandler<string>;
86
+ } | {
87
+ type: 'number' | 'range';
88
+ value?: number;
89
+ defaultValue?: number;
90
+ min?: number;
91
+ max?: number;
92
+ step?: number;
93
+ element?: GInputElementHandler<number>;
94
+ } | {
95
+ type: 'time' | 'week' | 'date' | 'month' | 'datetime-local';
96
+ value?: string;
97
+ defaultValue?: string;
98
+ min?: number;
99
+ max?: number;
100
+ step?: number;
101
+ element?: GInputElementHandler<string>;
102
+ } | {
103
+ type: 'file';
104
+ defaultValue?: string;
105
+ value?: string;
106
+ element?: GInputElementHandler<string>;
107
+ });
108
+
109
+ type IForm<T = any> = {
110
+ [key in keyof T]: GInputState<T[key]>;
111
+ } & {
112
+ [key: string]: GInputState<any>;
113
+ };
114
+ type PartialForm<T> = Partial<{
115
+ [key in keyof T]: Partial<GInputStateMutable<T[key]>>;
116
+ }>;
117
+
118
+ /**a class for handling validations for input(s)
119
+ * @example
120
+ * const baseValidator = new GValidator().withRequiredMessage('this field is required');
121
+ *
122
+ * const validators: GValidators<SignInForm> = {
123
+ * username: new GValidator(baseValidator).withMinLengthMessage('...'),
124
+ * '*': baseValidator // a default validator for all other fields in the form
125
+ * };
126
+ */
127
+ declare class GValidator<T = any> {
128
+ private _handlers;
129
+ private _constraintHandlers;
130
+ private _asyncHandlers;
131
+ track?: (keyof ValidityState)[];
132
+ get handlers(): GCustomValidatorHandler<T>[];
133
+ get constraintHandlers(): GConstraintValidatorHandler[];
134
+ get asyncHandlers(): GCustomValidatorHandlerAsync<T>[];
135
+ constructor(baseValidator?: GValidator<T>);
136
+ hasConstraint(constraint: keyof ValidityState): boolean;
137
+ /**register a `valueMissing` violation handler (use this with `required` attribute) */
138
+ withRequiredMessage(message: string | GConstraintValidator): GValidator<T>;
139
+ /**register a `tooLong` violation handler (use this with `maxLength` attribute) */
140
+ withMaxLengthMessage(message: string | GConstraintValidator): GValidator<T>;
141
+ /**register a `tooShort` violation handler (use this with `minLength` attribute)*/
142
+ withMinLengthMessage(message: string | GConstraintValidator): GValidator<T>;
143
+ /**register a `patternMismatch` violation handler (use this with `pattern` attribute)*/
144
+ withPatternMismatchMessage(message: string | GConstraintValidator): GValidator<T>;
145
+ /**register a `badInput` violation handler */
146
+ withBadInputMessage(message: string | GConstraintValidator): GValidator<T>;
147
+ /**register a `rangeUnderflow` violation handler (use this with `min` attribute) */
148
+ withRangeUnderflowMessage(message: string | GConstraintValidator): GValidator<T>;
149
+ /**register a `rangeOverflow` violation handler (use this with `max` attribute) */
150
+ withRangeOverflowMessage(message: string | GConstraintValidator): GValidator<T>;
151
+ /**
152
+ * register a `typeMismatch` violation handler<br />
153
+ * if its possible use `pattern` attribute (and `withPatternMismatchMessage`) or `custom validation` instead.<br/>
154
+ * use the `type` attribute to set the input's keyboard (for example type `'tel'` will show on mobile phones only numpads)
155
+ * and then with `pattern` or `custom validation` you can validate it.<br/>
156
+ * the reason for that is `type` is not a solid validation and likely will be replaced anyway.<br />
157
+ * if `pattern` or `custom` are used, then `withTypeMismatchMessage` is ignored
158
+ * */
159
+ withTypeMismatchMessage(message: string | GConstraintValidator): GValidator<T>;
160
+ /**register a `stepMismatch` violation handler (use this with `step` attribute)*/
161
+ withStepMismatchMessage(message: string | GConstraintValidator): GValidator<T>;
162
+ /**register a custom validation handler */
163
+ withCustomValidation(handler: GCustomValidatorHandler<T>): GValidator<T>;
164
+ /**register a custom validation async handler */
165
+ withCustomValidationAsync(handler: GCustomValidatorHandlerAsync<T>): GValidator<T>;
166
+ private __addConstraintValidationHandler;
167
+ }
168
+
169
+ type GConstraintValidator = (input: GInputState<any>) => string;
170
+ type GConstraintValidatorHandler = (input: GInputState<any>, validityKey: keyof ValidityState | undefined) => boolean;
171
+ type GCustomValidatorHandler<T> = (input: GInputState<any>, fields: IForm<T>) => RegExp | boolean;
172
+ type GCustomValidatorHandlerAsync<T> = (input: GInputState<any>, fields: IForm<T>) => Promise<ReturnType<GCustomValidatorHandler<T>>>;
173
+ type GInputValidator<T> = GValidator<T> | {
174
+ handlers: GCustomValidatorHandler<T>[];
175
+ constraintHandlers: GConstraintValidatorHandler[];
176
+ asyncHandlers: GCustomValidatorHandlerAsync<T>[];
177
+ hasConstraint(constraint: keyof ValidityState): boolean;
178
+ };
179
+ type GValidators<T = any> = {
180
+ [key in keyof T]?: GInputValidator<T>;
181
+ } & {
182
+ [key: string]: GInputValidator<T> | undefined;
183
+ };
184
+
185
+ type RawData<T> = {
186
+ [key in keyof T]: T[key];
187
+ };
188
+ type InitialState<T = any> = {
189
+ fields: IForm<T> & {
190
+ [key: string]: GInputState<any>;
191
+ };
192
+ };
193
+ type ToURLSearchParamsOptions<T> = {
194
+ exclude?: (keyof T)[];
195
+ include?: (keyof T)[];
196
+ transform?: {
197
+ [key in keyof T]?: (value: GFormState<T>[key]['value']) => any;
198
+ };
199
+ };
200
+ type ToRawDataOptions<T> = ToURLSearchParamsOptions<T>;
201
+ type ToFormDataOptions<T> = ToURLSearchParamsOptions<T>;
202
+ type RNGFormState<T> = IForm<T> & {
203
+ /**indicates whether the form is valid */
204
+ isValid: boolean;
205
+ /**indicates whether the form is invalid */
206
+ isInvalid: boolean;
207
+ /**returns an object with key value pairs represents the form*/
208
+ toRawData(options?: ToRawDataOptions<T>): RawData<T>;
209
+ /**returns `URLSearchParams` instance represents the form*/
210
+ toURLSearchParams(options?: ToURLSearchParamsOptions<T>): URLSearchParams;
211
+ /**update the validity state (invokes all defined validators) */
212
+ checkValidity(): boolean;
213
+ /**manually dispatch any changes to input(s) */
214
+ dispatchChanges: (changes: PartialForm<T> & {
215
+ [key: string]: Partial<GInputState<any>>;
216
+ }) => void;
217
+ };
218
+ type GFormState<T> = RNGFormState<T> & {
219
+ /**returns `FormData` instance represents the form*/
220
+ toFormData(options?: ToFormDataOptions<T>): FormData;
221
+ };
222
+
223
+ type GFormProps<T> = Omit<DetailedHTMLProps<FormHTMLAttributes<HTMLFormElement>, HTMLFormElement>, 'onSubmit' | 'onPaste' | 'onChange' | 'onKeyUp' | 'onKeyDown' | 'children'> & {
224
+ children?: ReactNode | ReactNode[] | ((state: GFormState<T>) => ReactNode | ReactNode[]);
225
+ /** @param loader - a component to display while loading (optional). */
226
+ loader?: ReactNode;
227
+ /** @param stateRef - pass a ref which will points to the current state of the form (optional). */
228
+ stateRef?: RefObject<GFormState<T> | undefined>;
229
+ /** @param onSubmit - a handler for the form submission (optional). */
230
+ onSubmit?: (state: GFormState<T>, e: FormEvent<HTMLFormElement>) => void;
231
+ /** @param onChange - register onChange handler (optional). */
232
+ onChange?: (state: GFormState<T>, e: FormEvent<HTMLFormElement>) => void;
233
+ /** @param onPaste - register onPaste handler (optional). */
234
+ onPaste?: (state: GFormState<T>, e: ClipboardEvent<HTMLFormElement>) => void;
235
+ /** @param onKeyUp - register onKeyUp handler (optional). */
236
+ onKeyUp?: (state: GFormState<T>, e: KeyboardEvent<HTMLFormElement>) => void;
237
+ /** @param onKeyDown - register onKeyDown handler (optional). */
238
+ onKeyDown?: (state: GFormState<T>, e: KeyboardEvent<HTMLFormElement>) => void;
239
+ /** @param validators - an object for handling validations (optional). */
240
+ validators?: GValidators<T>;
241
+ /** @param onInit - execute a handler once the form has initialized (optional). */
242
+ onInit?: (state: GFormState<T>) => void | PartialForm<T> | Promise<void | PartialForm<T>>;
243
+ /** @param optimized - enable optimization by registering the required handlers on the form itself.
244
+ * @see {@link https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#event_delegation|EventDelegation}
245
+ * @optional
246
+ */
247
+ optimized?: boolean;
248
+ };
249
+ /**
250
+ * build dynamic forms with validations.
251
+ * @link Docs - https://gform-react.onrender.com
252
+ * @link Npm - https://www.npmjs.com/package/gform-react
253
+ */
254
+ declare const GForm: <T>(props: GFormProps<T> & {
255
+ ref?: React.Ref<HTMLFormElement>;
256
+ }) => React.ReactElement | null;
257
+
258
+ declare const useFormSelector: <T extends any>(selector: (state: InitialState) => T) => T;
259
+
260
+ export { GForm, GInput, GValidator, useFormSelector };