@radicalbit/formbit 1.0.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 (35) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1180 -0
  3. package/dist/__tests__/check.test.d.ts +1 -0
  4. package/dist/__tests__/error.test.d.ts +1 -0
  5. package/dist/__tests__/initialize.test.d.ts +1 -0
  6. package/dist/__tests__/is-form-invalid.test.d.ts +1 -0
  7. package/dist/__tests__/is-form-valid.test.d.ts +1 -0
  8. package/dist/__tests__/live-validation.test.d.ts +1 -0
  9. package/dist/__tests__/remove.test.d.ts +1 -0
  10. package/dist/__tests__/reset-form.test.d.ts +1 -0
  11. package/dist/__tests__/set-error.test.d.ts +1 -0
  12. package/dist/__tests__/set-schema.test.d.ts +1 -0
  13. package/dist/__tests__/submit-form.test.d.ts +1 -0
  14. package/dist/__tests__/use-execute-callbacks.test.d.ts +1 -0
  15. package/dist/__tests__/use-formbit-context.test.d.ts +1 -0
  16. package/dist/__tests__/validate-all.test.d.ts +1 -0
  17. package/dist/__tests__/validate-form.test.d.ts +1 -0
  18. package/dist/__tests__/validate-sync-all.test.d.ts +1 -0
  19. package/dist/__tests__/validate.test.d.ts +1 -0
  20. package/dist/__tests__/write-all.test.d.ts +1 -0
  21. package/dist/__tests__/write.test.d.ts +1 -0
  22. package/dist/formbit-context.d.ts +9 -0
  23. package/dist/helpers/constants.d.ts +9 -0
  24. package/dist/helpers/obj-leaves.d.ts +3 -0
  25. package/dist/index.d.ts +4 -0
  26. package/dist/index.js +658 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/index.modern.js +654 -0
  29. package/dist/index.modern.js.map +1 -0
  30. package/dist/types/helpers.d.ts +2 -0
  31. package/dist/types/index.d.ts +418 -0
  32. package/dist/use-execute-callbacks.d.ts +11 -0
  33. package/dist/use-formbit.d.ts +7 -0
  34. package/dist/validate-sync-all.d.ts +2 -0
  35. package/package.json +90 -0
package/dist/index.js ADDED
@@ -0,0 +1,658 @@
1
+ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
2
+
3
+ var React = require('react');
4
+ var React__default = _interopDefault(React);
5
+ var lodash = require('lodash');
6
+ var yup = require('yup');
7
+
8
+ function _extends() {
9
+ _extends = Object.assign || function (target) {
10
+ for (var i = 1; i < arguments.length; i++) {
11
+ var source = arguments[i];
12
+
13
+ for (var key in source) {
14
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
15
+ target[key] = source[key];
16
+ }
17
+ }
18
+ }
19
+
20
+ return target;
21
+ };
22
+
23
+ return _extends.apply(this, arguments);
24
+ }
25
+
26
+ function _objectWithoutPropertiesLoose(source, excluded) {
27
+ if (source == null) return {};
28
+ var target = {};
29
+ var sourceKeys = Object.keys(source);
30
+ var key, i;
31
+
32
+ for (i = 0; i < sourceKeys.length; i++) {
33
+ key = sourceKeys[i];
34
+ if (excluded.indexOf(key) >= 0) continue;
35
+ target[key] = source[key];
36
+ }
37
+
38
+ return target;
39
+ }
40
+
41
+ function _unsupportedIterableToArray(o, minLen) {
42
+ if (!o) return;
43
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
44
+ var n = Object.prototype.toString.call(o).slice(8, -1);
45
+ if (n === "Object" && o.constructor) n = o.constructor.name;
46
+ if (n === "Map" || n === "Set") return Array.from(o);
47
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
48
+ }
49
+
50
+ function _arrayLikeToArray(arr, len) {
51
+ if (len == null || len > arr.length) len = arr.length;
52
+
53
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
54
+
55
+ return arr2;
56
+ }
57
+
58
+ function _createForOfIteratorHelperLoose(o, allowArrayLike) {
59
+ var it;
60
+
61
+ if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
62
+ if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
63
+ if (it) o = it;
64
+ var i = 0;
65
+ return function () {
66
+ if (i >= o.length) return {
67
+ done: true
68
+ };
69
+ return {
70
+ done: false,
71
+ value: o[i++]
72
+ };
73
+ };
74
+ }
75
+
76
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
77
+ }
78
+
79
+ it = o[Symbol.iterator]();
80
+ return it.next.bind(it);
81
+ }
82
+
83
+ function isObject(val) {
84
+ return val != null && typeof val === 'object' && Array.isArray(val) === false;
85
+ }
86
+
87
+ var objLeaves = function objLeaves(obj, acc) {
88
+ if (acc === void 0) {
89
+ acc = [];
90
+ }
91
+
92
+ var fn = function fn(o, s) {
93
+ if (Array.isArray(o)) {
94
+ return Object.keys(o).forEach(function (k) {
95
+ var path = s ? s + "[" + k + "]" : k;
96
+ var isLeaf = fn(o[+k], path) === false;
97
+
98
+ if (isLeaf) {
99
+ acc.push(path);
100
+ }
101
+ });
102
+ }
103
+
104
+ if (isObject(o)) {
105
+ return Object.keys(o).forEach(function (k) {
106
+ var path = s ? s + "." + k : k;
107
+ var isLeaf = fn(o[k], path) === false;
108
+
109
+ if (isLeaf) {
110
+ acc.push(path);
111
+ }
112
+ });
113
+ }
114
+
115
+ return false;
116
+ };
117
+
118
+ fn(obj);
119
+ return acc;
120
+ };
121
+
122
+ var ACTIONS;
123
+
124
+ (function (ACTIONS) {
125
+ ACTIONS["write"] = "write";
126
+ ACTIONS["remove"] = "remove";
127
+ })(ACTIONS || (ACTIONS = {}));
128
+
129
+ var MISSING_CONTEXT_ERROR = 'No FormbitContext.Provider found, useFormbitContext can only be called inside proper context';
130
+
131
+ var isValidationError = function isValidationError(error) {
132
+ if (error && typeof error === 'object' && 'message' in error && 'path' in error && 'inner' in error) {
133
+ var path = error.path,
134
+ message = error.message,
135
+ inner = error.inner;
136
+ return (typeof path === 'string' || path === undefined) && (typeof message === 'string' || path === undefined) && Array.isArray(inner);
137
+ }
138
+
139
+ return false;
140
+ };
141
+
142
+ var validateSyncAll = function validateSyncAll(paths, schema, form, options) {
143
+ if (options === void 0) {
144
+ options = {};
145
+ }
146
+
147
+ var errors = [];
148
+
149
+ if (lodash.isEmpty(paths)) {
150
+ return errors;
151
+ }
152
+
153
+ for (var _iterator = _createForOfIteratorHelperLoose(paths), _step; !(_step = _iterator()).done;) {
154
+ var pathToValidate = _step.value;
155
+
156
+ try {
157
+ schema.validateSyncAt(pathToValidate, form, options);
158
+ } catch (e) {
159
+ var _options;
160
+
161
+ if (isValidationError(e)) {
162
+ var message = e.message,
163
+ path = e.path;
164
+ errors = errors.concat({
165
+ message: message,
166
+ path: path
167
+ });
168
+ }
169
+
170
+ if ((_options = options) !== null && _options !== void 0 && _options.abortEarly) {
171
+ break;
172
+ }
173
+ }
174
+ }
175
+
176
+ return errors;
177
+ };
178
+
179
+ var useExecuteCallbacks = (function (writer, setError) {
180
+ var callbacksStore = React.useRef([]);
181
+ React.useEffect(function () {
182
+ if (callbacksStore.current.length) {
183
+ callbacksStore.current.forEach(function (cb) {
184
+ if (cb) {
185
+ cb(writer, setError);
186
+ }
187
+ });
188
+ callbacksStore.current = [];
189
+ }
190
+ }, [setError, writer]);
191
+ return React.useCallback(function (cb) {
192
+ if (cb) {
193
+ callbacksStore.current.push(cb);
194
+ }
195
+ }, []);
196
+ });
197
+
198
+ var useFormbit = (function (_ref) {
199
+ var _ref$initialValues = _ref.initialValues,
200
+ initialValues = _ref$initialValues === void 0 ? {} : _ref$initialValues,
201
+ schema = _ref.yup;
202
+ var schemaRef = React.useRef(schema);
203
+
204
+ var _useState = React.useState({
205
+ form: initialValues,
206
+ initialValues: initialValues,
207
+ errors: {},
208
+ liveValidation: {},
209
+ isDirty: false
210
+ }),
211
+ writer = _useState[0],
212
+ setWriter = _useState[1];
213
+
214
+ var initialize = React.useCallback(function (values) {
215
+ setWriter(function (w) {
216
+ return _extends({}, w, {
217
+ initialValues: values,
218
+ form: values,
219
+ errors: {},
220
+ liveValidation: {},
221
+ isDirty: false
222
+ });
223
+ });
224
+ }, []);
225
+ var setSchema = React.useCallback(function (newSchema) {
226
+ schemaRef.current = newSchema;
227
+ }, []);
228
+ var setError = React.useCallback(function (path, value) {
229
+ setWriter(function (w) {
230
+ var errors = lodash.set(lodash.cloneDeep(w.errors), path, value);
231
+ return _extends({}, w, {
232
+ errors: errors
233
+ });
234
+ });
235
+ }, []);
236
+ var executeCb = useExecuteCallbacks(writer, setError);
237
+ var writeOrRemove = React.useCallback(function (path, value, _temp, action) {
238
+ var _ref2 = _temp === void 0 ? {} : _temp,
239
+ _ref2$noLiveValidatio = _ref2.noLiveValidation,
240
+ noLiveValidation = _ref2$noLiveValidatio === void 0 ? false : _ref2$noLiveValidatio,
241
+ _ref2$pathsToValidate = _ref2.pathsToValidate,
242
+ pathsToValidate = _ref2$pathsToValidate === void 0 ? [] : _ref2$pathsToValidate,
243
+ successCallback = _ref2.successCallback,
244
+ errorCallback = _ref2.errorCallback,
245
+ _ref2$options = _ref2.options,
246
+ options = _ref2$options === void 0 ? {} : _ref2$options;
247
+
248
+ setWriter(function (w) {
249
+ var liveValidationPaths = noLiveValidation ? [] : Object.keys(w.liveValidation);
250
+ var paths = pathsToValidate.concat(liveValidationPaths);
251
+
252
+ var form = function execute() {
253
+ switch (action) {
254
+ case ACTIONS.write:
255
+ return lodash.set(lodash.cloneDeep(w.form), path, value);
256
+
257
+ case ACTIONS.remove:
258
+ return lodash.omit(lodash.cloneDeep(w.form), path);
259
+
260
+ default:
261
+ return lodash.cloneDeep(w.form);
262
+ }
263
+ }();
264
+
265
+ var newWriter = _extends({}, w, {
266
+ form: form,
267
+ isDirty: true
268
+ });
269
+
270
+ if (paths.length === 0) {
271
+ executeCb(successCallback);
272
+ return newWriter;
273
+ }
274
+
275
+ var cleanErrors = paths.reduce(function (acc, key) {
276
+ return lodash.set(acc, key, undefined);
277
+ }, lodash.cloneDeep(newWriter.errors));
278
+ var inner = validateSyncAll(paths, schemaRef.current, newWriter.form, options);
279
+
280
+ if (lodash.isEmpty(inner)) {
281
+ var _neww = _extends({}, newWriter, {
282
+ errors: cleanErrors
283
+ });
284
+
285
+ executeCb(successCallback);
286
+ return _neww;
287
+ }
288
+
289
+ var errors = inner.reduce(function (acc, _ref3) {
290
+ var innerPath = _ref3.path,
291
+ message = _ref3.message;
292
+ return innerPath ? lodash.set(acc, innerPath, message) : acc;
293
+ }, cleanErrors);
294
+ var liveValidation = inner.reduce(function (acc, _ref4) {
295
+ var _extends2;
296
+
297
+ var innerPath = _ref4.path;
298
+ return innerPath ? _extends({}, acc, (_extends2 = {}, _extends2[innerPath] = true, _extends2)) : acc;
299
+ }, newWriter.liveValidation);
300
+
301
+ var neww = _extends({}, newWriter, {
302
+ errors: errors,
303
+ liveValidation: liveValidation
304
+ });
305
+
306
+ executeCb(errorCallback);
307
+ return neww;
308
+ });
309
+ }, [executeCb]);
310
+ var write = React.useCallback(function (path, value, options) {
311
+ return writeOrRemove(path, value, options, ACTIONS.write);
312
+ }, [writeOrRemove]);
313
+ var remove = React.useCallback(function (path, options) {
314
+ return writeOrRemove(path, undefined, options, ACTIONS.remove);
315
+ }, [writeOrRemove]);
316
+ var writeAll = React.useCallback(function (arr, _temp2) {
317
+ var _ref5 = _temp2 === void 0 ? {} : _temp2,
318
+ _ref5$noLiveValidatio = _ref5.noLiveValidation,
319
+ noLiveValidation = _ref5$noLiveValidatio === void 0 ? false : _ref5$noLiveValidatio,
320
+ _ref5$pathsToValidate = _ref5.pathsToValidate,
321
+ pathsToValidate = _ref5$pathsToValidate === void 0 ? [] : _ref5$pathsToValidate,
322
+ successCallback = _ref5.successCallback,
323
+ errorCallback = _ref5.errorCallback,
324
+ _ref5$options = _ref5.options,
325
+ options = _ref5$options === void 0 ? {} : _ref5$options;
326
+
327
+ setWriter(function (w) {
328
+ var liveValidationPaths = noLiveValidation ? [] : Object.keys(w.liveValidation);
329
+ var paths = pathsToValidate.concat(liveValidationPaths);
330
+ var form = arr.reduce(function (acc, _ref6) {
331
+ var key = _ref6[0],
332
+ val = _ref6[1];
333
+ return lodash.set(acc, key, val);
334
+ }, lodash.cloneDeep(w.form));
335
+
336
+ var newWriter = _extends({}, w, {
337
+ form: form,
338
+ isDirty: true
339
+ });
340
+
341
+ if (paths.length === 0) {
342
+ executeCb(successCallback);
343
+ return newWriter;
344
+ }
345
+
346
+ var cleanErrors = paths.reduce(function (acc, key) {
347
+ return lodash.set(acc, key, undefined);
348
+ }, lodash.cloneDeep(newWriter.errors));
349
+ var inner = validateSyncAll(paths, schemaRef.current, newWriter.form, options);
350
+
351
+ if (lodash.isEmpty(inner)) {
352
+ var _neww2 = _extends({}, newWriter, {
353
+ errors: cleanErrors
354
+ });
355
+
356
+ executeCb(successCallback);
357
+ return _neww2;
358
+ }
359
+
360
+ var errors = inner.reduce(function (acc, _ref7) {
361
+ var _ref7$path = _ref7.path,
362
+ path = _ref7$path === void 0 ? '' : _ref7$path,
363
+ message = _ref7.message;
364
+ return lodash.set(acc, path, message);
365
+ }, cleanErrors);
366
+ var liveValidation = inner.reduce(function (acc, _ref8) {
367
+ var _extends3;
368
+
369
+ var _ref8$path = _ref8.path,
370
+ path = _ref8$path === void 0 ? '' : _ref8$path;
371
+ return _extends({}, acc, (_extends3 = {}, _extends3[path] = true, _extends3));
372
+ }, newWriter.liveValidation);
373
+
374
+ var neww = _extends({}, newWriter, {
375
+ errors: errors,
376
+ liveValidation: liveValidation
377
+ });
378
+
379
+ executeCb(errorCallback);
380
+ return neww;
381
+ });
382
+ }, [executeCb]);
383
+ var validate = React.useCallback(function (path, _temp3) {
384
+ var _ref9 = _temp3 === void 0 ? {} : _temp3,
385
+ successCallback = _ref9.successCallback,
386
+ errorCallback = _ref9.errorCallback,
387
+ options = _ref9.options;
388
+
389
+ setWriter(function (w) {
390
+ var cleanErrors = lodash.set(lodash.cloneDeep(w.errors), path, undefined);
391
+ var inner = validateSyncAll([path], schemaRef.current, w.form, options);
392
+
393
+ if (lodash.isEmpty(inner)) {
394
+ var _neww3 = _extends({}, w, {
395
+ errors: cleanErrors
396
+ });
397
+
398
+ executeCb(successCallback);
399
+ return _neww3;
400
+ }
401
+
402
+ var errors = inner.reduce(function (acc, _ref10) {
403
+ var _ref10$path = _ref10.path,
404
+ errorPath = _ref10$path === void 0 ? '' : _ref10$path,
405
+ message = _ref10.message;
406
+ return lodash.set(acc, errorPath, message);
407
+ }, cleanErrors);
408
+ var liveValidation = inner.reduce(function (acc, _ref11) {
409
+ var _extends4;
410
+
411
+ var _ref11$path = _ref11.path,
412
+ errorPath = _ref11$path === void 0 ? '' : _ref11$path;
413
+ return _extends({}, acc, (_extends4 = {}, _extends4[errorPath] = true, _extends4));
414
+ }, w.liveValidation);
415
+
416
+ var neww = _extends({}, w, {
417
+ errors: errors,
418
+ liveValidation: liveValidation
419
+ });
420
+
421
+ executeCb(errorCallback);
422
+ return neww;
423
+ });
424
+ }, [executeCb]);
425
+ var validateAll = React.useCallback(function (paths, _temp4) {
426
+ var _ref12 = _temp4 === void 0 ? {} : _temp4,
427
+ successCallback = _ref12.successCallback,
428
+ errorCallback = _ref12.errorCallback,
429
+ options = _ref12.options;
430
+
431
+ setWriter(function (w) {
432
+ var cleanErrors = paths.reduce(function (acc, path) {
433
+ return lodash.set(acc, path, undefined);
434
+ }, lodash.cloneDeep(w.errors));
435
+ var inner = validateSyncAll(paths, schemaRef.current, w.form, options);
436
+
437
+ if (lodash.isEmpty(inner)) {
438
+ var _neww4 = _extends({}, w, {
439
+ errors: cleanErrors
440
+ });
441
+
442
+ executeCb(successCallback);
443
+ return _neww4;
444
+ }
445
+
446
+ var errors = inner.reduce(function (acc, _ref13) {
447
+ var _ref13$path = _ref13.path,
448
+ path = _ref13$path === void 0 ? '' : _ref13$path,
449
+ message = _ref13.message;
450
+ return lodash.set(acc, path, message);
451
+ }, cleanErrors);
452
+ var liveValidation = inner.reduce(function (acc, _ref14) {
453
+ var _extends5;
454
+
455
+ var _ref14$path = _ref14.path,
456
+ path = _ref14$path === void 0 ? '' : _ref14$path;
457
+ return _extends({}, acc, (_extends5 = {}, _extends5[path] = true, _extends5));
458
+ }, w.liveValidation);
459
+
460
+ var neww = _extends({}, w, {
461
+ errors: errors,
462
+ liveValidation: liveValidation
463
+ });
464
+
465
+ executeCb(errorCallback);
466
+ return neww;
467
+ });
468
+ }, [executeCb]);
469
+ var check = React.useCallback(function (json, _temp5) {
470
+ var _ref15 = _temp5 === void 0 ? {} : _temp5,
471
+ successCallback = _ref15.successCallback,
472
+ errorCallback = _ref15.errorCallback,
473
+ options = _ref15.options;
474
+
475
+ try {
476
+ schema.validateSync(json, _extends({
477
+ abortEarly: false
478
+ }, options));
479
+ successCallback === null || successCallback === void 0 ? void 0 : successCallback(json, writer, setError);
480
+ return undefined;
481
+ } catch (e) {
482
+ if (isValidationError(e)) {
483
+ var inner = e.inner;
484
+ errorCallback === null || errorCallback === void 0 ? void 0 : errorCallback(json, inner, writer, setError);
485
+ return inner;
486
+ }
487
+
488
+ return undefined;
489
+ }
490
+ }, [schema, setError, writer]);
491
+ var privateValidateForm = React.useCallback(function (successCallback, errorCallback, _temp6) {
492
+ var _ref16 = _temp6 === void 0 ? {} : _temp6,
493
+ options = _ref16.options;
494
+
495
+ setWriter(function (w) {
496
+ try {
497
+ schemaRef.current.validateSync(w.form, _extends({
498
+ abortEarly: false
499
+ }, options));
500
+ executeCb(successCallback);
501
+ return _extends({}, w, {
502
+ errors: {}
503
+ });
504
+ } catch (e) {
505
+ if (!isValidationError(e)) {
506
+ console.error(e);
507
+ return w;
508
+ }
509
+
510
+ var inner = e.inner;
511
+ var errors = inner.reduce(function (acc, _ref17) {
512
+ var message = _ref17.message,
513
+ _ref17$path = _ref17.path,
514
+ path = _ref17$path === void 0 ? '' : _ref17$path;
515
+ return lodash.set(acc, path, message);
516
+ }, {});
517
+
518
+ var _liveValidation = inner.reduce(function (acc, _ref18) {
519
+ var _extends6;
520
+
521
+ var _ref18$path = _ref18.path,
522
+ path = _ref18$path === void 0 ? '' : _ref18$path;
523
+ return _extends({}, acc, (_extends6 = {}, _extends6[path] = true, _extends6));
524
+ }, w.liveValidation);
525
+
526
+ var newWriter = _extends({}, w, {
527
+ errors: errors,
528
+ liveValidation: _liveValidation
529
+ });
530
+
531
+ executeCb(errorCallback);
532
+ return newWriter;
533
+ }
534
+ });
535
+ }, [executeCb]);
536
+ var validateForm = React.useCallback(function (successCallback, errorCallback, options) {
537
+ if (options === void 0) {
538
+ options = {};
539
+ }
540
+
541
+ return privateValidateForm(successCallback, errorCallback, {
542
+ options: options
543
+ });
544
+ }, [privateValidateForm]);
545
+ var submitForm = React.useCallback(function (successCallback, errorCallback, options) {
546
+ if (options === void 0) {
547
+ options = {};
548
+ }
549
+
550
+ var fn = function fn() {
551
+ return setWriter(function (w) {
552
+ return _extends({}, w, {
553
+ isDirty: false
554
+ });
555
+ });
556
+ };
557
+
558
+ var successCallbackAndClearIsDirty = function successCallbackAndClearIsDirty(a, b) {
559
+ var writer = a;
560
+
561
+ var _writer$form = writer.form,
562
+ form = _objectWithoutPropertiesLoose(_writer$form, ["__metadata"]);
563
+
564
+ return successCallback(_extends({}, writer, {
565
+ form: form
566
+ }), b, fn);
567
+ };
568
+
569
+ return privateValidateForm(successCallbackAndClearIsDirty, errorCallback, {
570
+ options: options
571
+ });
572
+ }, [privateValidateForm]);
573
+ var resetForm = React.useCallback(function () {
574
+ setWriter(function (w) {
575
+ return _extends({}, w, {
576
+ form: w.initialValues,
577
+ errors: {},
578
+ isDirty: false,
579
+ liveValidation: {}
580
+ });
581
+ });
582
+ }, []);
583
+ var isFormValid = React.useCallback(function () {
584
+ var _writer$errors = writer.errors,
585
+ e = _objectWithoutPropertiesLoose(_writer$errors, ["silent"]);
586
+
587
+ return objLeaves(e).every(function (leaf) {
588
+ return !lodash.get(e, leaf);
589
+ });
590
+ }, [writer.errors]);
591
+ var isFormInvalid = React.useCallback(function () {
592
+ var _writer$errors2 = writer.errors,
593
+ e = _objectWithoutPropertiesLoose(_writer$errors2, ["silent"]);
594
+
595
+ return objLeaves(e).some(function (leaf) {
596
+ return lodash.get(e, leaf);
597
+ });
598
+ }, [writer.errors]);
599
+ var error = React.useCallback(function (path) {
600
+ return lodash.get(writer.errors, path, undefined);
601
+ }, [writer.errors]);
602
+ var liveValidation = React.useCallback(function (path) {
603
+ return lodash.get(writer.liveValidation, path, undefined);
604
+ }, [writer.liveValidation]);
605
+ return {
606
+ check: check,
607
+ error: error,
608
+ errors: writer.errors,
609
+ form: writer.form,
610
+ initialize: initialize,
611
+ isDirty: writer.isDirty,
612
+ isFormInvalid: isFormInvalid,
613
+ isFormValid: isFormValid,
614
+ liveValidation: liveValidation,
615
+ remove: remove,
616
+ resetForm: resetForm,
617
+ setError: setError,
618
+ setSchema: setSchema,
619
+ submitForm: submitForm,
620
+ validate: validate,
621
+ validateAll: validateAll,
622
+ validateForm: validateForm,
623
+ write: write,
624
+ writeAll: writeAll
625
+ };
626
+ });
627
+
628
+ var createFormbitContext = lodash.once(function () {
629
+ return React.createContext(undefined);
630
+ });
631
+ function FormbitContextProvider(_ref) {
632
+ var _ref$initialValues = _ref.initialValues,
633
+ initialValues = _ref$initialValues === void 0 ? {} : _ref$initialValues,
634
+ schema = _ref.schema,
635
+ children = _ref.children;
636
+ var FormbitContext = createFormbitContext();
637
+ var value = useFormbit({
638
+ initialValues: initialValues,
639
+ yup: schema || yup.object()
640
+ });
641
+ return /*#__PURE__*/React__default.createElement(FormbitContext.Provider, {
642
+ value: value
643
+ }, children);
644
+ }
645
+ var useFormbitContext = function useFormbitContext() {
646
+ var context = React.useContext(createFormbitContext());
647
+
648
+ if (!context) {
649
+ throw new Error(MISSING_CONTEXT_ERROR);
650
+ }
651
+
652
+ return context;
653
+ };
654
+
655
+ exports.FormbitContextProvider = FormbitContextProvider;
656
+ exports.default = useFormbit;
657
+ exports.useFormbitContext = useFormbitContext;
658
+ //# sourceMappingURL=index.js.map