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
|
@@ -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,30 +482,36 @@ 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
|
+
console.log(`form changed stated from`, stateRef.current, '\nto\n', initialState);
|
|
496
|
+
listeners.current.clear();
|
|
497
|
+
_copyStateFields(stateRef.current, initialState);
|
|
498
|
+
}
|
|
499
|
+
stateRef.current = initialState;
|
|
487
500
|
for (const fieldKey in initialState.fields) {
|
|
488
501
|
initialState.fields[fieldKey].dispatchChanges = changes => handlers._dispatchChanges(changes, fieldKey);
|
|
489
502
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
503
|
+
return {
|
|
504
|
+
getState: () => stateRef.current,
|
|
505
|
+
setState,
|
|
506
|
+
subscribe: listener => {
|
|
507
|
+
listeners.current.add(listener);
|
|
508
|
+
return () => listeners.current.delete(listener);
|
|
509
|
+
},
|
|
510
|
+
handlers
|
|
511
|
+
};
|
|
512
|
+
}, [initialState]);
|
|
497
513
|
return React.createElement(GFormContext.Provider, {
|
|
498
|
-
value: store
|
|
514
|
+
value: store
|
|
499
515
|
}, children);
|
|
500
516
|
};
|
|
501
517
|
const useFormStore = () => {
|
|
@@ -549,11 +565,11 @@ const FormRenderer = React.forwardRef(({
|
|
|
549
565
|
formRef.current = element;
|
|
550
566
|
}, [ref]);
|
|
551
567
|
const getFormState = React.useCallback(() => {
|
|
552
|
-
const
|
|
568
|
+
const isFormValid = _checkIfFormIsValid(fields);
|
|
553
569
|
const formState = {
|
|
554
570
|
...fields,
|
|
555
|
-
isValid:
|
|
556
|
-
isInvalid:
|
|
571
|
+
isValid: isFormValid,
|
|
572
|
+
isInvalid: !isFormValid,
|
|
557
573
|
toRawData: options => _toRawData(fields, options),
|
|
558
574
|
toFormData: () => _toFormData(formRef.current),
|
|
559
575
|
toURLSearchParams: _toURLSearchParams,
|
|
@@ -660,7 +676,6 @@ const GForm = React.forwardRef(({
|
|
|
660
676
|
return _buildFormInitialValues(typeof children === 'function' ? children({}) : children);
|
|
661
677
|
}, [children]);
|
|
662
678
|
return React.createElement(GFormContextProvider, {
|
|
663
|
-
key: initialState.key,
|
|
664
679
|
initialState: initialState,
|
|
665
680
|
validators: validators,
|
|
666
681
|
optimized: optimized
|
|
@@ -728,7 +743,7 @@ const _GInput = React.forwardRef(({
|
|
|
728
743
|
} : (e, unknown) => {
|
|
729
744
|
store.handlers._updateInputHandler(inputState, e, unknown);
|
|
730
745
|
};
|
|
731
|
-
if (!inputState.touched) {
|
|
746
|
+
if (!inputState.touched && inputState.dispatchChanges) {
|
|
732
747
|
_props.onFocus = rest.onFocus ? e => {
|
|
733
748
|
rest.onFocus(e);
|
|
734
749
|
inputState.dispatchChanges({
|