relay-forms-nodeps 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 (96) hide show
  1. package/.babelrc.js +5 -0
  2. package/LICENSE +21 -0
  3. package/README.md +124 -0
  4. package/lib/RelayFormsTypes.d.ts +37 -0
  5. package/lib/RelayFormsTypes.js +5 -0
  6. package/lib/Utils.d.ts +11 -0
  7. package/lib/Utils.js +87 -0
  8. package/lib/es/RelayFormsTypes.d.ts +37 -0
  9. package/lib/es/RelayFormsTypes.js +0 -0
  10. package/lib/es/Utils.d.ts +11 -0
  11. package/lib/es/Utils.js +81 -0
  12. package/lib/es/index.d.ts +9 -0
  13. package/lib/es/index.js +7 -0
  14. package/lib/es/relay/RelayModernRecord.d.ts +107 -0
  15. package/lib/es/relay/RelayModernRecord.js +183 -0
  16. package/lib/es/relay/RelayModernSelector.d.ts +11 -0
  17. package/lib/es/relay/RelayModernSelector.js +29 -0
  18. package/lib/es/relay/RelayModernStore.d.ts +51 -0
  19. package/lib/es/relay/RelayModernStore.js +355 -0
  20. package/lib/es/relay/RelayReader.d.ts +14 -0
  21. package/lib/es/relay/RelayReader.js +86 -0
  22. package/lib/es/relay/RelayRecordProxy.d.ts +28 -0
  23. package/lib/es/relay/RelayRecordProxy.js +57 -0
  24. package/lib/es/relay/RelayRecordSource.d.ts +28 -0
  25. package/lib/es/relay/RelayRecordSource.js +70 -0
  26. package/lib/es/relay/RelayRecordSourceMutator.d.ts +36 -0
  27. package/lib/es/relay/RelayRecordSourceMutator.js +75 -0
  28. package/lib/es/relay/RelayRecordSourceProxy.d.ts +25 -0
  29. package/lib/es/relay/RelayRecordSourceProxy.js +52 -0
  30. package/lib/es/relay/RelayReferenceMarker.d.ts +10 -0
  31. package/lib/es/relay/RelayReferenceMarker.js +54 -0
  32. package/lib/es/relay/RelayStoreProvider.d.ts +7 -0
  33. package/lib/es/relay/RelayStoreProvider.js +16 -0
  34. package/lib/es/relay/RelayStoreUtils.d.ts +51 -0
  35. package/lib/es/relay/RelayStoreUtils.js +199 -0
  36. package/lib/es/relay/RelayTypes.d.ts +92 -0
  37. package/lib/es/relay/RelayTypes.js +0 -0
  38. package/lib/es/relay/queries.d.ts +86 -0
  39. package/lib/es/relay/queries.js +68 -0
  40. package/lib/es/useForceUpdate.d.ts +1 -0
  41. package/lib/es/useForceUpdate.js +8 -0
  42. package/lib/es/useFormSetValue.d.ts +2 -0
  43. package/lib/es/useFormSetValue.js +84 -0
  44. package/lib/es/useFormState.d.ts +2 -0
  45. package/lib/es/useFormState.js +49 -0
  46. package/lib/es/useFormSubmit.d.ts +2 -0
  47. package/lib/es/useFormSubmit.js +105 -0
  48. package/lib/es/useFormValue.d.ts +2 -0
  49. package/lib/es/useFormValue.js +19 -0
  50. package/lib/index.d.ts +9 -0
  51. package/lib/index.js +20 -0
  52. package/lib/relay/RelayModernRecord.d.ts +107 -0
  53. package/lib/relay/RelayModernRecord.js +188 -0
  54. package/lib/relay/RelayModernSelector.d.ts +11 -0
  55. package/lib/relay/RelayModernSelector.js +36 -0
  56. package/lib/relay/RelayModernStore.d.ts +51 -0
  57. package/lib/relay/RelayModernStore.js +359 -0
  58. package/lib/relay/RelayReader.d.ts +14 -0
  59. package/lib/relay/RelayReader.js +92 -0
  60. package/lib/relay/RelayRecordProxy.d.ts +28 -0
  61. package/lib/relay/RelayRecordProxy.js +60 -0
  62. package/lib/relay/RelayRecordSource.d.ts +28 -0
  63. package/lib/relay/RelayRecordSource.js +75 -0
  64. package/lib/relay/RelayRecordSourceMutator.d.ts +36 -0
  65. package/lib/relay/RelayRecordSourceMutator.js +80 -0
  66. package/lib/relay/RelayRecordSourceProxy.d.ts +25 -0
  67. package/lib/relay/RelayRecordSourceProxy.js +57 -0
  68. package/lib/relay/RelayReferenceMarker.d.ts +10 -0
  69. package/lib/relay/RelayReferenceMarker.js +60 -0
  70. package/lib/relay/RelayStoreProvider.d.ts +7 -0
  71. package/lib/relay/RelayStoreProvider.js +22 -0
  72. package/lib/relay/RelayStoreUtils.d.ts +51 -0
  73. package/lib/relay/RelayStoreUtils.js +208 -0
  74. package/lib/relay/RelayTypes.d.ts +92 -0
  75. package/lib/relay/RelayTypes.js +5 -0
  76. package/lib/relay/queries.d.ts +86 -0
  77. package/lib/relay/queries.js +73 -0
  78. package/lib/umd/relay-forms-nodeps.js +1478 -0
  79. package/lib/umd/relay-forms-nodeps.js.map +1 -0
  80. package/lib/umd/relay-forms-nodeps.min.js +2 -0
  81. package/lib/umd/relay-forms-nodeps.min.js.map +1 -0
  82. package/lib/useForceUpdate.d.ts +1 -0
  83. package/lib/useForceUpdate.js +14 -0
  84. package/lib/useFormSetValue.d.ts +2 -0
  85. package/lib/useFormSetValue.js +90 -0
  86. package/lib/useFormState.d.ts +2 -0
  87. package/lib/useFormState.js +54 -0
  88. package/lib/useFormSubmit.d.ts +2 -0
  89. package/lib/useFormSubmit.js +110 -0
  90. package/lib/useFormValue.d.ts +2 -0
  91. package/lib/useFormValue.js +25 -0
  92. package/package.json +89 -0
  93. package/replace.config.js +5 -0
  94. package/rollup.config.js +92 -0
  95. package/tsconfig.esm.json +7 -0
  96. package/yarn-error.log +127 -0
@@ -0,0 +1,199 @@
1
+ /* eslint-disable @typescript-eslint/explicit-function-return-type */
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ * @flow
9
+ * @format
10
+ */
11
+ export function isPromise(p) {
12
+ return !!p && typeof p.then === 'function';
13
+ }
14
+ 'use strict';
15
+ /**
16
+ * Copyright (c) 2013-present, Facebook, Inc.
17
+ *
18
+ * This source code is licensed under the MIT license found in the
19
+ * LICENSE file in the root directory of this source tree.
20
+ *
21
+ *
22
+ */
23
+ var aStackPool = [];
24
+ var bStackPool = [];
25
+ /**
26
+ * Checks if two values are equal. Values may be primitives, arrays, or objects.
27
+ * Returns true if both arguments have the same keys and values.
28
+ *
29
+ * @see http://underscorejs.org
30
+ * @copyright 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
31
+ * @license MIT
32
+ */
33
+ export function areEqual(a, b) {
34
+ var aStack = aStackPool.length ? aStackPool.pop() : [];
35
+ var bStack = bStackPool.length ? bStackPool.pop() : [];
36
+ var result = eq(a, b, aStack, bStack);
37
+ aStack.length = 0;
38
+ bStack.length = 0;
39
+ aStackPool.push(aStack);
40
+ bStackPool.push(bStack);
41
+ return result;
42
+ }
43
+ function eq(a, b, aStack, bStack) {
44
+ if (a === b) {
45
+ // Identical objects are equal. `0 === -0`, but they aren't identical.
46
+ return a !== 0 || 1 / a == 1 / b;
47
+ }
48
+ if (a == null || b == null) {
49
+ // a or b can be `null` or `undefined`
50
+ return false;
51
+ }
52
+ if (typeof a != 'object' || typeof b != 'object') {
53
+ return false;
54
+ }
55
+ var objToStr = Object.prototype.toString;
56
+ var className = objToStr.call(a);
57
+ if (className != objToStr.call(b)) {
58
+ return false;
59
+ }
60
+ switch (className) {
61
+ case '[object String]':
62
+ return a == String(b);
63
+ case '[object Number]':
64
+ return isNaN(a) || isNaN(b) ? false : a == Number(b);
65
+ case '[object Date]':
66
+ case '[object Boolean]':
67
+ return +a == +b;
68
+ case '[object RegExp]':
69
+ return a.source == b.source && a.global == b.global && a.multiline == b.multiline && a.ignoreCase == b.ignoreCase;
70
+ } // Assume equality for cyclic structures.
71
+ var length = aStack.length;
72
+ // eslint-disable-next-line no-plusplus
73
+ while (length--) {
74
+ if (aStack[length] == a) {
75
+ return bStack[length] == b;
76
+ }
77
+ }
78
+ aStack.push(a);
79
+ bStack.push(b);
80
+ var size = 0; // Recursively compare objects and arrays.
81
+ if (className === '[object Array]') {
82
+ size = a.length;
83
+ if (size !== b.length) {
84
+ return false;
85
+ } // Deep compare the contents, ignoring non-numeric properties.
86
+ // eslint-disable-next-line no-plusplus
87
+ while (size--) {
88
+ if (!eq(a[size], b[size], aStack, bStack)) {
89
+ return false;
90
+ }
91
+ }
92
+ } else {
93
+ if (a.constructor !== b.constructor) {
94
+ return false;
95
+ }
96
+ if (a.hasOwnProperty('valueOf') && b.hasOwnProperty('valueOf')) {
97
+ return a.valueOf() == b.valueOf();
98
+ }
99
+ var keys = Object.keys(a);
100
+ if (keys.length != Object.keys(b).length) {
101
+ return false;
102
+ }
103
+ for (var i = 0; i < keys.length; i++) {
104
+ if (!eq(a[keys[i]], b[keys[i]], aStack, bStack)) {
105
+ return false;
106
+ }
107
+ }
108
+ }
109
+ aStack.pop();
110
+ bStack.pop();
111
+ return true;
112
+ }
113
+ /**
114
+ * Constants shared by all implementations of RecordSource/MutableRecordSource/etc.
115
+ */
116
+ export var RelayStoreUtils = {
117
+ ID_KEY: '__id',
118
+ REF_KEY: '__ref',
119
+ REFS_KEY: '__refs',
120
+ ROOT_ID: 'client:root'
121
+ };
122
+ export var RelayRecordState;
123
+ (function (RelayRecordState) {
124
+ /**
125
+ * Record exists (either fetched from the server or produced by a local,
126
+ * optimistic update).
127
+ */
128
+ RelayRecordState["EXISTENT"] = "EXISTENT";
129
+ /**
130
+ * Record is known not to exist (either as the result of a mutation, or
131
+ * because the server returned `null` when queried for the record).
132
+ */
133
+ RelayRecordState["NONEXISTENT"] = "NONEXISTENT";
134
+ /**
135
+ * Record State is unknown because it has not yet been fetched from the
136
+ * server.
137
+ */
138
+ RelayRecordState["UNKNOWN"] = "UNKNOWN";
139
+ })(RelayRecordState || (RelayRecordState = {}));
140
+ export var LINKED_FIELD = 'LinkedField';
141
+ //const hasWeakSetDefined = typeof WeakSet !== 'undefined';
142
+ //const hasWeakMapDefined = typeof WeakMap !== 'undefined';
143
+ function checkRecycleData(data) {
144
+ return typeof data !== 'object' || data instanceof Set || data instanceof Map ||
145
+ //(hasWeakSetDefined && nextData instanceof WeakSet) ||
146
+ //(hasWeakMapDefined && nextData instanceof WeakMap) ||
147
+ !data;
148
+ }
149
+ /**
150
+ * Recycles subtrees from `prevData` by replacing equal subtrees in `nextData`.
151
+ */
152
+ export function recycleNodesInto(prevData, nextData) {
153
+ if (prevData === nextData || checkRecycleData(prevData) || checkRecycleData(nextData)) {
154
+ return nextData;
155
+ }
156
+ var canRecycle = false;
157
+ // Assign local variables to preserve Flow type refinement.
158
+ var prevArray = Array.isArray(prevData) ? prevData : null;
159
+ var nextArray = Array.isArray(nextData) ? nextData : null;
160
+ if (prevArray && nextArray) {
161
+ canRecycle = nextArray.reduce(function (wasEqual, nextItem, ii) {
162
+ var prevValue = prevArray[ii];
163
+ var nextValue = recycleNodesInto(prevValue, nextItem);
164
+ if (nextValue !== nextArray[ii]) {
165
+ nextArray[ii] = nextValue;
166
+ }
167
+ return wasEqual && nextValue === prevArray[ii];
168
+ }, true) && prevArray.length === nextArray.length;
169
+ } else if (!prevArray && !nextArray) {
170
+ // Assign local variables to preserve Flow type refinement.
171
+ var prevObject_1 = prevData;
172
+ var nextObject_1 = nextData;
173
+ var prevKeys = Object.keys(prevObject_1);
174
+ var nextKeys = Object.keys(nextObject_1);
175
+ canRecycle = nextKeys.reduce(function (wasEqual, key) {
176
+ var prevValue = prevObject_1[key];
177
+ var nextValue = recycleNodesInto(prevValue, nextObject_1[key]);
178
+ if (nextValue !== nextObject_1[key]) {
179
+ nextObject_1[key] = nextValue;
180
+ }
181
+ return wasEqual && nextValue === prevObject_1[key];
182
+ }, true) && prevKeys.length === nextKeys.length;
183
+ }
184
+ return canRecycle ? prevData : nextData;
185
+ }
186
+ var ITERATOR_KEY = Symbol.iterator;
187
+ export function hasOverlappingIDs(seenRecords, updatedRecordIDs) {
188
+ // $FlowFixMe: Set is an iterable type, accessing its iterator is allowed.
189
+ var iterator = seenRecords[ITERATOR_KEY]();
190
+ var next = iterator.next();
191
+ while (!next.done) {
192
+ var key = next.value;
193
+ if (updatedRecordIDs.has(key)) {
194
+ return true;
195
+ }
196
+ next = iterator.next();
197
+ }
198
+ return false;
199
+ }
@@ -0,0 +1,92 @@
1
+ import { RelayRecordState } from './RelayStoreUtils';
2
+ export declare type Disposable = {
3
+ dispose(): void;
4
+ };
5
+ export interface RecordProxy {
6
+ getDataID(): string;
7
+ getLinkedRecords(name: string): Array<RecordProxy | null | undefined> | null | undefined;
8
+ setLinkedRecord(record: RecordProxy, name: string): RecordProxy;
9
+ setLinkedRecords(records: Array<RecordProxy>, name: string): RecordProxy;
10
+ setValue(value: any, name: string): RecordProxy;
11
+ }
12
+ export interface RecordSourceProxy {
13
+ create(string: string): RecordProxy;
14
+ delete(string: string): void;
15
+ get(string: string): RecordProxy | null | undefined;
16
+ getRoot(): RecordProxy;
17
+ }
18
+ export declare type StoreUpdater = (store: RecordSourceProxy) => void;
19
+ export declare type Record = {
20
+ [key: string]: any;
21
+ };
22
+ export declare type SelectorData = {
23
+ [key: string]: any;
24
+ };
25
+ export declare type RecordState = keyof typeof RelayRecordState;
26
+ export interface RecordSource {
27
+ get(dataID: string): Record | null | undefined;
28
+ getRecordIDs(): Array<string>;
29
+ getStatus(dataID: string): RecordState;
30
+ has(dataID: string): boolean;
31
+ size(): number;
32
+ toJSON(): {
33
+ [key: string]: Record | null | undefined;
34
+ };
35
+ }
36
+ export declare type Snapshot = {
37
+ data: SelectorData | null | undefined;
38
+ seenRecords: Set<string>;
39
+ selector: any;
40
+ };
41
+ export declare type OperationDescriptor = {
42
+ fragment: any;
43
+ request: any;
44
+ root: any;
45
+ };
46
+ export interface Store {
47
+ /**
48
+ * Commit an updater to the environment. This mutation cannot be reverted and
49
+ * should therefore not be used for optimistic updates. This is mainly
50
+ * intended for updating fields from client schema extensions.
51
+ */
52
+ commitUpdate(updater: StoreUpdater): void;
53
+ /**
54
+ * Get a read-only view of the store's internal RecordSource.
55
+ */
56
+ getSource(): RecordSource;
57
+ /**
58
+ * Read the results of a selector from in-memory records in the store.
59
+ */
60
+ lookup(selector: any): Snapshot;
61
+ /**
62
+ * Notify subscribers (see `subscribe`) of any data that was published
63
+ * (`publish()`) since the last time `notify` was called.
64
+ * Optionally provide an OperationDescriptor indicating the source operation
65
+ * that was being processed to produce this run.
66
+ */
67
+ notify(sourceOperation?: OperationDescriptor, invalidateStore?: boolean): any;
68
+ /**
69
+ * Publish new information (e.g. from the network) to the store, updating its
70
+ * internal record source. Subscribers are not immediately notified - this
71
+ * occurs when `notify()` is called.
72
+ */
73
+ publish(source: RecordSource): void;
74
+ /**
75
+ * Ensure that all the records necessary to fulfill the given selector are
76
+ * retained in memory. The records will not be eligible for garbage collection
77
+ * until the returned reference is disposed.
78
+ */
79
+ retain(operation: OperationDescriptor): Disposable;
80
+ /**
81
+ * Subscribe to changes to the results of a selector. The callback is called
82
+ * when `notify()` is called *and* records have been published that affect the
83
+ * selector results relative to the last `notify()`.
84
+ */
85
+ subscribe(snapshot: Snapshot, callback: (snapshot: Snapshot) => void): Disposable;
86
+ }
87
+ export interface MutableRecordSource extends RecordSource {
88
+ clear(): void;
89
+ delete(dataID: string): void;
90
+ remove(dataID: string): void;
91
+ set(dataID: string, record: Record): void;
92
+ }
File without changes
@@ -0,0 +1,86 @@
1
+ export declare type QueryErrorsData = {
2
+ readonly form: {
3
+ readonly entries: ReadonlyArray<{
4
+ readonly check: string | null;
5
+ readonly error: string | null;
6
+ readonly id: string;
7
+ readonly key: string;
8
+ } | null> | null;
9
+ readonly isSubmitting: boolean | null;
10
+ readonly isValidating: boolean | null;
11
+ } | null;
12
+ };
13
+ export declare const QueryErrors: {
14
+ id: string;
15
+ fragment: {
16
+ kind: string;
17
+ name: string;
18
+ selections: {
19
+ kind: string;
20
+ name: string;
21
+ plural: boolean;
22
+ selections: ({
23
+ name: string;
24
+ kind?: undefined;
25
+ plural?: undefined;
26
+ selections?: undefined;
27
+ } | {
28
+ kind: string;
29
+ name: string;
30
+ plural: boolean;
31
+ selections: {
32
+ name: string;
33
+ }[];
34
+ })[];
35
+ }[];
36
+ };
37
+ };
38
+ export declare type QueryFieldsData = {
39
+ readonly form: {
40
+ readonly entries: ReadonlyArray<{
41
+ readonly check: string | null;
42
+ readonly error: string | null;
43
+ readonly id: string;
44
+ readonly key: string;
45
+ readonly value: string | null;
46
+ } | null> | null;
47
+ } | null;
48
+ };
49
+ export declare const QueryFields: {
50
+ id: string;
51
+ fragment: {
52
+ kind: string;
53
+ name: string;
54
+ selections: {
55
+ kind: string;
56
+ name: string;
57
+ plural: boolean;
58
+ selections: {
59
+ kind: string;
60
+ name: string;
61
+ plural: boolean;
62
+ selections: {
63
+ name: string;
64
+ }[];
65
+ }[];
66
+ }[];
67
+ };
68
+ };
69
+ export declare type FragmentSetData = {
70
+ readonly check: string | null;
71
+ readonly id: string;
72
+ };
73
+ export declare const FragmentSet: {
74
+ kind: string;
75
+ name: string;
76
+ selections: {
77
+ name: string;
78
+ }[];
79
+ };
80
+ export declare const FragmentValue: {
81
+ kind: string;
82
+ name: string;
83
+ selections: {
84
+ name: string;
85
+ }[];
86
+ };
@@ -0,0 +1,68 @@
1
+ import { LINKED_FIELD } from './RelayStoreUtils';
2
+ var id = {
3
+ name: 'id'
4
+ };
5
+ var key = {
6
+ name: 'key'
7
+ };
8
+ var check = {
9
+ name: 'check'
10
+ };
11
+ var error = {
12
+ name: 'error'
13
+ };
14
+ var value = {
15
+ name: 'value'
16
+ };
17
+ var kindFragment = 'Fragment';
18
+ export var QueryErrors = {
19
+ id: '1',
20
+ fragment: {
21
+ kind: kindFragment,
22
+ name: 'queryErrors',
23
+ selections: [{
24
+ kind: LINKED_FIELD,
25
+ name: 'form',
26
+ plural: false,
27
+ selections: [{
28
+ name: 'isSubmitting'
29
+ }, {
30
+ name: 'isValidating'
31
+ }, {
32
+ kind: LINKED_FIELD,
33
+ name: 'entries',
34
+ plural: true,
35
+ selections: [id, key, error, check]
36
+ }]
37
+ }] /*: any*/
38
+ }
39
+ };
40
+
41
+ export var QueryFields = {
42
+ id: '0',
43
+ fragment: {
44
+ kind: kindFragment,
45
+ name: 'queryFields',
46
+ selections: [{
47
+ kind: LINKED_FIELD,
48
+ name: 'form',
49
+ plural: false,
50
+ selections: [{
51
+ kind: LINKED_FIELD,
52
+ name: 'entries',
53
+ plural: true,
54
+ selections: [id, key, value, error, check]
55
+ }]
56
+ }]
57
+ }
58
+ };
59
+ export var FragmentSet = {
60
+ kind: kindFragment,
61
+ name: 'fragmentSet',
62
+ selections: [id, check]
63
+ };
64
+ export var FragmentValue = {
65
+ kind: kindFragment,
66
+ name: 'fragmentValue',
67
+ selections: [id, value, error]
68
+ };
@@ -0,0 +1 @@
1
+ export declare function useForceUpdate(): () => void;
@@ -0,0 +1,8 @@
1
+ import { useReducer } from 'react';
2
+ export function useForceUpdate() {
3
+ var _a = useReducer(function (x) {
4
+ return x + 1;
5
+ }, 0),
6
+ forceUpdate = _a[1];
7
+ return forceUpdate;
8
+ }
@@ -0,0 +1,2 @@
1
+ import { FormSetValueOptions, FormSetValueReturn } from './RelayFormsTypes';
2
+ export declare function useFormSetValue<ValueType>({ key, initialValue, validate, validateOnChange, }: FormSetValueOptions<ValueType>): FormSetValueReturn<ValueType>;
@@ -0,0 +1,84 @@
1
+ import React from 'react';
2
+ import { FragmentSet } from './relay/queries';
3
+ import { useStore } from './relay/RelayStoreProvider';
4
+ import { isPromise } from './relay/RelayStoreUtils';
5
+ import { useForceUpdate } from './useForceUpdate';
6
+ import { commitValue, commitErrorIntoRelay, commitResetField, getSnapshot } from './Utils';
7
+ export function useFormSetValue(_a) {
8
+ var key = _a.key,
9
+ initialValue = _a.initialValue,
10
+ validate = _a.validate,
11
+ validateOnChange = _a.validateOnChange;
12
+ var forceUpdate = useForceUpdate();
13
+ var environment = useStore();
14
+ var ref = React.useRef({
15
+ value: initialValue,
16
+ error: undefined,
17
+ check: validate ? 'INIT' : 'DONE',
18
+ isChecking: false
19
+ });
20
+ var setValue = React.useCallback(function (newValue) {
21
+ ref.current.value = newValue;
22
+ if (ref.current.check === 'DONE' && validate) {
23
+ ref.current.check = 'START';
24
+ }
25
+ commitValue(key, newValue, ref.current.check, environment);
26
+ forceUpdate();
27
+ }, [environment, key, validate, forceUpdate]);
28
+ React.useEffect(function () {
29
+ //commitResetField(environment, key);
30
+ commitValue(key, initialValue, ref.current.check, environment);
31
+ var snapshot = getSnapshot(environment, FragmentSet, key);
32
+ var disposeSubscrition = environment.subscribe(snapshot, function (s) {
33
+ var check = s.data.check;
34
+ var isStart = check === 'START';
35
+ var isReset = check === 'RESET';
36
+ ref.current.check = check;
37
+ if (isReset) {
38
+ ref.current.check = validate ? 'INIT' : 'DONE';
39
+ setValue(initialValue);
40
+ } else {
41
+ if (!validate) {
42
+ commitErrorIntoRelay(key, undefined, environment);
43
+ return;
44
+ }
45
+ if (isStart && !ref.current.isChecking) {
46
+ internalValidate(ref.current.value);
47
+ }
48
+ }
49
+ }).dispose;
50
+ var dispose = function () {
51
+ disposeSubscrition();
52
+ commitResetField(environment, key);
53
+ };
54
+ function finalizeCheck(error) {
55
+ ref.current.isChecking = false;
56
+ if (ref.current.error !== error) {
57
+ ref.current.error = error;
58
+ forceUpdate();
59
+ }
60
+ commitErrorIntoRelay(key, error, environment);
61
+ }
62
+ function internalValidate(value) {
63
+ ref.current.isChecking = true;
64
+ var result = validate(value);
65
+ function internalFinalize(error) {
66
+ if (value !== ref.current.value) {
67
+ internalValidate(ref.current.value);
68
+ return;
69
+ }
70
+ finalizeCheck(error);
71
+ }
72
+ if (isPromise(result)) {
73
+ result.then(internalFinalize).catch(internalFinalize);
74
+ } else {
75
+ internalFinalize(result);
76
+ }
77
+ }
78
+ if (validateOnChange) {
79
+ internalValidate(initialValue);
80
+ }
81
+ return dispose;
82
+ }, [environment, initialValue, key, setValue, validate, validateOnChange, forceUpdate]);
83
+ return [ref.current, setValue];
84
+ }
@@ -0,0 +1,2 @@
1
+ import { FormStateReturn } from './RelayFormsTypes';
2
+ export declare const useFormState: () => FormStateReturn;
@@ -0,0 +1,49 @@
1
+ import React from 'react';
2
+ import { useStore } from './relay/RelayStoreProvider';
3
+ import { areEqual } from './relay/RelayStoreUtils';
4
+ import { useForceUpdate } from './useForceUpdate';
5
+ import { operationQueryErrorsForm } from './Utils';
6
+ export var useFormState = function () {
7
+ var ref = React.useRef({
8
+ errors: undefined,
9
+ isValidating: false,
10
+ isSubmitting: false,
11
+ isValid: false
12
+ });
13
+ var forceUpdate = useForceUpdate();
14
+ var environment = useStore();
15
+ React.useEffect(function () {
16
+ function checkError(s) {
17
+ var data = s.data;
18
+ var form = data.form;
19
+ var entries = form.entries;
20
+ var current = ref.current;
21
+ var entryErrors = entries.filter(function (value) {
22
+ return !!value.error;
23
+ });
24
+ var entryValidated = entries.filter(function (value) {
25
+ return value.check === 'DONE';
26
+ });
27
+ var errors = entryErrors.length > 0 ? entryErrors : undefined;
28
+ var isValidating = form.isValidating;
29
+ var isSubmitting = form.isSubmitting;
30
+ var isValid = entries.length === entryValidated.length && (!errors || Object.keys(errors).length === 0);
31
+ var newState = {
32
+ errors: errors,
33
+ isValid: isValid,
34
+ isValidating: isValidating,
35
+ isSubmitting: isSubmitting
36
+ };
37
+ if (!areEqual(current, newState)) {
38
+ ref.current = newState;
39
+ forceUpdate();
40
+ }
41
+ }
42
+ var snapshot = environment.lookup(operationQueryErrorsForm.fragment);
43
+ checkError(snapshot);
44
+ return environment.subscribe(snapshot, function (s) {
45
+ checkError(s);
46
+ }).dispose;
47
+ }, [environment, forceUpdate]);
48
+ return ref.current;
49
+ };
@@ -0,0 +1,2 @@
1
+ import { FormSubmitOptions, FormSubmitReturn } from './RelayFormsTypes';
2
+ export declare const useFormSubmit: <ValueType extends object = object>({ onSubmit, }: FormSubmitOptions<ValueType>) => FormSubmitReturn;