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.
Files changed (55) hide show
  1. package/dist/cjs/gform-react.development.js +97 -82
  2. package/dist/cjs/gform-react.development.js.map +1 -1
  3. package/dist/cjs/gform-react.production.js +1 -1
  4. package/dist/cjs/gform-react.production.js.map +1 -1
  5. package/dist/cjs/index.js +8 -7
  6. package/dist/esm/GForm.development.js +160 -0
  7. package/dist/esm/GForm.development.js.map +1 -0
  8. package/dist/esm/GForm.production.js +1 -1
  9. package/dist/esm/GForm.production.js.map +1 -1
  10. package/dist/esm/GInput.development.js +105 -0
  11. package/dist/esm/GInput.development.js.map +1 -0
  12. package/dist/esm/GInput.production.js +1 -1
  13. package/dist/esm/GInput.production.js.map +1 -1
  14. package/dist/esm/GValidator.development.js +118 -0
  15. package/dist/esm/GValidator.development.js.map +1 -0
  16. package/dist/esm/GValidator.production.js +1 -1
  17. package/dist/esm/GValidator.production.js.map +1 -1
  18. package/dist/esm/index.development.js +4 -761
  19. package/dist/esm/index.js +4 -4
  20. package/dist/esm/shared.development.js +411 -0
  21. package/dist/esm/shared.development.js.map +1 -0
  22. package/dist/esm/shared.production.js +1 -1
  23. package/dist/esm/shared.production.js.map +1 -1
  24. package/dist/esm/useFormSelector.development.js +6 -0
  25. package/dist/esm/useFormSelector.development.js.map +1 -0
  26. package/dist/esm/useFormSelector.production.js +2 -0
  27. package/dist/esm/useFormSelector.production.js.map +1 -0
  28. package/dist/index.d.ts +260 -290
  29. package/native/dist/cjs/{gform-react-native.development.js → gform-react.development.js} +94 -79
  30. package/native/dist/cjs/gform-react.development.js.map +1 -0
  31. package/native/dist/cjs/gform-react.production.js +2 -0
  32. package/native/dist/cjs/gform-react.production.js.map +1 -0
  33. package/native/dist/cjs/index.js +8 -7
  34. package/native/dist/esm/RNGForm.development.js +105 -0
  35. package/native/dist/esm/RNGForm.development.js.map +1 -0
  36. package/native/dist/esm/RNGForm.production.js +1 -1
  37. package/native/dist/esm/RNGForm.production.js.map +1 -1
  38. package/native/dist/esm/RNGInput.development.js +83 -0
  39. package/native/dist/esm/RNGInput.development.js.map +1 -0
  40. package/native/dist/esm/RNGInput.production.js +1 -1
  41. package/native/dist/esm/RNGInput.production.js.map +1 -1
  42. package/native/dist/esm/index.development.js +2 -592
  43. package/native/dist/esm/index.js +2 -2
  44. package/native/dist/esm/shared.development.js +431 -0
  45. package/native/dist/esm/shared.development.js.map +1 -0
  46. package/native/dist/esm/shared.production.js +1 -1
  47. package/native/dist/esm/shared.production.js.map +1 -1
  48. package/native/dist/index.d.ts +183 -73
  49. package/package.json +10 -7
  50. package/dist/esm/index.development.js.map +0 -1
  51. package/native/dist/cjs/gform-react-native.development.js.map +0 -1
  52. package/native/dist/cjs/gform-react-native.production.js +0 -2
  53. package/native/dist/cjs/gform-react-native.production.js.map +0 -1
  54. package/native/dist/esm/index.development.js.map +0 -1
  55. package/native/package.json +0 -15
@@ -20,74 +20,84 @@ const typeValueDict = {
20
20
  checkbox: 'checked',
21
21
  number: 'valueAsNumber'
22
22
  };
23
- const generateId = () => (+new Date()).toString(36) + (1 - Math.random()).toString(36).substring(2, 16);
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
- if (!Array.isArray(rows)) rows = [rows];
27
- for (const row of rows) {
28
- const inputConfigs = _findInputs(row);
29
- inputConfigs.forEach(config => {
30
- if (fields[config.formKey]) {
31
- console.warn(`DEV ONLY - [Duplicate Keys] - field with key '${config.formKey}' has already been defined.`);
32
- }
33
- const {
34
- required = false,
35
- max,
36
- maxLength,
37
- min,
38
- minLength,
39
- step,
40
- pattern,
41
- type = 'text',
42
- defaultValue,
43
- value,
44
- checked,
45
- defaultChecked,
46
- formKey,
47
- debounce,
48
- validatorKey
49
- } = config;
50
- const defaultProps = defaultFieldProps[type] || defaultFieldProps.text;
51
- const inputValue = value || defaultValue || checked || defaultChecked || defaultProps.value;
52
- fields[config.formKey] = {
53
- formKey,
54
- type,
55
- required,
56
- max,
57
- maxLength,
58
- min,
59
- minLength,
60
- step,
61
- pattern,
62
- value: inputValue,
63
- validatorKey,
64
- debounce,
65
- dirty: false,
66
- touched: false,
67
- gid: generateId()
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
- if (Array.isArray(root)) {
83
- root.forEach(element => _findInputs(element, total));
84
- return total;
85
- }
86
- if ((_root$props = root.props) !== null && _root$props !== void 0 && _root$props.formKey) {
87
- total.push(root.props);
88
- return total;
89
- }
90
- return _findInputs((_root$props2 = root.props) === null || _root$props2 === void 0 ? void 0 : _root$props2.children, total);
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 / custom validation / pattern validator are 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`);
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(new Set());
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 getState = React.useCallback(() => stateRef.current, []);
482
- const subscribe = React.useCallback(listener => {
483
- listeners.current.add(listener);
484
- return () => listeners.current.delete(listener);
485
- }, []);
486
- React.useEffect(() => {
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
- const store = React.useRef({
492
- getState,
493
- setState,
494
- subscribe,
495
- handlers
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.current
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 isFormInvalid = _checkIfFormIsValid(fields);
568
+ const isFormValid = _checkIfFormIsValid(fields);
553
569
  const formState = {
554
570
  ...fields,
555
- isValid: !isFormInvalid,
556
- isInvalid: isFormInvalid,
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({