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.
- package/dist/cjs/gform-react.development.js +97 -82
- package/dist/cjs/gform-react.development.js.map +1 -1
- package/dist/cjs/gform-react.production.js +1 -1
- package/dist/cjs/gform-react.production.js.map +1 -1
- package/dist/cjs/index.js +8 -7
- package/dist/esm/GForm.development.js +160 -0
- package/dist/esm/GForm.development.js.map +1 -0
- package/dist/esm/GForm.production.js +1 -1
- package/dist/esm/GForm.production.js.map +1 -1
- package/dist/esm/GInput.development.js +105 -0
- package/dist/esm/GInput.development.js.map +1 -0
- package/dist/esm/GInput.production.js +1 -1
- package/dist/esm/GInput.production.js.map +1 -1
- package/dist/esm/GValidator.development.js +118 -0
- package/dist/esm/GValidator.development.js.map +1 -0
- package/dist/esm/GValidator.production.js +1 -1
- package/dist/esm/GValidator.production.js.map +1 -1
- package/dist/esm/index.development.js +4 -761
- package/dist/esm/index.js +4 -4
- package/dist/esm/shared.development.js +411 -0
- package/dist/esm/shared.development.js.map +1 -0
- package/dist/esm/shared.production.js +1 -1
- package/dist/esm/shared.production.js.map +1 -1
- package/dist/esm/useFormSelector.development.js +6 -0
- package/dist/esm/useFormSelector.development.js.map +1 -0
- package/dist/esm/useFormSelector.production.js +2 -0
- package/dist/esm/useFormSelector.production.js.map +1 -0
- package/dist/index.d.ts +260 -290
- package/native/dist/cjs/{gform-react-native.development.js → gform-react.development.js} +94 -79
- package/native/dist/cjs/gform-react.development.js.map +1 -0
- package/native/dist/cjs/gform-react.production.js +2 -0
- package/native/dist/cjs/gform-react.production.js.map +1 -0
- package/native/dist/cjs/index.js +8 -7
- package/native/dist/esm/RNGForm.development.js +105 -0
- package/native/dist/esm/RNGForm.development.js.map +1 -0
- package/native/dist/esm/RNGForm.production.js +1 -1
- package/native/dist/esm/RNGForm.production.js.map +1 -1
- package/native/dist/esm/RNGInput.development.js +83 -0
- package/native/dist/esm/RNGInput.development.js.map +1 -0
- package/native/dist/esm/RNGInput.production.js +1 -1
- package/native/dist/esm/RNGInput.production.js.map +1 -1
- package/native/dist/esm/index.development.js +2 -592
- package/native/dist/esm/index.js +2 -2
- package/native/dist/esm/shared.development.js +431 -0
- package/native/dist/esm/shared.development.js.map +1 -0
- package/native/dist/esm/shared.production.js +1 -1
- package/native/dist/esm/shared.production.js.map +1 -1
- package/native/dist/index.d.ts +183 -73
- package/package.json +10 -7
- package/dist/esm/index.development.js.map +0 -1
- package/native/dist/cjs/gform-react-native.development.js.map +0 -1
- package/native/dist/cjs/gform-react-native.production.js +0 -2
- package/native/dist/cjs/gform-react-native.production.js.map +0 -1
- package/native/dist/esm/index.development.js.map +0 -1
- package/native/package.json +0 -15
package/dist/index.d.ts
CHANGED
|
@@ -1,290 +1,260 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
minLength?: number;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
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 };
|