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