gform-react 2.5.4 → 2.6.2
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 +93 -79
- 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 +410 -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 -291
- package/native/dist/cjs/{gform-react-native.development.js → gform-react.development.js} +94 -80
- 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 +430 -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 +12 -9
- 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
|
@@ -20,74 +20,84 @@ const typeValueDict = {
|
|
|
20
20
|
checkbox: 'checked',
|
|
21
21
|
number: 'valueAsNumber'
|
|
22
22
|
};
|
|
23
|
-
const
|
|
23
|
+
const _generateIdUnsafe = () => (+new Date()).toString(36) + (1 - Math.random()).toString(36).substring(2, 16);
|
|
24
|
+
const _copyStateFields = (source, destination) => {
|
|
25
|
+
for (const key in destination.fields) {
|
|
26
|
+
const sourceField = source.fields[key];
|
|
27
|
+
const destField = destination.fields[key];
|
|
28
|
+
if (!sourceField || sourceField.type !== destField.type) continue;
|
|
29
|
+
destination.fields[key] = {
|
|
30
|
+
...destField,
|
|
31
|
+
...sourceField
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
};
|
|
24
35
|
const _buildFormInitialValues = (rows = []) => {
|
|
25
36
|
const fields = {};
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
};
|
|
69
|
-
Object.keys(fields[config.formKey]).forEach(key => {
|
|
70
|
-
if (typeof fields[config.formKey][key] === 'undefined') delete fields[config.formKey][key];
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
}
|
|
37
|
+
const inputs = _findInputs(rows);
|
|
38
|
+
inputs.forEach(config => {
|
|
39
|
+
if (fields[config.formKey]) {
|
|
40
|
+
console.warn(`DEV ONLY - [Duplicate Keys] - field with key '${config.formKey}' already defined.`);
|
|
41
|
+
}
|
|
42
|
+
const {
|
|
43
|
+
required = false,
|
|
44
|
+
max,
|
|
45
|
+
maxLength,
|
|
46
|
+
min,
|
|
47
|
+
minLength,
|
|
48
|
+
step,
|
|
49
|
+
pattern,
|
|
50
|
+
type = "text",
|
|
51
|
+
defaultValue,
|
|
52
|
+
value,
|
|
53
|
+
checked,
|
|
54
|
+
defaultChecked,
|
|
55
|
+
formKey,
|
|
56
|
+
debounce,
|
|
57
|
+
validatorKey
|
|
58
|
+
} = config;
|
|
59
|
+
const defaultProps = defaultFieldProps[type] || defaultFieldProps.text;
|
|
60
|
+
const inputValue = value || defaultValue || checked || defaultChecked || defaultProps.value;
|
|
61
|
+
fields[formKey] = {
|
|
62
|
+
formKey,
|
|
63
|
+
type,
|
|
64
|
+
required,
|
|
65
|
+
max,
|
|
66
|
+
maxLength,
|
|
67
|
+
min,
|
|
68
|
+
minLength,
|
|
69
|
+
step,
|
|
70
|
+
pattern,
|
|
71
|
+
value: inputValue,
|
|
72
|
+
validatorKey,
|
|
73
|
+
debounce,
|
|
74
|
+
dirty: false,
|
|
75
|
+
touched: false,
|
|
76
|
+
gid: _generateIdUnsafe()
|
|
77
|
+
};
|
|
78
|
+
});
|
|
74
79
|
return {
|
|
75
|
-
fields: fields
|
|
76
|
-
key: generateId()
|
|
80
|
+
fields: fields
|
|
77
81
|
};
|
|
78
82
|
};
|
|
79
83
|
const _findInputs = (root, total = []) => {
|
|
80
|
-
var _root$props, _root$props2;
|
|
81
84
|
if (!root) return total;
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
85
|
+
React.Children.forEach(root, child => {
|
|
86
|
+
if (!React.isValidElement(child)) return;
|
|
87
|
+
if (child.props) {
|
|
88
|
+
const {
|
|
89
|
+
formKey,
|
|
90
|
+
children
|
|
91
|
+
} = child.props;
|
|
92
|
+
if (formKey) {
|
|
93
|
+
total.push(child.props);
|
|
94
|
+
}
|
|
95
|
+
if (children) {
|
|
96
|
+
_findInputs(children, total);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
return total;
|
|
91
101
|
};
|
|
92
102
|
const _findValidityKey = (validity, exclude = []) => {
|
|
93
103
|
for (const key in validity) {
|
|
@@ -390,7 +400,7 @@ const useFormHandlers = (getState, setState, validators = {}, optimized = false)
|
|
|
390
400
|
const inputValidator = validators[input.validatorKey || input.formKey] || validators['*'];
|
|
391
401
|
{
|
|
392
402
|
if (validityKey && !(inputValidator !== null && inputValidator !== void 0 && inputValidator.hasConstraint(validityKey))) {
|
|
393
|
-
if (validityKey === 'typeMismatch') console.warn(`DEV ONLY - [Missing Validator] - the input '${input.formKey}' has described the constraint '${validityMap[validityKey]}' however, a correspond validator
|
|
403
|
+
if (validityKey === 'typeMismatch') console.warn(`DEV ONLY - [Missing Validator] - the input '${input.formKey}' has described the constraint '${validityMap[validityKey]}' however, a correspond validator/custom validation/pattern 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().withPatternMismatchMessage('pattern mismatch'),\n\t...\n}\n\nor either remove the constraint '${validityMap[validityKey]}' from the input props`);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`);
|
|
394
404
|
}
|
|
395
405
|
}
|
|
396
406
|
if (inputValidator) {
|
|
@@ -472,35 +482,40 @@ const GFormContextProvider = ({
|
|
|
472
482
|
optimized
|
|
473
483
|
}) => {
|
|
474
484
|
const stateRef = React.useRef(initialState);
|
|
475
|
-
const listeners = React.useRef(
|
|
485
|
+
const listeners = React.useRef(null);
|
|
476
486
|
const setState = React.useCallback(updater => {
|
|
477
487
|
stateRef.current = typeof updater === 'function' ? updater(stateRef.current) : updater;
|
|
478
488
|
listeners.current.forEach(l => l());
|
|
479
489
|
}, []);
|
|
480
490
|
const handlers = useFormHandlers(() => stateRef.current, setState, validators, optimized);
|
|
481
|
-
const
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
491
|
+
const store = React.useMemo(() => {
|
|
492
|
+
if (!listeners.current) {
|
|
493
|
+
listeners.current = new Set();
|
|
494
|
+
} else {
|
|
495
|
+
listeners.current.clear();
|
|
496
|
+
_copyStateFields(stateRef.current, initialState);
|
|
497
|
+
}
|
|
498
|
+
stateRef.current = initialState;
|
|
487
499
|
for (const fieldKey in initialState.fields) {
|
|
488
500
|
initialState.fields[fieldKey].dispatchChanges = changes => handlers._dispatchChanges(changes, fieldKey);
|
|
489
501
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
502
|
+
return {
|
|
503
|
+
getState: () => stateRef.current,
|
|
504
|
+
setState,
|
|
505
|
+
subscribe: listener => {
|
|
506
|
+
listeners.current.add(listener);
|
|
507
|
+
return () => listeners.current.delete(listener);
|
|
508
|
+
},
|
|
509
|
+
handlers
|
|
510
|
+
};
|
|
511
|
+
}, [initialState]);
|
|
497
512
|
return React.createElement(GFormContext.Provider, {
|
|
498
|
-
value: store
|
|
513
|
+
value: store
|
|
499
514
|
}, children);
|
|
500
515
|
};
|
|
501
516
|
const useFormStore = () => {
|
|
502
517
|
const store = React.useContext(GFormContext);
|
|
503
|
-
if (!store) throw new Error('useGFormStore must be used within `GForm` component');
|
|
518
|
+
if (!store.getState) throw new Error('useGFormStore must be used within `GForm` component');
|
|
504
519
|
return store;
|
|
505
520
|
};
|
|
506
521
|
const useFormSelector = selector => {
|
|
@@ -660,7 +675,6 @@ const GForm = React.forwardRef(({
|
|
|
660
675
|
return _buildFormInitialValues(typeof children === 'function' ? children({}) : children);
|
|
661
676
|
}, [children]);
|
|
662
677
|
return React.createElement(GFormContextProvider, {
|
|
663
|
-
key: initialState.key,
|
|
664
678
|
initialState: initialState,
|
|
665
679
|
validators: validators,
|
|
666
680
|
optimized: optimized
|