gform-react 1.2.0 → 1.4.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/README.md +9 -3
- package/dist/cjs/gform-react.development.js +576 -0
- package/dist/cjs/gform-react.development.js.map +1 -0
- package/dist/cjs/gform-react.production.js +2 -0
- package/dist/cjs/gform-react.production.js.map +1 -0
- package/dist/cjs/index.js +7 -0
- package/dist/esm/GForm.production.js +2 -0
- package/dist/esm/GForm.production.js.map +1 -0
- package/dist/esm/GInput.production.js +2 -0
- package/dist/esm/GInput.production.js.map +1 -0
- package/dist/esm/GValidator.production.js +2 -0
- package/dist/esm/GValidator.production.js.map +1 -0
- package/dist/esm/index.development.js +565 -0
- package/dist/esm/index.development.js.map +1 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/shared.production.js +2 -0
- package/dist/esm/shared.production.js.map +1 -0
- package/dist/index.d.ts +30 -16
- package/package.json +33 -31
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js +0 -2
- package/dist/index.esm.js.map +0 -1
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|
|
|
5
|
-
[](https://unpkg.com/gform-react@latest/dist/cjs/gform-react.cjs.js)
|
|
6
|
+
[](https://unpkg.com/gform-react@latest/dist/cjs/gform-react.cjs.js)
|
|
7
7
|

|
|
8
8
|

|
|
9
9
|
[](https://unpkg.com/gform-react@latest/LICENSE.md)
|
|
@@ -12,6 +12,12 @@
|
|
|
12
12
|
it doesn't matter which UI library you're using,
|
|
13
13
|
it only cares about the form and the inputs inside.
|
|
14
14
|
|
|
15
|
+
## Pros
|
|
16
|
+
* Lightweight, no dependencies
|
|
17
|
+
* Based on native form and constraint validations (can also add custom and async validations)
|
|
18
|
+
* Can be used with any UI library that preserves native form controls (input, button, etc. can also be adjusted to non-native controls)
|
|
19
|
+
* Tree shakeable
|
|
20
|
+
|
|
15
21
|
## Docs
|
|
16
22
|
https://gform-react.onrender.com
|
|
17
23
|
|
|
@@ -30,4 +36,4 @@ yarn add gform-react
|
|
|
30
36
|
```
|
|
31
37
|
|
|
32
38
|
#### NOTE:
|
|
33
|
-
react >=16.
|
|
39
|
+
react >=16.8.0, react-dom >=16.8.0 are peer dependecies
|
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _extends = require('@babel/runtime/helpers/extends');
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
6
|
+
|
|
7
|
+
const isObject = o => o && typeof o === 'object' && !Array.isArray(o);
|
|
8
|
+
const defaultFieldProps = {
|
|
9
|
+
text: {
|
|
10
|
+
value: ''
|
|
11
|
+
},
|
|
12
|
+
checkbox: {
|
|
13
|
+
value: false
|
|
14
|
+
},
|
|
15
|
+
number: {
|
|
16
|
+
value: 0
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const typeValueDict = {
|
|
20
|
+
checkbox: 'checked',
|
|
21
|
+
number: 'valueAsNumber'
|
|
22
|
+
};
|
|
23
|
+
const _buildFormInitialValues = (rows = []) => {
|
|
24
|
+
const fields = {};
|
|
25
|
+
if (!Array.isArray(rows)) rows = [rows];
|
|
26
|
+
rows.forEach(row => {
|
|
27
|
+
const inputConfigs = _findInputs(row);
|
|
28
|
+
inputConfigs.forEach(config => {
|
|
29
|
+
if (fields[config.formKey]) {
|
|
30
|
+
console.warn(`[Duplicate Keys] - field with key '${config.formKey}' has already been defined.`);
|
|
31
|
+
}
|
|
32
|
+
const {
|
|
33
|
+
required = false,
|
|
34
|
+
max,
|
|
35
|
+
maxLength,
|
|
36
|
+
min,
|
|
37
|
+
minLength,
|
|
38
|
+
step,
|
|
39
|
+
pattern,
|
|
40
|
+
type = 'text',
|
|
41
|
+
defaultValue,
|
|
42
|
+
value,
|
|
43
|
+
checked,
|
|
44
|
+
defaultChecked,
|
|
45
|
+
formKey,
|
|
46
|
+
debounce,
|
|
47
|
+
validatorKey
|
|
48
|
+
} = config;
|
|
49
|
+
const defaultProps = defaultFieldProps[type] || defaultFieldProps.text;
|
|
50
|
+
const inputValue = value || defaultValue || checked || defaultChecked || defaultProps.value;
|
|
51
|
+
fields[config.formKey] = {
|
|
52
|
+
formKey,
|
|
53
|
+
type,
|
|
54
|
+
required,
|
|
55
|
+
max,
|
|
56
|
+
maxLength,
|
|
57
|
+
min,
|
|
58
|
+
minLength,
|
|
59
|
+
step,
|
|
60
|
+
pattern,
|
|
61
|
+
value: inputValue,
|
|
62
|
+
validatorKey,
|
|
63
|
+
debounce,
|
|
64
|
+
dirty: false,
|
|
65
|
+
touched: false,
|
|
66
|
+
gid: Math.random().toString(36)
|
|
67
|
+
};
|
|
68
|
+
Object.keys(fields[config.formKey]).forEach(key => {
|
|
69
|
+
if (typeof fields[config.formKey][key] === 'undefined') delete fields[config.formKey][key];
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
return {
|
|
74
|
+
fields,
|
|
75
|
+
loading: false
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
const _findInputs = (root, total = []) => {
|
|
79
|
+
var _root$props, _root$props2;
|
|
80
|
+
if (!root) return total;
|
|
81
|
+
if (Array.isArray(root)) {
|
|
82
|
+
root.forEach(element => _findInputs(element, total));
|
|
83
|
+
return total;
|
|
84
|
+
}
|
|
85
|
+
if ((_root$props = root.props) !== null && _root$props !== void 0 && _root$props.formKey) {
|
|
86
|
+
total.push(root.props);
|
|
87
|
+
return total;
|
|
88
|
+
}
|
|
89
|
+
return _findInputs((_root$props2 = root.props) === null || _root$props2 === void 0 ? void 0 : _root$props2.children, total);
|
|
90
|
+
};
|
|
91
|
+
const _findValidityKey = validity => {
|
|
92
|
+
for (const key in validity) {
|
|
93
|
+
if (key !== 'valid' && validity[key]) {
|
|
94
|
+
return key;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const hasSubmitter = form => {
|
|
99
|
+
if (!form) return false;
|
|
100
|
+
for (const element of form) {
|
|
101
|
+
if (element.type === 'submit') return true;
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
104
|
+
};
|
|
105
|
+
const _checkIfFormIsValid = fields => {
|
|
106
|
+
for (const f in fields) {
|
|
107
|
+
if (fields[f].error) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
};
|
|
113
|
+
const _toRawData = (fields, options = {}) => {
|
|
114
|
+
const data = {};
|
|
115
|
+
const {
|
|
116
|
+
include,
|
|
117
|
+
exclude,
|
|
118
|
+
transform
|
|
119
|
+
} = options;
|
|
120
|
+
if (include) {
|
|
121
|
+
include.forEach(key => {
|
|
122
|
+
var _fields$key;
|
|
123
|
+
return data[key] = (_fields$key = fields[key]) === null || _fields$key === void 0 ? void 0 : _fields$key.value;
|
|
124
|
+
});
|
|
125
|
+
} else for (const f in fields) {
|
|
126
|
+
data[f] = fields[f].value;
|
|
127
|
+
}
|
|
128
|
+
exclude === null || exclude === void 0 ? void 0 : exclude.forEach(key => delete data[key]);
|
|
129
|
+
if (transform) {
|
|
130
|
+
for (const key in transform) {
|
|
131
|
+
var _fields$key2;
|
|
132
|
+
const set = transform[key];
|
|
133
|
+
data[key] = set((_fields$key2 = fields[key]) === null || _fields$key2 === void 0 ? void 0 : _fields$key2.value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return data;
|
|
137
|
+
};
|
|
138
|
+
const _toFormData = (form, options) => {
|
|
139
|
+
if (!form) return new FormData();
|
|
140
|
+
if (options) {
|
|
141
|
+
const {
|
|
142
|
+
exclude,
|
|
143
|
+
include,
|
|
144
|
+
transform
|
|
145
|
+
} = options;
|
|
146
|
+
let formData;
|
|
147
|
+
if (include) {
|
|
148
|
+
formData = new FormData();
|
|
149
|
+
include.forEach(key => {
|
|
150
|
+
var _form;
|
|
151
|
+
return formData.set(key, (_form = form[key]) === null || _form === void 0 ? void 0 : _form.value);
|
|
152
|
+
});
|
|
153
|
+
} else {
|
|
154
|
+
formData = new FormData(form);
|
|
155
|
+
exclude === null || exclude === void 0 ? void 0 : exclude.forEach(key => formData.delete(key));
|
|
156
|
+
}
|
|
157
|
+
if (transform) {
|
|
158
|
+
for (const key in transform) {
|
|
159
|
+
var _form$key;
|
|
160
|
+
const set = transform[key];
|
|
161
|
+
formData.set(key, set((_form$key = form[key]) === null || _form$key === void 0 ? void 0 : _form$key.value));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return formData;
|
|
165
|
+
}
|
|
166
|
+
return new FormData(form);
|
|
167
|
+
};
|
|
168
|
+
function _toURLSearchParams(options) {
|
|
169
|
+
let data;
|
|
170
|
+
if (options) {
|
|
171
|
+
const {
|
|
172
|
+
exclude,
|
|
173
|
+
include,
|
|
174
|
+
transform
|
|
175
|
+
} = options;
|
|
176
|
+
if (include) {
|
|
177
|
+
data = {};
|
|
178
|
+
include.forEach(key => {
|
|
179
|
+
var _this$key;
|
|
180
|
+
return data[key] = (_this$key = this[key]) === null || _this$key === void 0 ? void 0 : _this$key.value;
|
|
181
|
+
});
|
|
182
|
+
} else {
|
|
183
|
+
data = this.toRawData();
|
|
184
|
+
exclude === null || exclude === void 0 ? void 0 : exclude.forEach(key => delete data[key]);
|
|
185
|
+
}
|
|
186
|
+
if (transform) {
|
|
187
|
+
for (const key in transform) {
|
|
188
|
+
var _this$key2;
|
|
189
|
+
const set = transform[key];
|
|
190
|
+
data[key] = set((_this$key2 = this[key]) === null || _this$key2 === void 0 ? void 0 : _this$key2.value);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
} else data = this.toRawData();
|
|
194
|
+
return new URLSearchParams(data);
|
|
195
|
+
}
|
|
196
|
+
function __debounce(timeout, id) {
|
|
197
|
+
return new Promise(resolve => {
|
|
198
|
+
var _this$id;
|
|
199
|
+
if ((_this$id = this[id]) !== null && _this$id !== void 0 && _this$id.timerId) clearTimeout(this[id].timerId);
|
|
200
|
+
const timerId = setTimeout(() => resolve(), timeout);
|
|
201
|
+
if (this[id]) {
|
|
202
|
+
this[id].timerId = timerId;
|
|
203
|
+
} else this[id] = {
|
|
204
|
+
timerId
|
|
205
|
+
};
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
const _debounce = __debounce.bind({});
|
|
209
|
+
const _extractValue = (e, unknown) => {
|
|
210
|
+
if (e.target) {
|
|
211
|
+
if (Object.hasOwn(typeValueDict, e.target.type)) return e.target[typeValueDict[e.target.type]];
|
|
212
|
+
return e.target.value;
|
|
213
|
+
}
|
|
214
|
+
return e.value || (isObject(unknown) ? unknown.value : unknown);
|
|
215
|
+
};
|
|
216
|
+
const _checkResult = (handlerResult, value) => typeof handlerResult === 'boolean' ? handlerResult : typeof value === 'string' ? !handlerResult.test(value) : false;
|
|
217
|
+
const _merge = (target = {}, ...nodes) => {
|
|
218
|
+
if (!nodes.length) return target;
|
|
219
|
+
const next = nodes.shift();
|
|
220
|
+
if (isObject(next)) {
|
|
221
|
+
for (const key in next) {
|
|
222
|
+
target[key] = target[key] ? {
|
|
223
|
+
...target[key],
|
|
224
|
+
...next[key]
|
|
225
|
+
} : next[key];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return _merge(target, ...nodes);
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const useForm = (children, validators = {}, optimized = false) => {
|
|
232
|
+
const initialValues = React.useMemo(() => _buildFormInitialValues(typeof children === 'function' ? children({}) : children), [children]);
|
|
233
|
+
const [state, setState] = React.useState(initialValues);
|
|
234
|
+
const _validateInputHandler = (input, e) => {
|
|
235
|
+
if (!input) return;
|
|
236
|
+
const element = e.target;
|
|
237
|
+
if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) {
|
|
238
|
+
if (!input.checkValidity) input.checkValidity = () => element.checkValidity();
|
|
239
|
+
element.setCustomValidity('');
|
|
240
|
+
const validityKey = _findValidityKey(element.validity);
|
|
241
|
+
_validateInput(input, validityKey, v => element.setCustomValidity(v));
|
|
242
|
+
if (!validityKey && input.error) {
|
|
243
|
+
element.setCustomValidity(input.errorText || 'error');
|
|
244
|
+
}
|
|
245
|
+
} else {
|
|
246
|
+
_validateInput(input, input.required && !input.value ? 'valueMissing' : undefined);
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
const _updateInputHandler = (key, e, unknown) => {
|
|
250
|
+
const value = _extractValue(e, unknown);
|
|
251
|
+
_updateInput(key, value);
|
|
252
|
+
_validateInputHandler(state.fields[key], e);
|
|
253
|
+
};
|
|
254
|
+
const _validateInput = (input, validityKey, setValidity) => {
|
|
255
|
+
const inputValidator = validators[input.validatorKey || input.formKey] || validators['*'];
|
|
256
|
+
inputValidator && __validateInput(input, inputValidator, validityKey, setValidity);
|
|
257
|
+
input.touched = true;
|
|
258
|
+
_dispatchChanges(input, input.formKey);
|
|
259
|
+
};
|
|
260
|
+
const _updateInput = (key, value) => {
|
|
261
|
+
const input = state.fields[key];
|
|
262
|
+
input.value = value;
|
|
263
|
+
input.dirty = true;
|
|
264
|
+
};
|
|
265
|
+
const _dispatchChanges = (changes, key) => setState(prev => {
|
|
266
|
+
if (key) {
|
|
267
|
+
return {
|
|
268
|
+
...prev,
|
|
269
|
+
fields: {
|
|
270
|
+
...prev.fields,
|
|
271
|
+
[key]: {
|
|
272
|
+
...prev.fields[key],
|
|
273
|
+
...changes
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
return {
|
|
279
|
+
...prev,
|
|
280
|
+
...changes
|
|
281
|
+
};
|
|
282
|
+
});
|
|
283
|
+
const __validateInput = (input, inputValidator, validityKey, setValidity) => {
|
|
284
|
+
for (const index in inputValidator.constraintHandlers) {
|
|
285
|
+
const result = inputValidator.constraintHandlers[index](input, validityKey);
|
|
286
|
+
input.error = _checkResult(result, input.value);
|
|
287
|
+
if (input.error) return;
|
|
288
|
+
}
|
|
289
|
+
for (const index in inputValidator.handlers) {
|
|
290
|
+
const result = inputValidator.handlers[index](input, state.fields);
|
|
291
|
+
input.error = _checkResult(result, input.value);
|
|
292
|
+
if (input.error) return;
|
|
293
|
+
}
|
|
294
|
+
input.errorText = '';
|
|
295
|
+
if (inputValidator.asyncHandlers.length) {
|
|
296
|
+
input.error = true;
|
|
297
|
+
_debounce(input.debounce || 300, `${input.gid}-async`).then(() => {
|
|
298
|
+
const validateAsync = async () => {
|
|
299
|
+
for (const index in inputValidator.asyncHandlers) {
|
|
300
|
+
const result = await inputValidator.asyncHandlers[index](input, state.fields);
|
|
301
|
+
input.error = _checkResult(result, input.value);
|
|
302
|
+
if (input.error) break;
|
|
303
|
+
}
|
|
304
|
+
if (!input.error) input.errorText = '';
|
|
305
|
+
_dispatchChanges({
|
|
306
|
+
error: input.error,
|
|
307
|
+
errorText: input.errorText
|
|
308
|
+
}, input.formKey);
|
|
309
|
+
setValidity === null || setValidity === void 0 ? void 0 : setValidity(input.errorText);
|
|
310
|
+
};
|
|
311
|
+
validateAsync();
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
return {
|
|
316
|
+
state,
|
|
317
|
+
_updateInputHandler,
|
|
318
|
+
_validateInputHandler,
|
|
319
|
+
_dispatchChanges,
|
|
320
|
+
optimized
|
|
321
|
+
};
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
const gFormContext = React.createContext({
|
|
325
|
+
state: {
|
|
326
|
+
fields: {},
|
|
327
|
+
loading: false
|
|
328
|
+
},
|
|
329
|
+
_updateInputHandler: () => null,
|
|
330
|
+
_validateInputHandler: () => null,
|
|
331
|
+
_dispatchChanges: () => null,
|
|
332
|
+
optimized: false
|
|
333
|
+
});
|
|
334
|
+
const useGenericFormContext = () => React.useContext(gFormContext);
|
|
335
|
+
const GFormContextProvider = gFormContext.Provider;
|
|
336
|
+
|
|
337
|
+
const GForm = ({
|
|
338
|
+
loader = React.createElement("div", null, "loading"),
|
|
339
|
+
stateRef,
|
|
340
|
+
onSubmit,
|
|
341
|
+
onChange,
|
|
342
|
+
children,
|
|
343
|
+
validators,
|
|
344
|
+
onInit,
|
|
345
|
+
optimized,
|
|
346
|
+
...rest
|
|
347
|
+
}) => {
|
|
348
|
+
const formRef = React.useRef(null);
|
|
349
|
+
const values = useForm(children, validators, optimized);
|
|
350
|
+
const {
|
|
351
|
+
state,
|
|
352
|
+
_updateInputHandler,
|
|
353
|
+
_validateInputHandler,
|
|
354
|
+
_dispatchChanges
|
|
355
|
+
} = values;
|
|
356
|
+
const formState = React.useMemo(() => {
|
|
357
|
+
const _isFormValid = _checkIfFormIsValid(state.fields);
|
|
358
|
+
const formState = {
|
|
359
|
+
...state.fields,
|
|
360
|
+
isValid: _isFormValid,
|
|
361
|
+
isInvalid: !_isFormValid,
|
|
362
|
+
loading: state.loading,
|
|
363
|
+
toRawData: options => _toRawData(state.fields, options),
|
|
364
|
+
toFormData: () => _toFormData(formRef.current),
|
|
365
|
+
toURLSearchParams: _toURLSearchParams,
|
|
366
|
+
checkValidity: function () {
|
|
367
|
+
var _formRef$current;
|
|
368
|
+
this.isValid = ((_formRef$current = formRef.current) === null || _formRef$current === void 0 ? void 0 : _formRef$current.checkValidity()) || false;
|
|
369
|
+
this.isInvalid = !this.isValid;
|
|
370
|
+
return this.isValid;
|
|
371
|
+
},
|
|
372
|
+
setLoading: p => _dispatchChanges({
|
|
373
|
+
loading: typeof p === 'function' ? p(state.loading) : p
|
|
374
|
+
})
|
|
375
|
+
};
|
|
376
|
+
if (stateRef) stateRef.current = formState;
|
|
377
|
+
return formState;
|
|
378
|
+
}, [state.fields]);
|
|
379
|
+
const form = React.useMemo(() => {
|
|
380
|
+
const formChildren = typeof children === 'function' ? children(formState) : children;
|
|
381
|
+
const _onSubmit = e => {
|
|
382
|
+
e.preventDefault();
|
|
383
|
+
onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit(formState, e);
|
|
384
|
+
};
|
|
385
|
+
return optimized ? React.createElement("form", _extends({}, rest, {
|
|
386
|
+
ref: formRef,
|
|
387
|
+
onBlur: e => {
|
|
388
|
+
_validateInputHandler(state.fields[e.target.name], e);
|
|
389
|
+
},
|
|
390
|
+
onInvalid: e => {
|
|
391
|
+
e.preventDefault();
|
|
392
|
+
_validateInputHandler(state.fields[e.target.name], e);
|
|
393
|
+
},
|
|
394
|
+
onChange: (e, unknown) => {
|
|
395
|
+
_updateInputHandler(e.target.name, e, unknown);
|
|
396
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(formState, e);
|
|
397
|
+
},
|
|
398
|
+
onSubmit: _onSubmit
|
|
399
|
+
}), formChildren) : React.createElement("form", _extends({}, rest, {
|
|
400
|
+
ref: formRef,
|
|
401
|
+
onSubmit: _onSubmit
|
|
402
|
+
}), formChildren);
|
|
403
|
+
}, [formState, children]);
|
|
404
|
+
React.useEffect(() => {
|
|
405
|
+
if (!hasSubmitter(formRef.current)) {
|
|
406
|
+
console.warn(`[No Submit Button] - you have created a form without a button type=submit, this will prevent the onSubmit event from being fired.\nif you have a button with onClick event that handle the submission of the form then ignore this warning\nbut don't forget to manually invoke the checkValidity() function and to check if the form is valid before perfoming any action, for example:\nif (formState.checkValidity()) { \n\t//do somthing\n}\n`);
|
|
407
|
+
}
|
|
408
|
+
const dispatchers = Object.keys(state.fields).reduce((fields, key) => {
|
|
409
|
+
fields[key] = {
|
|
410
|
+
dispatchChanges: changes => _dispatchChanges(changes, key)
|
|
411
|
+
};
|
|
412
|
+
return fields;
|
|
413
|
+
}, {});
|
|
414
|
+
if (onInit) {
|
|
415
|
+
const _handler = _c => {
|
|
416
|
+
_dispatchChanges({
|
|
417
|
+
fields: _merge({}, state.fields, dispatchers, _c)
|
|
418
|
+
});
|
|
419
|
+
};
|
|
420
|
+
const changes = onInit(formState);
|
|
421
|
+
changes instanceof Promise ? changes.then(_handler) : _handler(changes);
|
|
422
|
+
} else {
|
|
423
|
+
_dispatchChanges({
|
|
424
|
+
fields: _merge({}, state.fields, dispatchers)
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}, []);
|
|
428
|
+
return React.createElement(GFormContextProvider, {
|
|
429
|
+
value: values
|
|
430
|
+
}, state.loading ? loader : form);
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
const GInput = ({
|
|
434
|
+
formKey,
|
|
435
|
+
element,
|
|
436
|
+
title,
|
|
437
|
+
type,
|
|
438
|
+
validatorKey,
|
|
439
|
+
fetch,
|
|
440
|
+
fetchDeps = [],
|
|
441
|
+
optimized,
|
|
442
|
+
defaultChecked,
|
|
443
|
+
defaultValue,
|
|
444
|
+
checked,
|
|
445
|
+
value,
|
|
446
|
+
debounce = 300,
|
|
447
|
+
...rest
|
|
448
|
+
}) => {
|
|
449
|
+
const {
|
|
450
|
+
state: {
|
|
451
|
+
fields
|
|
452
|
+
},
|
|
453
|
+
_updateInputHandler,
|
|
454
|
+
_validateInputHandler,
|
|
455
|
+
_dispatchChanges,
|
|
456
|
+
optimized: formOptimized
|
|
457
|
+
} = useGenericFormContext();
|
|
458
|
+
const inputState = fields[formKey];
|
|
459
|
+
const _element = React.useMemo(() => {
|
|
460
|
+
let value, checked;
|
|
461
|
+
if (type === 'checkbox') checked = inputState.value || false;else value = inputState.value || '';
|
|
462
|
+
const _props = {
|
|
463
|
+
...rest,
|
|
464
|
+
type,
|
|
465
|
+
name: formKey,
|
|
466
|
+
value,
|
|
467
|
+
checked,
|
|
468
|
+
title: title || inputState.errorText
|
|
469
|
+
};
|
|
470
|
+
if (!formOptimized || !optimized) {
|
|
471
|
+
_props.onBlur = e => _validateInputHandler(inputState, e);
|
|
472
|
+
_props.onInvalid = e => {
|
|
473
|
+
e.preventDefault();
|
|
474
|
+
_validateInputHandler(inputState, e);
|
|
475
|
+
};
|
|
476
|
+
_props.onChange = (e, unknown) => _updateInputHandler(formKey, e, unknown);
|
|
477
|
+
}
|
|
478
|
+
if (element) {
|
|
479
|
+
return element(inputState, _props);
|
|
480
|
+
}
|
|
481
|
+
return React.createElement("input", _props);
|
|
482
|
+
}, [inputState, element]);
|
|
483
|
+
const _fetchDeps = React.useMemo(() => fetchDeps.map(key => fields[key].value), [fields]);
|
|
484
|
+
React.useEffect(() => {
|
|
485
|
+
if (fetch) {
|
|
486
|
+
_debounce(debounce, `${inputState.gid}-fetch`).then(() => {
|
|
487
|
+
const res = fetch(inputState, fields);
|
|
488
|
+
res instanceof Promise ? res.then(state => state && _dispatchChanges(state, formKey)) : res && _dispatchChanges(res, formKey);
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
}, _fetchDeps);
|
|
492
|
+
return _element;
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
class GValidator {
|
|
496
|
+
get handlers() {
|
|
497
|
+
return this._handlers;
|
|
498
|
+
}
|
|
499
|
+
get constraintHandlers() {
|
|
500
|
+
return this._constraintHandlers;
|
|
501
|
+
}
|
|
502
|
+
get asyncHandlers() {
|
|
503
|
+
return this._asyncHandlers;
|
|
504
|
+
}
|
|
505
|
+
constructor(baseValidator) {
|
|
506
|
+
_defineProperty(this, "_handlers", void 0);
|
|
507
|
+
_defineProperty(this, "_constraintHandlers", void 0);
|
|
508
|
+
_defineProperty(this, "_asyncHandlers", void 0);
|
|
509
|
+
_defineProperty(this, "_track", void 0);
|
|
510
|
+
const baseHandlers = (baseValidator === null || baseValidator === void 0 ? void 0 : baseValidator.handlers) || [];
|
|
511
|
+
const baseConstraintHandlers = (baseValidator === null || baseValidator === void 0 ? void 0 : baseValidator.constraintHandlers) || [];
|
|
512
|
+
const baseHandlersAsync = (baseValidator === null || baseValidator === void 0 ? void 0 : baseValidator.asyncHandlers) || [];
|
|
513
|
+
this._handlers = new Array().concat(baseHandlers);
|
|
514
|
+
this._constraintHandlers = new Array().concat(baseConstraintHandlers);
|
|
515
|
+
this._asyncHandlers = new Array().concat(baseHandlersAsync);
|
|
516
|
+
{
|
|
517
|
+
this._track = [];
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
withRequiredMessage(message) {
|
|
521
|
+
return this.__addConstraintValidationHandler('valueMissing', message);
|
|
522
|
+
}
|
|
523
|
+
withMaxLengthMessage(message) {
|
|
524
|
+
return this.__addConstraintValidationHandler('tooLong', message);
|
|
525
|
+
}
|
|
526
|
+
withMinLengthMessage(message) {
|
|
527
|
+
return this.__addConstraintValidationHandler('tooShort', message);
|
|
528
|
+
}
|
|
529
|
+
withPatternMismatchMessage(message) {
|
|
530
|
+
return this.__addConstraintValidationHandler('patternMismatch', message);
|
|
531
|
+
}
|
|
532
|
+
withBadInputMessage(message) {
|
|
533
|
+
return this.__addConstraintValidationHandler('badInput', message);
|
|
534
|
+
}
|
|
535
|
+
withRangeUnderflowMessage(message) {
|
|
536
|
+
return this.__addConstraintValidationHandler('rangeUnderflow', message);
|
|
537
|
+
}
|
|
538
|
+
withRangeOverflowMessage(message) {
|
|
539
|
+
return this.__addConstraintValidationHandler('rangeOverflow', message);
|
|
540
|
+
}
|
|
541
|
+
withTypeMismatchMessage(message) {
|
|
542
|
+
return this.__addConstraintValidationHandler('typeMismatch', message);
|
|
543
|
+
}
|
|
544
|
+
withStepMismatchMessage(message) {
|
|
545
|
+
return this.__addConstraintValidationHandler('stepMismatch', message);
|
|
546
|
+
}
|
|
547
|
+
withCustomValidation(handler) {
|
|
548
|
+
this._handlers.push(handler);
|
|
549
|
+
return this;
|
|
550
|
+
}
|
|
551
|
+
withCustomValidationAsync(handler) {
|
|
552
|
+
this._asyncHandlers.push(handler);
|
|
553
|
+
return this;
|
|
554
|
+
}
|
|
555
|
+
__addConstraintValidationHandler(validityKey, message) {
|
|
556
|
+
{
|
|
557
|
+
if (this._track.includes(validityKey)) {
|
|
558
|
+
console.warn(`[Duplicate Handlers] - handler for '${validityKey}' has already been defined`);
|
|
559
|
+
}
|
|
560
|
+
this._track.push(validityKey);
|
|
561
|
+
}
|
|
562
|
+
this._constraintHandlers.push((input, key) => {
|
|
563
|
+
if (key === validityKey) {
|
|
564
|
+
input.errorText = typeof message === 'string' ? message : message(input);
|
|
565
|
+
return true;
|
|
566
|
+
}
|
|
567
|
+
return false;
|
|
568
|
+
});
|
|
569
|
+
return this;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
exports.GForm = GForm;
|
|
574
|
+
exports.GInput = GInput;
|
|
575
|
+
exports.GValidator = GValidator;
|
|
576
|
+
//# sourceMappingURL=gform-react.development.js.map
|