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
|
@@ -1,592 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
|
|
4
|
-
import React, { useContext, createContext, useSyncExternalStore, useRef, useCallback, useEffect, forwardRef, useMemo, memo } from 'react';
|
|
5
|
-
import '@babel/runtime/helpers/esm/defineProperty';
|
|
6
|
-
import { TextInput } from 'react-native';
|
|
7
|
-
|
|
8
|
-
const isObject = o => o && typeof o === 'object' && !Array.isArray(o);
|
|
9
|
-
const defaultFieldProps = {
|
|
10
|
-
text: {
|
|
11
|
-
value: ''
|
|
12
|
-
},
|
|
13
|
-
checkbox: {
|
|
14
|
-
value: false
|
|
15
|
-
},
|
|
16
|
-
number: {
|
|
17
|
-
value: 0
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
const typeValueDict = {
|
|
21
|
-
checkbox: 'checked',
|
|
22
|
-
number: 'valueAsNumber'
|
|
23
|
-
};
|
|
24
|
-
const generateId = () => (+new Date()).toString(36) + (1 - Math.random()).toString(36).substring(2, 16);
|
|
25
|
-
const _buildFormInitialValues = (rows = []) => {
|
|
26
|
-
const fields = {};
|
|
27
|
-
if (!Array.isArray(rows)) rows = [rows];
|
|
28
|
-
for (const row of rows) {
|
|
29
|
-
const inputConfigs = _findInputs(row);
|
|
30
|
-
inputConfigs.forEach(config => {
|
|
31
|
-
if (fields[config.formKey]) {
|
|
32
|
-
console.warn(`DEV ONLY - [Duplicate Keys] - field with key '${config.formKey}' has already been defined.`);
|
|
33
|
-
}
|
|
34
|
-
const {
|
|
35
|
-
required = false,
|
|
36
|
-
max,
|
|
37
|
-
maxLength,
|
|
38
|
-
min,
|
|
39
|
-
minLength,
|
|
40
|
-
step,
|
|
41
|
-
pattern,
|
|
42
|
-
type = 'text',
|
|
43
|
-
defaultValue,
|
|
44
|
-
value,
|
|
45
|
-
checked,
|
|
46
|
-
defaultChecked,
|
|
47
|
-
formKey,
|
|
48
|
-
debounce,
|
|
49
|
-
validatorKey
|
|
50
|
-
} = config;
|
|
51
|
-
const defaultProps = defaultFieldProps[type] || defaultFieldProps.text;
|
|
52
|
-
const inputValue = value || defaultValue || checked || defaultChecked || defaultProps.value;
|
|
53
|
-
fields[config.formKey] = {
|
|
54
|
-
formKey,
|
|
55
|
-
type,
|
|
56
|
-
required,
|
|
57
|
-
max,
|
|
58
|
-
maxLength,
|
|
59
|
-
min,
|
|
60
|
-
minLength,
|
|
61
|
-
step,
|
|
62
|
-
pattern,
|
|
63
|
-
value: inputValue,
|
|
64
|
-
validatorKey,
|
|
65
|
-
debounce,
|
|
66
|
-
dirty: false,
|
|
67
|
-
touched: false,
|
|
68
|
-
gid: generateId()
|
|
69
|
-
};
|
|
70
|
-
Object.keys(fields[config.formKey]).forEach(key => {
|
|
71
|
-
if (typeof fields[config.formKey][key] === 'undefined') delete fields[config.formKey][key];
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
return {
|
|
76
|
-
fields: fields,
|
|
77
|
-
key: generateId()
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
const _findInputs = (root, total = []) => {
|
|
81
|
-
var _root$props, _root$props2;
|
|
82
|
-
if (!root) return total;
|
|
83
|
-
if (Array.isArray(root)) {
|
|
84
|
-
root.forEach(element => _findInputs(element, total));
|
|
85
|
-
return total;
|
|
86
|
-
}
|
|
87
|
-
if ((_root$props = root.props) !== null && _root$props !== void 0 && _root$props.formKey) {
|
|
88
|
-
total.push(root.props);
|
|
89
|
-
return total;
|
|
90
|
-
}
|
|
91
|
-
return _findInputs((_root$props2 = root.props) === null || _root$props2 === void 0 ? void 0 : _root$props2.children, total);
|
|
92
|
-
};
|
|
93
|
-
const _findValidityKey = (validity, exclude = []) => {
|
|
94
|
-
for (const key in validity) {
|
|
95
|
-
if (exclude.includes(key)) continue;
|
|
96
|
-
if (key !== 'valid' && validity[key]) {
|
|
97
|
-
return key;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
const _checkTypeMismatch = input => {
|
|
102
|
-
var _input$value;
|
|
103
|
-
const value = (_input$value = input.value) === null || _input$value === void 0 ? void 0 : _input$value.toString().trim();
|
|
104
|
-
if (!value) return false;
|
|
105
|
-
switch (input.type) {
|
|
106
|
-
case 'email':
|
|
107
|
-
return !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
|
|
108
|
-
case 'url':
|
|
109
|
-
try {
|
|
110
|
-
new URL(value);
|
|
111
|
-
return false;
|
|
112
|
-
} catch (_unused) {
|
|
113
|
-
return true;
|
|
114
|
-
}
|
|
115
|
-
case 'tel':
|
|
116
|
-
return !/^\+?[0-9\s\-().]{7,}$/.test(value);
|
|
117
|
-
default:
|
|
118
|
-
return false;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
const _toRawData = (fields, options = {}) => {
|
|
122
|
-
const data = {};
|
|
123
|
-
const {
|
|
124
|
-
include,
|
|
125
|
-
exclude,
|
|
126
|
-
transform
|
|
127
|
-
} = options;
|
|
128
|
-
if (include) {
|
|
129
|
-
include.forEach(key => {
|
|
130
|
-
var _fields$key;
|
|
131
|
-
return data[key] = (_fields$key = fields[key]) === null || _fields$key === void 0 ? void 0 : _fields$key.value;
|
|
132
|
-
});
|
|
133
|
-
} else for (const f in fields) {
|
|
134
|
-
data[f] = fields[f].value;
|
|
135
|
-
}
|
|
136
|
-
exclude === null || exclude === void 0 || exclude.forEach(key => delete data[key]);
|
|
137
|
-
if (transform) {
|
|
138
|
-
for (const key in transform) {
|
|
139
|
-
var _fields$key2;
|
|
140
|
-
const set = transform[key];
|
|
141
|
-
data[key] = set(((_fields$key2 = fields[key]) === null || _fields$key2 === void 0 ? void 0 : _fields$key2.value) || fields[key]);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return data;
|
|
145
|
-
};
|
|
146
|
-
function _toURLSearchParams(options) {
|
|
147
|
-
let data;
|
|
148
|
-
if (options) {
|
|
149
|
-
const {
|
|
150
|
-
exclude,
|
|
151
|
-
include,
|
|
152
|
-
transform
|
|
153
|
-
} = options;
|
|
154
|
-
if (include) {
|
|
155
|
-
data = {};
|
|
156
|
-
include.forEach(key => {
|
|
157
|
-
var _this$key;
|
|
158
|
-
return data[key] = (_this$key = this[key]) === null || _this$key === void 0 ? void 0 : _this$key.value;
|
|
159
|
-
});
|
|
160
|
-
} else {
|
|
161
|
-
data = this.toRawData();
|
|
162
|
-
exclude === null || exclude === void 0 || exclude.forEach(key => delete data[key]);
|
|
163
|
-
}
|
|
164
|
-
if (transform) {
|
|
165
|
-
for (const key in transform) {
|
|
166
|
-
var _this$key2;
|
|
167
|
-
const set = transform[key];
|
|
168
|
-
data[key] = set((_this$key2 = this[key]) === null || _this$key2 === void 0 ? void 0 : _this$key2.value);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
} else data = this.toRawData();
|
|
172
|
-
return new URLSearchParams(data);
|
|
173
|
-
}
|
|
174
|
-
function __debounce(timeout, id) {
|
|
175
|
-
return new Promise(resolve => {
|
|
176
|
-
var _this$id;
|
|
177
|
-
if ((_this$id = this[id]) !== null && _this$id !== void 0 && _this$id.timerId) clearTimeout(this[id].timerId);
|
|
178
|
-
const timerId = setTimeout(() => resolve(), timeout);
|
|
179
|
-
if (this[id]) {
|
|
180
|
-
this[id].timerId = timerId;
|
|
181
|
-
} else this[id] = {
|
|
182
|
-
timerId
|
|
183
|
-
};
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
const _debounce = __debounce.bind({});
|
|
187
|
-
const _extractValue = (e, unknown) => {
|
|
188
|
-
if (e !== null && e !== void 0 && e.target) {
|
|
189
|
-
if (Object.hasOwn(typeValueDict, e.target.type)) return e.target[typeValueDict[e.target.type]];
|
|
190
|
-
return e.target.value;
|
|
191
|
-
}
|
|
192
|
-
return (e === null || e === void 0 ? void 0 : e.value) || (isObject(unknown) ? unknown.value : unknown);
|
|
193
|
-
};
|
|
194
|
-
const _checkResult = (handlerResult, value) => typeof handlerResult === 'boolean' ? handlerResult : typeof value === 'string' ? typeof handlerResult === 'string' ? !new RegExp(handlerResult).test(value) : !handlerResult.test(value) : false;
|
|
195
|
-
const _merge = (target = {}, ...nodes) => {
|
|
196
|
-
if (!nodes.length) return target;
|
|
197
|
-
const next = nodes.shift();
|
|
198
|
-
if (isObject(next)) {
|
|
199
|
-
for (const key in next) {
|
|
200
|
-
target[key] = target[key] ? _objectSpread(_objectSpread({}, target[key]), next[key]) : next[key];
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return _merge(target, ...nodes);
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
let handlersMap;
|
|
207
|
-
let validityMap;
|
|
208
|
-
{
|
|
209
|
-
handlersMap = {
|
|
210
|
-
minLength: 'withMinLengthMessage',
|
|
211
|
-
maxLength: 'withMaxLengthMessage',
|
|
212
|
-
required: 'withRequiredMessage',
|
|
213
|
-
pattern: 'withPatternMismatchMessage',
|
|
214
|
-
min: 'withRangeUnderflowMessage',
|
|
215
|
-
max: 'withRangeOverflowMessage',
|
|
216
|
-
step: 'withStepMismatchMessage',
|
|
217
|
-
type: 'withTypeMismatchMessage'
|
|
218
|
-
};
|
|
219
|
-
validityMap = {
|
|
220
|
-
tooShort: 'minLength',
|
|
221
|
-
valueMissing: 'required',
|
|
222
|
-
tooLong: 'maxLength',
|
|
223
|
-
patternMismatch: 'pattern',
|
|
224
|
-
rangeOverflow: 'max',
|
|
225
|
-
rangeUnderflow: 'min',
|
|
226
|
-
stepMismatch: 'step',
|
|
227
|
-
typeMismatch: 'type'
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const useFormHandlers = (getState, setState, validators = {}, optimized = false) => {
|
|
232
|
-
const _viHandler = (input, e) => {
|
|
233
|
-
if (!input) return;
|
|
234
|
-
const element = e && e.target;
|
|
235
|
-
const hasInitialValue = !input.dirty && input.value && !input.touched;
|
|
236
|
-
if (!element && !hasInitialValue) return;
|
|
237
|
-
if (typeof document !== 'undefined' && (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement)) {
|
|
238
|
-
if (!input.checkValidity) input.checkValidity = () => element.checkValidity();
|
|
239
|
-
if (hasInitialValue) {
|
|
240
|
-
_checkInputManually(input);
|
|
241
|
-
_dispatchChanges(input, input.formKey);
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
element.setCustomValidity('');
|
|
245
|
-
const exclude = input.type && (input.pattern || hasCustomValidation(input)) ? ['typeMismatch'] : [];
|
|
246
|
-
const validityKey = _findValidityKey(element.validity, exclude);
|
|
247
|
-
_validateInput(input, validityKey, v => element.setCustomValidity(v));
|
|
248
|
-
if (!validityKey && input.error) {
|
|
249
|
-
element.setCustomValidity(input.errorText || 'error');
|
|
250
|
-
}
|
|
251
|
-
_dispatchChanges(input, input.formKey);
|
|
252
|
-
} else {
|
|
253
|
-
input.checkValidity = () => _checkInputManually(input);
|
|
254
|
-
input.checkValidity();
|
|
255
|
-
_dispatchChanges(input, input.formKey);
|
|
256
|
-
}
|
|
257
|
-
};
|
|
258
|
-
const _checkInputManually = input => {
|
|
259
|
-
const exclude = input.type && (input.pattern || hasCustomValidation(input)) ? ['typeMismatch'] : [];
|
|
260
|
-
let validityKey = _findValidityKey({
|
|
261
|
-
valueMissing: input.required && !input.value || false,
|
|
262
|
-
typeMismatch: _checkTypeMismatch(input),
|
|
263
|
-
tooShort: input.minLength && input.value.toString().length < input.minLength || false,
|
|
264
|
-
tooLong: input.maxLength && input.value.toString().length > input.maxLength || false,
|
|
265
|
-
patternMismatch: input.pattern && _checkResult(input.pattern, input.value) || false,
|
|
266
|
-
rangeUnderflow: input.min && Number(input.value) < Number(input.min) || false,
|
|
267
|
-
rangeOverflow: input.max && Number(input.value) > Number(input.max) || false
|
|
268
|
-
}, exclude);
|
|
269
|
-
if (!validityKey && input.error) {
|
|
270
|
-
validityKey = 'customError';
|
|
271
|
-
}
|
|
272
|
-
_validateInput(input, validityKey);
|
|
273
|
-
return !input.error;
|
|
274
|
-
};
|
|
275
|
-
const _updateInputHandler = (input, e, unknown) => {
|
|
276
|
-
input.value = _extractValue(e, unknown);
|
|
277
|
-
_viHandler(input, e);
|
|
278
|
-
};
|
|
279
|
-
const _validateInput = (input, validityKey, setValidity) => {
|
|
280
|
-
const inputValidator = validators[input.validatorKey || input.formKey] || validators['*'];
|
|
281
|
-
{
|
|
282
|
-
if (validityKey && !(inputValidator !== null && inputValidator !== void 0 && inputValidator.hasConstraint(validityKey))) {
|
|
283
|
-
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`);
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
if (inputValidator) {
|
|
287
|
-
__validateInput(input, inputValidator, validityKey, setValidity);
|
|
288
|
-
}
|
|
289
|
-
input.touched = true;
|
|
290
|
-
};
|
|
291
|
-
const _dispatchChanges = (changes, key) => setState(prev => {
|
|
292
|
-
if (key) {
|
|
293
|
-
return _objectSpread(_objectSpread({}, prev), {}, {
|
|
294
|
-
fields: _objectSpread(_objectSpread({}, prev.fields), {}, {
|
|
295
|
-
[key]: _objectSpread(_objectSpread({}, prev.fields[key]), changes)
|
|
296
|
-
})
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
return _objectSpread(_objectSpread({}, prev), changes);
|
|
300
|
-
});
|
|
301
|
-
const __validateInput = (input, inputValidator, validityKey, setValidity) => {
|
|
302
|
-
const fields = getState().fields;
|
|
303
|
-
for (const index in inputValidator.constraintHandlers) {
|
|
304
|
-
const result = inputValidator.constraintHandlers[index](input, validityKey);
|
|
305
|
-
input.error = _checkResult(result, input.value);
|
|
306
|
-
if (input.error) return;
|
|
307
|
-
}
|
|
308
|
-
for (const index in inputValidator.handlers) {
|
|
309
|
-
const result = inputValidator.handlers[index](input, fields);
|
|
310
|
-
input.error = _checkResult(result, input.value);
|
|
311
|
-
if (input.error) return;
|
|
312
|
-
}
|
|
313
|
-
input.errorText = '';
|
|
314
|
-
if (inputValidator.asyncHandlers.length) {
|
|
315
|
-
input.error = true;
|
|
316
|
-
_debounce(input.debounce || 300, `${input.gid}-async`).then(() => {
|
|
317
|
-
const validateAsync = async () => {
|
|
318
|
-
for (const index in inputValidator.asyncHandlers) {
|
|
319
|
-
const result = await inputValidator.asyncHandlers[index](input, fields);
|
|
320
|
-
input.error = _checkResult(result, input.value);
|
|
321
|
-
if (input.error) break;
|
|
322
|
-
}
|
|
323
|
-
if (!input.error) input.errorText = '';
|
|
324
|
-
_dispatchChanges({
|
|
325
|
-
error: input.error,
|
|
326
|
-
errorText: input.errorText
|
|
327
|
-
}, input.formKey);
|
|
328
|
-
if (setValidity) {
|
|
329
|
-
setValidity(input.errorText);
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
validateAsync();
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
};
|
|
336
|
-
const hasCustomValidation = input => {
|
|
337
|
-
const validator = validators[input.validatorKey || input.formKey] || validators['*'];
|
|
338
|
-
return validator && (validator.asyncHandlers.length > 0 || validator.handlers.length > 0);
|
|
339
|
-
};
|
|
340
|
-
return {
|
|
341
|
-
_updateInputHandler,
|
|
342
|
-
_viHandler,
|
|
343
|
-
_dispatchChanges,
|
|
344
|
-
optimized,
|
|
345
|
-
_createInputChecker: _checkInputManually
|
|
346
|
-
};
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
const GFormContext = createContext({});
|
|
350
|
-
const GFormContextProvider = ({
|
|
351
|
-
children,
|
|
352
|
-
initialState,
|
|
353
|
-
validators,
|
|
354
|
-
optimized
|
|
355
|
-
}) => {
|
|
356
|
-
const stateRef = useRef(initialState);
|
|
357
|
-
const listeners = useRef(new Set());
|
|
358
|
-
const setState = useCallback(updater => {
|
|
359
|
-
stateRef.current = typeof updater === 'function' ? updater(stateRef.current) : updater;
|
|
360
|
-
listeners.current.forEach(l => l());
|
|
361
|
-
}, []);
|
|
362
|
-
const handlers = useFormHandlers(() => stateRef.current, setState, validators, optimized);
|
|
363
|
-
const getState = useCallback(() => stateRef.current, []);
|
|
364
|
-
const subscribe = useCallback(listener => {
|
|
365
|
-
listeners.current.add(listener);
|
|
366
|
-
return () => listeners.current.delete(listener);
|
|
367
|
-
}, []);
|
|
368
|
-
useEffect(() => {
|
|
369
|
-
for (const fieldKey in initialState.fields) {
|
|
370
|
-
initialState.fields[fieldKey].dispatchChanges = changes => handlers._dispatchChanges(changes, fieldKey);
|
|
371
|
-
}
|
|
372
|
-
}, []);
|
|
373
|
-
const store = useRef({
|
|
374
|
-
getState,
|
|
375
|
-
setState,
|
|
376
|
-
subscribe,
|
|
377
|
-
handlers
|
|
378
|
-
});
|
|
379
|
-
return React.createElement(GFormContext.Provider, {
|
|
380
|
-
value: store.current
|
|
381
|
-
}, children);
|
|
382
|
-
};
|
|
383
|
-
const useFormStore = () => {
|
|
384
|
-
const store = useContext(GFormContext);
|
|
385
|
-
if (!store) throw new Error('useGFormStore must be used within `GForm` component');
|
|
386
|
-
return store;
|
|
387
|
-
};
|
|
388
|
-
const useFormSelector = selector => {
|
|
389
|
-
const store = useFormStore();
|
|
390
|
-
return useSyncExternalStore(store.subscribe, () => selector(store.getState()), () => selector(store.getState()));
|
|
391
|
-
};
|
|
392
|
-
function createSelector(selectors, combiner) {
|
|
393
|
-
let lastArgs = [];
|
|
394
|
-
let lastResult;
|
|
395
|
-
return state => {
|
|
396
|
-
const args = selectors.map(fn => fn(state));
|
|
397
|
-
if (lastArgs.length === args.length && args.every((val, i) => val === lastArgs[i])) {
|
|
398
|
-
return lastResult;
|
|
399
|
-
}
|
|
400
|
-
lastArgs = args;
|
|
401
|
-
lastResult = combiner(...args);
|
|
402
|
-
return lastResult;
|
|
403
|
-
};
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
const selectFields = [state => state.fields];
|
|
407
|
-
const selectFirstInvalidField = createSelector(selectFields, fields => {
|
|
408
|
-
for (const f in fields) {
|
|
409
|
-
if (fields[f].error) {
|
|
410
|
-
return true;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
return false;
|
|
414
|
-
});
|
|
415
|
-
const makeSelectFields = (keys = []) => createSelector(selectFields, fields => {
|
|
416
|
-
const selected = keys.map(key => fields[key]).filter(Boolean);
|
|
417
|
-
return selected.length ? selected : null;
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
const _excluded$1 = ["stateRef", "children", "onInit", "el"],
|
|
421
|
-
_excluded2 = ["children", "validators"];
|
|
422
|
-
const FormRenderer = forwardRef((_ref, ref) => {
|
|
423
|
-
let {
|
|
424
|
-
stateRef,
|
|
425
|
-
children,
|
|
426
|
-
onInit,
|
|
427
|
-
el: El
|
|
428
|
-
} = _ref,
|
|
429
|
-
rest = _objectWithoutProperties(_ref, _excluded$1);
|
|
430
|
-
const {
|
|
431
|
-
getState,
|
|
432
|
-
handlers
|
|
433
|
-
} = useFormStore();
|
|
434
|
-
const isFormInvalid = useFormSelector(selectFirstInvalidField);
|
|
435
|
-
const getFormState = useCallback(() => {
|
|
436
|
-
const fields = getState().fields;
|
|
437
|
-
const formState = _objectSpread(_objectSpread({}, fields), {}, {
|
|
438
|
-
isValid: !isFormInvalid,
|
|
439
|
-
isInvalid: isFormInvalid,
|
|
440
|
-
toRawData: options => _toRawData(fields, options),
|
|
441
|
-
toURLSearchParams: _toURLSearchParams,
|
|
442
|
-
checkValidity: () => {
|
|
443
|
-
for (const i in fields) {
|
|
444
|
-
const valid = fields[i].checkValidity();
|
|
445
|
-
if (!valid) {
|
|
446
|
-
return false;
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
return true;
|
|
450
|
-
},
|
|
451
|
-
dispatchChanges: changes => handlers._dispatchChanges({
|
|
452
|
-
fields: _merge({}, fields, changes)
|
|
453
|
-
})
|
|
454
|
-
});
|
|
455
|
-
if (stateRef) stateRef.current = formState;
|
|
456
|
-
return formState;
|
|
457
|
-
}, [isFormInvalid]);
|
|
458
|
-
const formComponent = useMemo(() => {
|
|
459
|
-
const state = getFormState();
|
|
460
|
-
const formChildren = typeof children === 'function' ? children(state) : children;
|
|
461
|
-
return React.createElement(El, _extends({}, rest, {
|
|
462
|
-
ref: ref
|
|
463
|
-
}), formChildren);
|
|
464
|
-
}, [getFormState, children]);
|
|
465
|
-
useEffect(() => {
|
|
466
|
-
const state = getFormState();
|
|
467
|
-
if (onInit) {
|
|
468
|
-
const changes = onInit(state);
|
|
469
|
-
if (changes) {
|
|
470
|
-
const _handler = _c => handlers._dispatchChanges({
|
|
471
|
-
fields: _merge({}, state, _c)
|
|
472
|
-
});
|
|
473
|
-
if (changes instanceof Promise) {
|
|
474
|
-
changes.then(_handler);
|
|
475
|
-
} else _handler(changes);
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
const dispatchers = {};
|
|
479
|
-
const fields = getState().fields;
|
|
480
|
-
for (const fieldKey in fields) {
|
|
481
|
-
dispatchers[fieldKey] = {
|
|
482
|
-
dispatchChanges: changes => handlers._dispatchChanges(changes, fieldKey),
|
|
483
|
-
checkValidity: () => {
|
|
484
|
-
const result = handlers._createInputChecker(state[fieldKey]);
|
|
485
|
-
handlers._dispatchChanges(state[fieldKey], fieldKey);
|
|
486
|
-
return result;
|
|
487
|
-
}
|
|
488
|
-
};
|
|
489
|
-
const field = fields[fieldKey];
|
|
490
|
-
if (!field.value) continue;
|
|
491
|
-
handlers._viHandler(field);
|
|
492
|
-
}
|
|
493
|
-
handlers._dispatchChanges({
|
|
494
|
-
fields: _merge(dispatchers, state)
|
|
495
|
-
});
|
|
496
|
-
}, [getFormState]);
|
|
497
|
-
return formComponent;
|
|
498
|
-
});
|
|
499
|
-
const RNGForm = forwardRef((_ref2, ref) => {
|
|
500
|
-
let {
|
|
501
|
-
children,
|
|
502
|
-
validators
|
|
503
|
-
} = _ref2,
|
|
504
|
-
props = _objectWithoutProperties(_ref2, _excluded2);
|
|
505
|
-
const initialState = useMemo(() => {
|
|
506
|
-
return _buildFormInitialValues(typeof children === 'function' ? children({}) : children);
|
|
507
|
-
}, [children]);
|
|
508
|
-
return React.createElement(GFormContextProvider, {
|
|
509
|
-
key: initialState.key,
|
|
510
|
-
initialState: initialState,
|
|
511
|
-
validators: validators
|
|
512
|
-
}, React.createElement(FormRenderer, _extends({
|
|
513
|
-
ref: ref
|
|
514
|
-
}, props), children));
|
|
515
|
-
});
|
|
516
|
-
|
|
517
|
-
const _excluded = ["formKey", "element", "type", "fetch", "fetchDeps", "debounce", "defaultValue", "validatorKey", "value"];
|
|
518
|
-
const _RNGInput = forwardRef((_ref, ref) => {
|
|
519
|
-
let {
|
|
520
|
-
formKey,
|
|
521
|
-
element,
|
|
522
|
-
type,
|
|
523
|
-
fetch,
|
|
524
|
-
fetchDeps,
|
|
525
|
-
debounce = 300,
|
|
526
|
-
defaultValue,
|
|
527
|
-
validatorKey,
|
|
528
|
-
value
|
|
529
|
-
} = _ref,
|
|
530
|
-
rest = _objectWithoutProperties(_ref, _excluded);
|
|
531
|
-
const inputState = useFormSelector(state => state.fields[formKey]);
|
|
532
|
-
const store = useFormStore();
|
|
533
|
-
const _element = useMemo(() => {
|
|
534
|
-
const value = inputState.value || '';
|
|
535
|
-
const _props = _objectSpread(_objectSpread({}, rest), {}, {
|
|
536
|
-
value,
|
|
537
|
-
inputMode: type,
|
|
538
|
-
ref
|
|
539
|
-
});
|
|
540
|
-
_props.onEndEditing = rest.onEndEditing ? e => {
|
|
541
|
-
store.handlers._viHandler(inputState);
|
|
542
|
-
rest.onEndEditing(e);
|
|
543
|
-
} : () => {
|
|
544
|
-
store.handlers._viHandler(inputState);
|
|
545
|
-
};
|
|
546
|
-
_props.onChangeText = rest.onChangeText ? e => {
|
|
547
|
-
store.handlers._updateInputHandler(inputState, undefined, {
|
|
548
|
-
value: e
|
|
549
|
-
});
|
|
550
|
-
rest.onChangeText(e);
|
|
551
|
-
} : e => {
|
|
552
|
-
store.handlers._updateInputHandler(inputState, undefined, {
|
|
553
|
-
value: e
|
|
554
|
-
});
|
|
555
|
-
};
|
|
556
|
-
if (!inputState.touched) {
|
|
557
|
-
_props.onFocus = rest.onFocus ? e => {
|
|
558
|
-
rest.onFocus(e);
|
|
559
|
-
inputState.dispatchChanges({
|
|
560
|
-
touched: true
|
|
561
|
-
});
|
|
562
|
-
} : () => {
|
|
563
|
-
inputState.dispatchChanges({
|
|
564
|
-
touched: true
|
|
565
|
-
});
|
|
566
|
-
};
|
|
567
|
-
}
|
|
568
|
-
if (element) {
|
|
569
|
-
return element(inputState, _props);
|
|
570
|
-
}
|
|
571
|
-
return React.createElement(TextInput, _props);
|
|
572
|
-
}, [inputState, element]);
|
|
573
|
-
const _fetchDeps = useFormSelector(makeSelectFields(fetchDeps));
|
|
574
|
-
const stableFetchDeps = useMemo(() => JSON.stringify(_fetchDeps), [_fetchDeps]);
|
|
575
|
-
useEffect(() => {
|
|
576
|
-
if (fetch) {
|
|
577
|
-
_debounce(debounce, `${inputState.gid}-fetch`).then(() => {
|
|
578
|
-
const res = fetch(inputState, store.getState().fields);
|
|
579
|
-
if (res instanceof Promise) {
|
|
580
|
-
res.then(state => state && store.handlers._dispatchChanges(state, formKey));
|
|
581
|
-
} else if (res) {
|
|
582
|
-
store.handlers._dispatchChanges(res, formKey);
|
|
583
|
-
}
|
|
584
|
-
});
|
|
585
|
-
}
|
|
586
|
-
}, [stableFetchDeps]);
|
|
587
|
-
return _element;
|
|
588
|
-
});
|
|
589
|
-
const RNGInput = memo(_RNGInput);
|
|
590
|
-
|
|
591
|
-
export { RNGForm, RNGInput };
|
|
592
|
-
//# sourceMappingURL=index.development.js.map
|
|
1
|
+
export { RNGForm } from './RNGForm.development.js';
|
|
2
|
+
export { RNGInput } from './RNGInput.development.js';
|
package/native/dist/esm/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { RNGForm } from './RNGForm.production';
|
|
2
|
-
export { RNGInput } from './RNGInput.production';
|
|
1
|
+
export { RNGForm } from './RNGForm.production.js';
|
|
2
|
+
export { RNGInput } from './RNGInput.production.js';
|