gform-react 2.7.5 → 2.8.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 +30 -30
- package/dist/cjs/gform-react.development.js +165 -140
- 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/esm/GForm.development.js +12 -9
- package/dist/esm/GForm.development.js.map +1 -1
- package/dist/esm/GForm.production.js +1 -1
- package/dist/esm/GForm.production.js.map +1 -1
- package/dist/esm/GInput.development.js +22 -21
- package/dist/esm/GInput.development.js.map +1 -1
- package/dist/esm/GInput.production.js +1 -1
- package/dist/esm/GInput.production.js.map +1 -1
- package/dist/esm/GValidator.development.js +5 -1
- package/dist/esm/GValidator.development.js.map +1 -1
- package/dist/esm/GValidator.production.js +1 -1
- package/dist/esm/GValidator.production.js.map +1 -1
- package/dist/esm/shared.development.js +133 -112
- package/dist/esm/shared.development.js.map +1 -1
- package/dist/esm/shared.production.js +1 -1
- package/dist/esm/shared.production.js.map +1 -1
- package/dist/esm/useFormSelector.development.js +3 -2
- package/dist/esm/useFormSelector.development.js.map +1 -1
- package/dist/esm/useFormSelector.production.js +1 -1
- package/native/dist/cjs/gform-react.development.js +151 -133
- package/native/dist/cjs/gform-react.development.js.map +1 -1
- package/native/dist/cjs/gform-react.production.js +1 -1
- package/native/dist/cjs/gform-react.production.js.map +1 -1
- package/native/dist/esm/RNGForm.development.js +4 -4
- package/native/dist/esm/RNGForm.development.js.map +1 -1
- 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 +23 -21
- package/native/dist/esm/RNGInput.development.js.map +1 -1
- package/native/dist/esm/RNGInput.production.js +1 -1
- package/native/dist/esm/RNGInput.production.js.map +1 -1
- package/native/dist/esm/shared.development.js +133 -112
- package/native/dist/esm/shared.development.js.map +1 -1
- package/native/dist/esm/shared.production.js +1 -1
- package/native/dist/esm/shared.production.js.map +1 -1
- package/package.json +3 -3
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
|
|
2
2
|
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
|
|
3
|
+
import React, { createContext, useContext, useSyncExternalStore, useRef, useCallback, useMemo } from 'react';
|
|
3
4
|
import { validityMap, handlersMap } from './GValidator.development.js';
|
|
4
5
|
|
|
5
|
-
function ownKeys$
|
|
6
|
-
function _objectSpread$
|
|
6
|
+
function ownKeys$2(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
7
|
+
function _objectSpread$2(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$2(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$2(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
7
8
|
const isObject = o => o && typeof o === 'object' && !Array.isArray(o);
|
|
8
9
|
const defaultFieldProps = {
|
|
9
10
|
text: {
|
|
@@ -21,84 +22,52 @@ const typeValueDict = {
|
|
|
21
22
|
number: 'valueAsNumber'
|
|
22
23
|
};
|
|
23
24
|
const _generateIdUnsafe = () => (+new Date()).toString(36) + (1 - Math.random()).toString(36).substring(2, 16);
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
minLength,
|
|
45
|
-
step,
|
|
46
|
-
pattern,
|
|
47
|
-
type = "text",
|
|
48
|
-
defaultValue,
|
|
49
|
-
value,
|
|
50
|
-
checked,
|
|
51
|
-
defaultChecked,
|
|
52
|
-
formKey,
|
|
53
|
-
debounce,
|
|
54
|
-
validatorKey
|
|
55
|
-
} = config;
|
|
56
|
-
const defaultProps = defaultFieldProps[type] || defaultFieldProps.text;
|
|
57
|
-
const inputValue = value || defaultValue || checked || defaultChecked || defaultProps.value;
|
|
58
|
-
fields[formKey] = {
|
|
59
|
-
formKey,
|
|
60
|
-
type,
|
|
61
|
-
required,
|
|
62
|
-
max,
|
|
63
|
-
maxLength,
|
|
64
|
-
min,
|
|
65
|
-
minLength,
|
|
66
|
-
step,
|
|
67
|
-
pattern,
|
|
68
|
-
value: inputValue,
|
|
69
|
-
validatorKey,
|
|
70
|
-
debounce,
|
|
71
|
-
dirty: false,
|
|
72
|
-
touched: false,
|
|
73
|
-
gid: _generateIdUnsafe()
|
|
74
|
-
};
|
|
75
|
-
});
|
|
25
|
+
const _buildInputInitialValues = input => {
|
|
26
|
+
const {
|
|
27
|
+
required = false,
|
|
28
|
+
max,
|
|
29
|
+
maxLength,
|
|
30
|
+
min,
|
|
31
|
+
minLength,
|
|
32
|
+
step,
|
|
33
|
+
pattern,
|
|
34
|
+
type = "text",
|
|
35
|
+
defaultValue,
|
|
36
|
+
value,
|
|
37
|
+
checked,
|
|
38
|
+
defaultChecked,
|
|
39
|
+
formKey,
|
|
40
|
+
debounce,
|
|
41
|
+
validatorKey
|
|
42
|
+
} = input;
|
|
43
|
+
const defaultProps = defaultFieldProps[type] || defaultFieldProps.text;
|
|
44
|
+
const inputValue = value || defaultValue || checked || defaultChecked || defaultProps.value;
|
|
76
45
|
return {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
46
|
+
formKey,
|
|
47
|
+
type,
|
|
48
|
+
required,
|
|
49
|
+
max,
|
|
50
|
+
maxLength,
|
|
51
|
+
min,
|
|
52
|
+
minLength,
|
|
53
|
+
step,
|
|
54
|
+
pattern,
|
|
55
|
+
value: inputValue,
|
|
56
|
+
validatorKey,
|
|
57
|
+
debounce,
|
|
58
|
+
dirty: false,
|
|
59
|
+
touched: false,
|
|
60
|
+
gid: _generateIdUnsafe(),
|
|
61
|
+
error: false,
|
|
62
|
+
errorText: '',
|
|
63
|
+
dispatchChanges(changes) {},
|
|
64
|
+
checkValidity() {
|
|
65
|
+
return false;
|
|
95
66
|
}
|
|
96
|
-
}
|
|
97
|
-
return total;
|
|
67
|
+
};
|
|
98
68
|
};
|
|
99
|
-
const _findValidityKey =
|
|
69
|
+
const _findValidityKey = validity => {
|
|
100
70
|
for (const key in validity) {
|
|
101
|
-
if (exclude.includes(key)) continue;
|
|
102
71
|
if (key !== 'valid' && validity[key]) {
|
|
103
72
|
return key;
|
|
104
73
|
}
|
|
@@ -222,7 +191,7 @@ const _merge = (target = {}, ...nodes) => {
|
|
|
222
191
|
const next = nodes.shift();
|
|
223
192
|
if (isObject(next)) {
|
|
224
193
|
for (const key in next) {
|
|
225
|
-
target[key] = target[key] ? _objectSpread$
|
|
194
|
+
target[key] = target[key] ? _objectSpread$2(_objectSpread$2({}, target[key]), next[key]) : next[key];
|
|
226
195
|
}
|
|
227
196
|
}
|
|
228
197
|
return _merge(target, ...nodes);
|
|
@@ -243,7 +212,7 @@ const _mergeRefs = (refA, refB) => {
|
|
|
243
212
|
};
|
|
244
213
|
const _buildFormState = (fields, formElement, dispatchChanges) => {
|
|
245
214
|
const isFormValid = _checkIfFormIsValid(fields);
|
|
246
|
-
const formState = _objectSpread$
|
|
215
|
+
const formState = _objectSpread$2(_objectSpread$2({}, fields), {}, {
|
|
247
216
|
isValid: isFormValid,
|
|
248
217
|
isInvalid: !isFormValid,
|
|
249
218
|
toRawData: options => _toRawData(fields, options),
|
|
@@ -261,36 +230,38 @@ const _buildFormState = (fields, formElement, dispatchChanges) => {
|
|
|
261
230
|
return formState;
|
|
262
231
|
};
|
|
263
232
|
|
|
264
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
265
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
233
|
+
function ownKeys$1(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
234
|
+
function _objectSpread$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
266
235
|
const useFormHandlers = (getState, setState, validators = {}, optimized = false) => {
|
|
267
236
|
const _viHandler = (input, e) => {
|
|
268
237
|
if (!input) return;
|
|
269
238
|
const element = e && e.target;
|
|
239
|
+
input.touched = true;
|
|
270
240
|
if (typeof document !== 'undefined' && (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement)) {
|
|
271
241
|
if (!input.checkValidity) input.checkValidity = () => element.checkValidity();
|
|
272
|
-
const hasInitialValue = !input.dirty && input.value
|
|
242
|
+
const hasInitialValue = !input.dirty && input.value;
|
|
273
243
|
if (hasInitialValue) {
|
|
274
|
-
|
|
244
|
+
const {
|
|
245
|
+
validityKey: _validityKey = 'custom'
|
|
246
|
+
} = _checkInputManually(input);
|
|
247
|
+
element.setCustomValidity(_validityKey);
|
|
275
248
|
_dispatchChanges(input, input.formKey);
|
|
276
249
|
return;
|
|
277
250
|
}
|
|
278
251
|
element.setCustomValidity('');
|
|
279
|
-
const
|
|
280
|
-
const validityKey = _findValidityKey(element.validity, exclude);
|
|
252
|
+
const validityKey = _findValidityKey(element.validity);
|
|
281
253
|
_validateInput(input, validityKey, v => element.setCustomValidity(v));
|
|
282
254
|
if (!validityKey && input.error) {
|
|
283
255
|
element.setCustomValidity(input.errorText || 'error');
|
|
284
256
|
}
|
|
285
257
|
_dispatchChanges(input, input.formKey);
|
|
286
258
|
} else {
|
|
287
|
-
input.checkValidity = () => _checkInputManually(input);
|
|
259
|
+
input.checkValidity = () => _checkInputManually(input).isValid;
|
|
288
260
|
input.checkValidity();
|
|
289
261
|
_dispatchChanges(input, input.formKey);
|
|
290
262
|
}
|
|
291
263
|
};
|
|
292
264
|
const _checkInputManually = input => {
|
|
293
|
-
const exclude = input.type && (input.pattern || hasCustomValidation(input)) ? ['typeMismatch'] : [];
|
|
294
265
|
let validityKey = _findValidityKey({
|
|
295
266
|
valueMissing: input.required && !input.value || false,
|
|
296
267
|
typeMismatch: _checkTypeMismatch(input),
|
|
@@ -299,48 +270,56 @@ const useFormHandlers = (getState, setState, validators = {}, optimized = false)
|
|
|
299
270
|
patternMismatch: input.pattern && _checkResult(input.pattern, input.value) || false,
|
|
300
271
|
rangeUnderflow: input.min && Number(input.value) < Number(input.min) || false,
|
|
301
272
|
rangeOverflow: input.max && Number(input.value) > Number(input.max) || false
|
|
302
|
-
}
|
|
273
|
+
});
|
|
303
274
|
if (!validityKey && input.error) {
|
|
304
275
|
validityKey = 'customError';
|
|
305
276
|
}
|
|
306
277
|
_validateInput(input, validityKey);
|
|
307
|
-
return
|
|
278
|
+
return {
|
|
279
|
+
isValid: !input.error,
|
|
280
|
+
validityKey
|
|
281
|
+
};
|
|
308
282
|
};
|
|
309
283
|
const _updateInputHandler = (input, e, unknown) => {
|
|
310
284
|
input.value = _extractValue(e, unknown);
|
|
285
|
+
input.dirty = true;
|
|
311
286
|
_viHandler(input, e);
|
|
312
287
|
};
|
|
313
288
|
const _validateInput = (input, validityKey, setValidity) => {
|
|
314
289
|
const inputValidator = validators[input.validatorKey || input.formKey] || validators['*'];
|
|
315
290
|
{
|
|
316
291
|
if (validityKey && !(inputValidator !== null && inputValidator !== void 0 && inputValidator.hasConstraint(validityKey))) {
|
|
317
|
-
if (validityKey === 'typeMismatch')
|
|
292
|
+
if (validityKey === 'typeMismatch') {
|
|
293
|
+
if (!(inputValidator !== null && inputValidator !== void 0 && inputValidator.handlers.length)) console.warn(`DEV ONLY - [Missing Validator] - the input '${input.formKey}' has described the constraint '${validityMap[validityKey]}' however, a correspond validator is missing.\nadd '${handlersMap[validityMap[validityKey]]}' or 'withCustomValidation' or '${handlersMap[validityMap.patternMismatch]}' to the input validator.\nexample:\nconst validators: GValidators = {\n\temail: new GValidator().withTypeMismatchMessage('pattern mismatch'),\n\t...\n}\nif you added on of these validators then the input is still suffering from '${validityKey}' violation.\n\nor either remove the constraint '${validityMap[validityKey]}' from the input props.\n`);else console.warn(`DEV ONLY - [Missing Validator] - the input '${input.formKey}' has described the constraint '${validityMap[validityKey]}' however, a correspond validator is missing or not satisfies the native constraint.\nadd '${handlersMap[validityMap[validityKey]]}' or 'withCustomValidation' to the input validator.\nexample:\nconst validators: GValidators = {\n\temail: new GValidator().withTypeMismatchMessage('pattern mismatch'),\n\t...\n}\n\nif you already have a Custom Validation then the input is still not satisfies the native type pattern.\neither enforce it or remove the constraint '${validityMap[validityKey]}' from the input props`);
|
|
294
|
+
} else console.warn(`DEV ONLY - [Missing Validator] - the input '${input.formKey}' has described the constraint '${validityMap[validityKey]}' however, a correspond validator is missing.\nadd '${handlersMap[validityMap[validityKey]]}' to the input validator.\nexample:\nconst validators: GValidators = {\n\temail: new GValidator().withPatternMismatchMessage('pattern mismatch'),\n\t...\n}\n\nor either remove the constraint '${validityMap[validityKey]}' from the input props`);
|
|
295
|
+
console.warn(`form submition is prevented due to violation(s) of input '${input.formKey}': violation '${validityKey}' caused by '${validityMap[validityKey]}' property (<Ginput ${validityMap[validityKey]}={...} />)`);
|
|
318
296
|
}
|
|
319
297
|
}
|
|
320
298
|
if (inputValidator) {
|
|
321
299
|
__validateInput(input, inputValidator, validityKey, setValidity);
|
|
322
300
|
}
|
|
323
|
-
input.touched = true;
|
|
324
301
|
};
|
|
325
302
|
const _dispatchChanges = (changes, key) => setState(prev => {
|
|
326
303
|
if (key) {
|
|
327
|
-
return _objectSpread(_objectSpread({}, prev), {}, {
|
|
328
|
-
fields: _objectSpread(_objectSpread({}, prev.fields), {}, {
|
|
329
|
-
[key]: _objectSpread(_objectSpread({}, prev.fields[key]), changes)
|
|
304
|
+
return _objectSpread$1(_objectSpread$1({}, prev), {}, {
|
|
305
|
+
fields: _objectSpread$1(_objectSpread$1({}, prev.fields), {}, {
|
|
306
|
+
[key]: _objectSpread$1(_objectSpread$1({}, prev.fields[key]), changes)
|
|
330
307
|
})
|
|
331
308
|
});
|
|
332
309
|
}
|
|
333
|
-
return _objectSpread(_objectSpread({}, prev), changes);
|
|
310
|
+
return _objectSpread$1(_objectSpread$1({}, prev), changes);
|
|
334
311
|
});
|
|
335
312
|
const __validateInput = (input, inputValidator, validityKey, setValidity) => {
|
|
336
313
|
const fields = getState().fields;
|
|
337
314
|
for (const index in inputValidator.constraintHandlers) {
|
|
338
|
-
const
|
|
315
|
+
const handler = inputValidator.constraintHandlers[index];
|
|
316
|
+
const result = handler(input, validityKey);
|
|
339
317
|
input.error = _checkResult(result, input.value);
|
|
340
318
|
if (input.error) return;
|
|
341
319
|
}
|
|
342
320
|
for (const index in inputValidator.handlers) {
|
|
343
|
-
const
|
|
321
|
+
const handler = inputValidator.handlers[index];
|
|
322
|
+
const result = handler(input, fields);
|
|
344
323
|
input.error = _checkResult(result, input.value);
|
|
345
324
|
if (input.error) return;
|
|
346
325
|
}
|
|
@@ -350,7 +329,8 @@ const useFormHandlers = (getState, setState, validators = {}, optimized = false)
|
|
|
350
329
|
_debounce(input.debounce || 300, `${input.gid}-async`).then(() => {
|
|
351
330
|
const validateAsync = async () => {
|
|
352
331
|
for (const index in inputValidator.asyncHandlers) {
|
|
353
|
-
const
|
|
332
|
+
const handler = inputValidator.asyncHandlers[index];
|
|
333
|
+
const result = await handler(input, fields);
|
|
354
334
|
input.error = _checkResult(result, input.value);
|
|
355
335
|
if (input.error) break;
|
|
356
336
|
}
|
|
@@ -367,10 +347,6 @@ const useFormHandlers = (getState, setState, validators = {}, optimized = false)
|
|
|
367
347
|
});
|
|
368
348
|
}
|
|
369
349
|
};
|
|
370
|
-
const hasCustomValidation = input => {
|
|
371
|
-
const validator = validators[input.validatorKey || input.formKey] || validators['*'];
|
|
372
|
-
return validator && (validator.asyncHandlers.length > 0 || validator.handlers.length > 0);
|
|
373
|
-
};
|
|
374
350
|
return {
|
|
375
351
|
_updateInputHandler,
|
|
376
352
|
_viHandler,
|
|
@@ -380,12 +356,17 @@ const useFormHandlers = (getState, setState, validators = {}, optimized = false)
|
|
|
380
356
|
};
|
|
381
357
|
};
|
|
382
358
|
|
|
359
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
360
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return (String )(t); }
|
|
361
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
362
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
383
363
|
const GFormContext = createContext({});
|
|
384
364
|
const GFormContextProvider = ({
|
|
385
365
|
children,
|
|
386
366
|
initialState,
|
|
387
367
|
validators,
|
|
388
|
-
optimized
|
|
368
|
+
optimized,
|
|
369
|
+
formRef
|
|
389
370
|
}) => {
|
|
390
371
|
const stateRef = useRef(initialState);
|
|
391
372
|
const listeners = useRef(null);
|
|
@@ -396,19 +377,56 @@ const GFormContextProvider = ({
|
|
|
396
377
|
return;
|
|
397
378
|
}
|
|
398
379
|
stateRef.current = nextState;
|
|
399
|
-
listeners.current.forEach(
|
|
380
|
+
listeners.current.forEach(listener => listener());
|
|
400
381
|
}, []);
|
|
401
382
|
const handlers = useFormHandlers(() => stateRef.current, setState, validators, optimized);
|
|
383
|
+
const getInputElement = useCallback(formKey => {
|
|
384
|
+
if (formRef && formRef.current) {
|
|
385
|
+
return formRef.current[formKey];
|
|
386
|
+
}
|
|
387
|
+
}, []);
|
|
388
|
+
const registerField = useCallback(config => {
|
|
389
|
+
const prev = stateRef.current;
|
|
390
|
+
if (prev.fields[config.formKey]) {
|
|
391
|
+
{
|
|
392
|
+
console.warn(`DEV ONLY - [Duplicate Keys] - field with key '${config.formKey}' already defined.`);
|
|
393
|
+
}
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
const inputState = _buildInputInitialValues(config);
|
|
397
|
+
stateRef.current = _objectSpread(_objectSpread({}, prev), {}, {
|
|
398
|
+
fields: _objectSpread(_objectSpread({}, prev.fields), {}, {
|
|
399
|
+
[config.formKey]: _objectSpread(_objectSpread({}, inputState), {}, {
|
|
400
|
+
dispatchChanges: changes => handlers._dispatchChanges(changes, config.formKey)
|
|
401
|
+
})
|
|
402
|
+
})
|
|
403
|
+
});
|
|
404
|
+
}, [handlers]);
|
|
405
|
+
const unregisterField = useCallback(formKey => {
|
|
406
|
+
const prev = stateRef.current;
|
|
407
|
+
if (!prev.fields[formKey]) return;
|
|
408
|
+
const _prev$fields = prev.fields,
|
|
409
|
+
{
|
|
410
|
+
[formKey]: _
|
|
411
|
+
} = _prev$fields,
|
|
412
|
+
remainingFields = _objectWithoutProperties(_prev$fields, [formKey].map(_toPropertyKey));
|
|
413
|
+
stateRef.current = _objectSpread(_objectSpread({}, prev), {}, {
|
|
414
|
+
fields: remainingFields
|
|
415
|
+
});
|
|
416
|
+
listeners.current.forEach(listener => listener());
|
|
417
|
+
}, []);
|
|
402
418
|
const store = useMemo(() => {
|
|
403
419
|
if (!listeners.current) {
|
|
404
420
|
listeners.current = new Set();
|
|
421
|
+
stateRef.current = initialState;
|
|
422
|
+
for (const fieldKey in initialState.fields) {
|
|
423
|
+
initialState.fields[fieldKey].dispatchChanges = changes => handlers._dispatchChanges(changes, fieldKey);
|
|
424
|
+
}
|
|
405
425
|
} else {
|
|
406
426
|
listeners.current.clear();
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
for (const fieldKey in initialState.fields) {
|
|
411
|
-
initialState.fields[fieldKey].dispatchChanges = changes => handlers._dispatchChanges(changes, fieldKey);
|
|
427
|
+
for (const fieldKey in stateRef.current.fields) {
|
|
428
|
+
stateRef.current.fields[fieldKey].dispatchChanges = changes => handlers._dispatchChanges(changes, fieldKey);
|
|
429
|
+
}
|
|
412
430
|
}
|
|
413
431
|
return {
|
|
414
432
|
getState: () => stateRef.current,
|
|
@@ -417,7 +435,10 @@ const GFormContextProvider = ({
|
|
|
417
435
|
listeners.current.add(listener);
|
|
418
436
|
return () => listeners.current.delete(listener);
|
|
419
437
|
},
|
|
420
|
-
handlers
|
|
438
|
+
handlers,
|
|
439
|
+
registerField,
|
|
440
|
+
unregisterField,
|
|
441
|
+
getInputElement
|
|
421
442
|
};
|
|
422
443
|
}, [initialState]);
|
|
423
444
|
return React.createElement(GFormContext.Provider, {
|
|
@@ -426,7 +447,7 @@ const GFormContextProvider = ({
|
|
|
426
447
|
};
|
|
427
448
|
const useFormStore = () => {
|
|
428
449
|
const store = useContext(GFormContext);
|
|
429
|
-
if (!store.getState) throw new Error('
|
|
450
|
+
if (!store.getState) throw new Error('useFormStore must be used within `GForm` component');
|
|
430
451
|
return store;
|
|
431
452
|
};
|
|
432
453
|
const useFormSelector = selector => {
|
|
@@ -447,5 +468,5 @@ function createSelector(selectors, combiner) {
|
|
|
447
468
|
};
|
|
448
469
|
}
|
|
449
470
|
|
|
450
|
-
export { GFormContextProvider as G, _buildFormState as _, useFormSelector as a, _mergeRefs as b, _hasSubmitter as c,
|
|
471
|
+
export { GFormContextProvider as G, _buildFormState as _, useFormSelector as a, _mergeRefs as b, _hasSubmitter as c, _merge as d, createSelector as e, _debounce as f, useFormStore as u };
|
|
451
472
|
//# sourceMappingURL=shared.development.js.map
|