@conform-to/react 1.19.4 → 1.20.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/README.md +1 -1
- package/dist/context.d.ts +2 -2
- package/dist/future/dom.d.ts +2 -2
- package/dist/future/dom.js +9 -4
- package/dist/future/dom.mjs +9 -4
- package/dist/future/forms.d.ts +62 -21
- package/dist/future/forms.js +59 -11
- package/dist/future/forms.mjs +56 -9
- package/dist/future/hooks.d.ts +9 -4
- package/dist/future/hooks.js +67 -35
- package/dist/future/hooks.mjs +70 -38
- package/dist/future/index.d.ts +4 -2
- package/dist/future/index.js +5 -0
- package/dist/future/index.mjs +3 -1
- package/dist/future/intent.d.ts +27 -21
- package/dist/future/intent.js +369 -371
- package/dist/future/intent.mjs +361 -372
- package/dist/future/state.d.ts +48 -21
- package/dist/future/state.js +344 -43
- package/dist/future/state.mjs +339 -46
- package/dist/future/types.d.ts +158 -149
- package/dist/future/util.d.ts +4 -8
- package/dist/future/util.js +25 -60
- package/dist/future/util.mjs +26 -59
- package/dist/helpers.d.ts +2 -2
- package/dist/helpers.js +2 -2
- package/dist/helpers.mjs +2 -2
- package/dist/hooks.d.ts +1 -1
- package/dist/vitest.config.d.ts +0 -1
- package/package.json +2 -2
package/dist/future/state.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { objectSpread2 as _objectSpread2 } from '../_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
-
import { parsePath, getRelativePath, appendPath, deepEqual, getPathValue, normalize,
|
|
3
|
-
import { when, generateUniqueKey, merge, getPathArray } from './util.mjs';
|
|
2
|
+
import { parsePath, getRelativePath, appendPath, deepEqual, formatPath, getPathValue, normalize, isPlainObject } from '@conform-to/dom/future';
|
|
3
|
+
import { when, generateUniqueKey, merge, appendUniqueItem, getPathArray } from './util.mjs';
|
|
4
4
|
|
|
5
5
|
function initializeState(options) {
|
|
6
6
|
var _options$resetKey, _options$defaultValue;
|
|
@@ -12,7 +12,12 @@ function initializeState(options) {
|
|
|
12
12
|
serverValue: null,
|
|
13
13
|
serverError: null,
|
|
14
14
|
clientError: null,
|
|
15
|
-
touchedFields: []
|
|
15
|
+
touchedFields: [],
|
|
16
|
+
customState: initializeCustomState({
|
|
17
|
+
handlers: options === null || options === void 0 ? void 0 : options.customStateHandlers,
|
|
18
|
+
currentState: options === null || options === void 0 ? void 0 : options.lastCustomState,
|
|
19
|
+
result: options === null || options === void 0 ? void 0 : options.result
|
|
20
|
+
})
|
|
16
21
|
};
|
|
17
22
|
}
|
|
18
23
|
|
|
@@ -23,28 +28,30 @@ function initializeState(options) {
|
|
|
23
28
|
* - Initialize: set initial server value
|
|
24
29
|
*/
|
|
25
30
|
function updateState(state, action) {
|
|
26
|
-
var _action$
|
|
27
|
-
if (action.reset) {
|
|
28
|
-
return action.ctx.reset(
|
|
31
|
+
var _action$result$target, _action$result$target2, _action$intent, _action$ctx$intentHan;
|
|
32
|
+
if (action.result.reset) {
|
|
33
|
+
return action.ctx.reset();
|
|
29
34
|
}
|
|
30
|
-
var value = (_action$
|
|
35
|
+
var value = (_action$result$target = action.result.targetValue) !== null && _action$result$target !== void 0 ? _action$result$target : action.result.submission.payload;
|
|
36
|
+
var isClientAction = action.type === 'client' || action.type === 'client:async';
|
|
37
|
+
var hasIntentEffects = action.type !== 'server' && action.type !== 'client:async';
|
|
31
38
|
|
|
32
39
|
// Apply the form error and target value from the result first
|
|
33
|
-
state =
|
|
34
|
-
targetValue: (_action$
|
|
35
|
-
serverValue: action.targetValue ? null : state.serverValue,
|
|
40
|
+
state = isClientAction ? merge(state, {
|
|
41
|
+
targetValue: action.type === 'client' ? (_action$result$target2 = action.result.targetValue) !== null && _action$result$target2 !== void 0 ? _action$result$target2 : state.targetValue : state.targetValue,
|
|
42
|
+
serverValue: action.type === 'client' && action.result.targetValue ? null : state.serverValue,
|
|
36
43
|
// Update client error only if the error is different from the previous one to minimize unnecessary re-renders
|
|
37
|
-
clientError: typeof action.error !== 'undefined' && !deepEqual(state.clientError, action.error) ? action.error : state.clientError,
|
|
44
|
+
clientError: typeof action.result.error !== 'undefined' && !deepEqual(state.clientError, action.result.error) ? action.result.error : state.clientError,
|
|
38
45
|
// Reset server error if form value is changed
|
|
39
|
-
serverError: typeof action.error !== 'undefined' && !deepEqual(state.serverValue, value) ? null : state.serverError
|
|
46
|
+
serverError: typeof action.result.error !== 'undefined' && !deepEqual(state.serverValue, value) ? null : state.serverError
|
|
40
47
|
}) : merge(state, {
|
|
41
48
|
// Clear client error to avoid showing stale errors
|
|
42
49
|
clientError: null,
|
|
43
50
|
// Update server error if the error is defined.
|
|
44
51
|
// There is no need to check if the error is different as we are updating other states as well
|
|
45
|
-
serverError: typeof action.error !== 'undefined' ? action.error : state.serverError,
|
|
46
|
-
listKeys: action.type === 'server' && action.targetValue ? pruneListKeys(state.listKeys, action.targetValue) : state.listKeys,
|
|
47
|
-
targetValue: action.type === 'server' && action.targetValue ? action.targetValue : state.targetValue,
|
|
52
|
+
serverError: typeof action.result.error !== 'undefined' ? action.result.error : state.serverError,
|
|
53
|
+
listKeys: action.type === 'server' && action.result.targetValue ? pruneListKeys(state.listKeys, action.result.targetValue) : state.listKeys,
|
|
54
|
+
targetValue: action.type === 'server' && action.result.targetValue ? action.result.targetValue : state.targetValue,
|
|
48
55
|
// Keep track of the value that the serverError is based on
|
|
49
56
|
serverValue: !deepEqual(state.serverValue, value) ? value : state.serverValue
|
|
50
57
|
});
|
|
@@ -52,23 +59,250 @@ function updateState(state, action) {
|
|
|
52
59
|
var intent = (_action$intent = action.intent) !== null && _action$intent !== void 0 ? _action$intent : {
|
|
53
60
|
type: 'validate'
|
|
54
61
|
};
|
|
55
|
-
var handler = (_action$ctx$
|
|
56
|
-
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
intent: {
|
|
65
|
-
type: intent.type,
|
|
66
|
-
payload: intent.payload
|
|
67
|
-
}
|
|
62
|
+
var handler = (_action$ctx$intentHan = action.ctx.intentHandlers) === null || _action$ctx$intentHan === void 0 ? void 0 : _action$ctx$intentHan[intent.type];
|
|
63
|
+
if (handler && action.type === 'client' && hasIntentEffects) {
|
|
64
|
+
if (typeof handler.move === 'function') {
|
|
65
|
+
var handleMove = handler.move;
|
|
66
|
+
state = moveState(state, action.result.submission.payload, value, name => handleMove({
|
|
67
|
+
name,
|
|
68
|
+
payload: intent.payload,
|
|
69
|
+
status: action.ctx.status,
|
|
70
|
+
targetValue: action.result.targetValue
|
|
68
71
|
}));
|
|
72
|
+
} else if (typeof handler.resolve === 'function') {
|
|
73
|
+
state = invalidateState(state, action.result.submission.payload, value);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (handler && action.type !== 'server' && hasIntentEffects && typeof handler.touch === 'function') {
|
|
77
|
+
var touchedFields = state.touchedFields;
|
|
78
|
+
for (var _name of getFields(action.result)) {
|
|
79
|
+
if (handler.touch({
|
|
80
|
+
name: _name,
|
|
81
|
+
payload: intent.payload
|
|
82
|
+
})) {
|
|
83
|
+
touchedFields = appendUniqueItem(touchedFields, _name);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
state = merge(state, {
|
|
87
|
+
touchedFields
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return merge(state, {
|
|
91
|
+
customState: updateCustomState(state.customState, action, {
|
|
92
|
+
handlers: action.ctx.customStateHandlers
|
|
93
|
+
})
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function getFields(result) {
|
|
97
|
+
var fields = result.submission.fields;
|
|
98
|
+
if (result.error) {
|
|
99
|
+
fields = fields.concat(Object.keys(result.error.fieldErrors));
|
|
100
|
+
}
|
|
101
|
+
var fieldsSet = new Set(['']);
|
|
102
|
+
for (var field of fields) {
|
|
103
|
+
var paths = parsePath(field);
|
|
104
|
+
for (var index = 1; index <= paths.length; index++) {
|
|
105
|
+
fieldsSet.add(formatPath(paths.slice(0, index)));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return Array.from(fieldsSet);
|
|
109
|
+
}
|
|
110
|
+
function getApplyStatus(baseTargetValue, finalTargetValue) {
|
|
111
|
+
if (baseTargetValue === finalTargetValue) {
|
|
112
|
+
return 'applied';
|
|
113
|
+
}
|
|
114
|
+
return typeof finalTargetValue === 'undefined' ? 'reverted' : 'modified';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Fallback state transition for intents that change the payload shape without
|
|
119
|
+
* providing a `move()` mapping. It drops list keys and touched fields under
|
|
120
|
+
* changed paths so stale client state does not point at the wrong fields.
|
|
121
|
+
*/
|
|
122
|
+
function invalidateState(state, previousValue, nextValue) {
|
|
123
|
+
if (previousValue === nextValue) {
|
|
124
|
+
return state;
|
|
125
|
+
}
|
|
126
|
+
var changedNames = [];
|
|
127
|
+
var stack = [{
|
|
128
|
+
previousValue,
|
|
129
|
+
nextValue,
|
|
130
|
+
name: ''
|
|
131
|
+
}];
|
|
132
|
+
var _loop = function _loop() {
|
|
133
|
+
var current = stack.pop();
|
|
134
|
+
if (!current) {
|
|
135
|
+
return 0; // break
|
|
136
|
+
}
|
|
137
|
+
if (Object.is(current.previousValue, current.nextValue)) {
|
|
138
|
+
return 1; // continue
|
|
139
|
+
}
|
|
140
|
+
if (Array.isArray(current.previousValue) && Array.isArray(current.nextValue)) {
|
|
141
|
+
changedNames = changedNames.concat(current.name);
|
|
142
|
+
return 1; // continue
|
|
143
|
+
}
|
|
144
|
+
if (isPlainObject(current.previousValue) && isPlainObject(current.nextValue)) {
|
|
145
|
+
for (var key of new Set([...Object.keys(current.previousValue), ...Object.keys(current.nextValue)])) {
|
|
146
|
+
stack.push({
|
|
147
|
+
previousValue: current.previousValue[key],
|
|
148
|
+
nextValue: current.nextValue[key],
|
|
149
|
+
name: appendPath(current.name, key)
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
return 1; // continue
|
|
153
|
+
}
|
|
154
|
+
var basePath = parsePath(current.name);
|
|
155
|
+
if (changedNames.some(existingName => getRelativePath(current.name, parsePath(existingName)) !== null)) {
|
|
156
|
+
return 1; // continue
|
|
157
|
+
}
|
|
158
|
+
changedNames = changedNames.filter(existingName => getRelativePath(existingName, basePath) === null).concat(current.name);
|
|
159
|
+
},
|
|
160
|
+
_ret;
|
|
161
|
+
while (stack.length > 0) {
|
|
162
|
+
_ret = _loop();
|
|
163
|
+
if (_ret === 0) break;
|
|
164
|
+
if (_ret === 1) continue;
|
|
165
|
+
}
|
|
166
|
+
var basePaths = changedNames.map(parsePath);
|
|
167
|
+
if (basePaths.length === 0) {
|
|
168
|
+
return state;
|
|
169
|
+
}
|
|
170
|
+
var listKeys = state.listKeys;
|
|
171
|
+
var touchedFields = state.touchedFields;
|
|
172
|
+
if (Object.keys(state.listKeys).length > 0) {
|
|
173
|
+
var changed = false;
|
|
174
|
+
var entries = Object.entries(state.listKeys).filter(_ref => {
|
|
175
|
+
var [name] = _ref;
|
|
176
|
+
var keep = !basePaths.some(basePath => getRelativePath(name, basePath) !== null);
|
|
177
|
+
changed || (changed = !keep);
|
|
178
|
+
return keep;
|
|
179
|
+
});
|
|
180
|
+
if (changed) {
|
|
181
|
+
listKeys = Object.fromEntries(entries);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (state.touchedFields.length > 0) {
|
|
185
|
+
var _changed = false;
|
|
186
|
+
var nextTouchedFields = state.touchedFields.filter(name => {
|
|
187
|
+
var keep = !basePaths.some(basePath => getRelativePath(name, basePath) !== null);
|
|
188
|
+
_changed || (_changed = !keep);
|
|
189
|
+
return keep;
|
|
190
|
+
});
|
|
191
|
+
if (_changed) {
|
|
192
|
+
touchedFields = nextTouchedFields;
|
|
69
193
|
}
|
|
70
194
|
}
|
|
71
|
-
return state
|
|
195
|
+
return merge(state, {
|
|
196
|
+
listKeys,
|
|
197
|
+
touchedFields
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Preserves list keys and touched fields for intents that can map old field
|
|
203
|
+
* paths to new ones, such as insert, remove, and reorder.
|
|
204
|
+
*/
|
|
205
|
+
function moveState(state, previousValue, nextValue, move) {
|
|
206
|
+
if (previousValue === nextValue) {
|
|
207
|
+
return state;
|
|
208
|
+
}
|
|
209
|
+
function collectListEntries(value) {
|
|
210
|
+
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
211
|
+
var entries = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
212
|
+
if (Array.isArray(value)) {
|
|
213
|
+
entries.push(name);
|
|
214
|
+
for (var index = 0; index < value.length; index++) {
|
|
215
|
+
collectListEntries(value[index], appendPath(name, index), entries);
|
|
216
|
+
}
|
|
217
|
+
} else if (isPlainObject(value)) {
|
|
218
|
+
for (var [key, childValue] of Object.entries(value)) {
|
|
219
|
+
collectListEntries(childValue, appendPath(name, key), entries);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return entries;
|
|
223
|
+
}
|
|
224
|
+
function getListEntry(name) {
|
|
225
|
+
if (name === null) {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
var paths = parsePath(name);
|
|
229
|
+
var index = paths[paths.length - 1];
|
|
230
|
+
if (typeof index !== 'number') {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
name: formatPath(paths.slice(0, -1)),
|
|
235
|
+
index
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
var targetSlots = {};
|
|
239
|
+
var getTargetSlots = listName => {
|
|
240
|
+
var _targetSlots$listName;
|
|
241
|
+
return (_targetSlots$listName = targetSlots[listName]) !== null && _targetSlots$listName !== void 0 ? _targetSlots$listName : targetSlots[listName] = Array.from({
|
|
242
|
+
length: getPathArray(nextValue, listName).length
|
|
243
|
+
}, () => undefined);
|
|
244
|
+
};
|
|
245
|
+
for (var listName of collectListEntries(previousValue)) {
|
|
246
|
+
var _state$listKeys$listN;
|
|
247
|
+
var sourceKeys = (_state$listKeys$listN = state.listKeys[listName]) !== null && _state$listKeys$listN !== void 0 ? _state$listKeys$listN : getDefaultListKey(state.resetKey, previousValue, listName);
|
|
248
|
+
for (var index = 0; index < sourceKeys.length; index++) {
|
|
249
|
+
var _entry$index, _target$_entry$index;
|
|
250
|
+
var entry = getListEntry(move(appendPath(listName, index)));
|
|
251
|
+
if (!entry) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
var target = getTargetSlots(entry.name);
|
|
255
|
+
if (entry.index >= target.length) {
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
(_target$_entry$index = target[_entry$index = entry.index]) !== null && _target$_entry$index !== void 0 ? _target$_entry$index : target[_entry$index] = sourceKeys[index];
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
var touchedFields = state.touchedFields;
|
|
262
|
+
for (var [_index, currentName] of state.touchedFields.entries()) {
|
|
263
|
+
var movedName = move(currentName);
|
|
264
|
+
if (movedName === currentName) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
touchedFields = state.touchedFields.slice(0, _index);
|
|
268
|
+
if (movedName !== null) {
|
|
269
|
+
touchedFields.push(movedName);
|
|
270
|
+
}
|
|
271
|
+
for (var nextName of state.touchedFields.slice(_index + 1)) {
|
|
272
|
+
var _movedName = move(nextName);
|
|
273
|
+
if (_movedName !== null) {
|
|
274
|
+
touchedFields.push(_movedName);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
var changed = false;
|
|
280
|
+
var result = {};
|
|
281
|
+
var listNames = new Set([...Object.keys(state.listKeys), ...Object.keys(targetSlots)]);
|
|
282
|
+
for (var _listName of listNames) {
|
|
283
|
+
var keys = targetSlots[_listName];
|
|
284
|
+
var currentKeys = state.listKeys[_listName];
|
|
285
|
+
if (!keys) {
|
|
286
|
+
changed = true;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
var nextKeys = keys.map(key => key !== null && key !== void 0 ? key : generateUniqueKey());
|
|
290
|
+
var defaultKeys = getDefaultListKey(state.resetKey, nextValue, _listName);
|
|
291
|
+
if (deepEqual(nextKeys, defaultKeys)) {
|
|
292
|
+
changed || (changed = typeof currentKeys !== 'undefined');
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
if (currentKeys && deepEqual(currentKeys, nextKeys)) {
|
|
296
|
+
result[_listName] = currentKeys;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
result[_listName] = nextKeys;
|
|
300
|
+
changed = true;
|
|
301
|
+
}
|
|
302
|
+
return merge(state, {
|
|
303
|
+
listKeys: changed ? result : state.listKeys,
|
|
304
|
+
touchedFields
|
|
305
|
+
});
|
|
72
306
|
}
|
|
73
307
|
|
|
74
308
|
/**
|
|
@@ -77,8 +311,8 @@ function updateState(state, action) {
|
|
|
77
311
|
*/
|
|
78
312
|
function pruneListKeys(listKeys, targetValue) {
|
|
79
313
|
var result = listKeys;
|
|
80
|
-
for (var [
|
|
81
|
-
var list = getPathArray(targetValue,
|
|
314
|
+
for (var [_name2, keys] of Object.entries(listKeys)) {
|
|
315
|
+
var list = getPathArray(targetValue, _name2);
|
|
82
316
|
|
|
83
317
|
// Reset list keys only if the length has changed
|
|
84
318
|
// to minimize potential UI state loss due to key changes
|
|
@@ -89,22 +323,26 @@ function pruneListKeys(listKeys, targetValue) {
|
|
|
89
323
|
}
|
|
90
324
|
|
|
91
325
|
// Remove the list key to force regeneration
|
|
92
|
-
delete result[
|
|
326
|
+
delete result[_name2];
|
|
93
327
|
}
|
|
94
328
|
}
|
|
95
329
|
return result;
|
|
96
330
|
}
|
|
97
331
|
function getDefaultPayload(context, name) {
|
|
98
|
-
var
|
|
99
|
-
var value = getPathValue((
|
|
332
|
+
var _ref2, _context$state$server;
|
|
333
|
+
var value = getPathValue((_ref2 = (_context$state$server = context.state.serverValue) !== null && _context$state$server !== void 0 ? _context$state$server : context.state.targetValue) !== null && _ref2 !== void 0 ? _ref2 : context.state.defaultValue, name);
|
|
100
334
|
if (value === null) {
|
|
101
335
|
return null;
|
|
102
336
|
}
|
|
103
|
-
return normalize(value,
|
|
337
|
+
return normalize(value, {
|
|
338
|
+
serialize: context.serialize,
|
|
339
|
+
stripEmptyValue: false,
|
|
340
|
+
name
|
|
341
|
+
});
|
|
104
342
|
}
|
|
105
343
|
function getDefaultValue(context, name) {
|
|
106
|
-
var
|
|
107
|
-
var value = getPathValue((
|
|
344
|
+
var _ref3, _context$state$server2;
|
|
345
|
+
var value = getPathValue((_ref3 = (_context$state$server2 = context.state.serverValue) !== null && _context$state$server2 !== void 0 ? _context$state$server2 : context.state.targetValue) !== null && _ref3 !== void 0 ? _ref3 : context.state.defaultValue, name);
|
|
108
346
|
var serializedValue = context.serialize(value, {
|
|
109
347
|
name
|
|
110
348
|
});
|
|
@@ -114,8 +352,8 @@ function getDefaultValue(context, name) {
|
|
|
114
352
|
return '';
|
|
115
353
|
}
|
|
116
354
|
function getDefaultOptions(context, name) {
|
|
117
|
-
var
|
|
118
|
-
var value = getPathValue((
|
|
355
|
+
var _ref4, _context$state$server3;
|
|
356
|
+
var value = getPathValue((_ref4 = (_context$state$server3 = context.state.serverValue) !== null && _context$state$server3 !== void 0 ? _context$state$server3 : context.state.targetValue) !== null && _ref4 !== void 0 ? _ref4 : context.state.defaultValue, name);
|
|
119
357
|
var serializedValue = context.serialize(value, {
|
|
120
358
|
name
|
|
121
359
|
});
|
|
@@ -128,8 +366,8 @@ function getDefaultOptions(context, name) {
|
|
|
128
366
|
return [];
|
|
129
367
|
}
|
|
130
368
|
function isDefaultChecked(context, name) {
|
|
131
|
-
var
|
|
132
|
-
var value = getPathValue((
|
|
369
|
+
var _ref5, _context$state$server4;
|
|
370
|
+
var value = getPathValue((_ref5 = (_context$state$server4 = context.state.serverValue) !== null && _context$state$server4 !== void 0 ? _context$state$server4 : context.state.targetValue) !== null && _ref5 !== void 0 ? _ref5 : context.state.defaultValue, name);
|
|
133
371
|
var serializedValue = context.serialize(value, {
|
|
134
372
|
name
|
|
135
373
|
});
|
|
@@ -159,8 +397,8 @@ function getDefaultListKey(prefix, initialValue, name) {
|
|
|
159
397
|
return getPathArray(initialValue, name).map((_, index) => "".concat(prefix, "-").concat(appendPath(name, index)));
|
|
160
398
|
}
|
|
161
399
|
function getListKey(context, name) {
|
|
162
|
-
var _context$state$listKe, _context$state$listKe2,
|
|
163
|
-
return (_context$state$listKe = (_context$state$listKe2 = context.state.listKeys) === null || _context$state$listKe2 === void 0 ? void 0 : _context$state$listKe2[name]) !== null && _context$state$listKe !== void 0 ? _context$state$listKe : getDefaultListKey(context.state.resetKey, (
|
|
400
|
+
var _context$state$listKe, _context$state$listKe2, _ref6, _context$state$server5;
|
|
401
|
+
return (_context$state$listKe = (_context$state$listKe2 = context.state.listKeys) === null || _context$state$listKe2 === void 0 ? void 0 : _context$state$listKe2[name]) !== null && _context$state$listKe !== void 0 ? _context$state$listKe : getDefaultListKey(context.state.resetKey, (_ref6 = (_context$state$server5 = context.state.serverValue) !== null && _context$state$server5 !== void 0 ? _context$state$server5 : context.state.targetValue) !== null && _ref6 !== void 0 ? _ref6 : context.state.defaultValue, name);
|
|
164
402
|
}
|
|
165
403
|
function getErrors(state, name) {
|
|
166
404
|
var _state$serverError;
|
|
@@ -200,8 +438,8 @@ function getFieldErrors(state, name) {
|
|
|
200
438
|
*/
|
|
201
439
|
function hasFieldError(error, name) {
|
|
202
440
|
var basePath = parsePath(name);
|
|
203
|
-
return Object.entries(error.fieldErrors).some(
|
|
204
|
-
var [field, fieldError] =
|
|
441
|
+
return Object.entries(error.fieldErrors).some(_ref7 => {
|
|
442
|
+
var [field, fieldError] = _ref7;
|
|
205
443
|
return getRelativePath(field, basePath) !== null && fieldError !== null;
|
|
206
444
|
});
|
|
207
445
|
}
|
|
@@ -241,6 +479,9 @@ function getFormMetadata(context, options) {
|
|
|
241
479
|
errorId: "".concat(context.formId, "-form-error"),
|
|
242
480
|
descriptionId: "".concat(context.formId, "-form-description"),
|
|
243
481
|
defaultValue: context.state.defaultValue,
|
|
482
|
+
get customState() {
|
|
483
|
+
return context.state.customState;
|
|
484
|
+
},
|
|
244
485
|
get errors() {
|
|
245
486
|
return getErrors(context.state);
|
|
246
487
|
},
|
|
@@ -410,5 +651,57 @@ function getFieldList(context, options) {
|
|
|
410
651
|
});
|
|
411
652
|
});
|
|
412
653
|
}
|
|
654
|
+
function defineCustomState(definition) {
|
|
655
|
+
return definition;
|
|
656
|
+
}
|
|
657
|
+
function mergeCustomStateHandlers(globalCustomState, inlineCustomState) {
|
|
658
|
+
if (globalCustomState && inlineCustomState) {
|
|
659
|
+
for (var key of Object.keys(inlineCustomState)) {
|
|
660
|
+
if (Object.prototype.hasOwnProperty.call(globalCustomState, key)) {
|
|
661
|
+
throw new Error("Duplicate custom state key \"".concat(key, "\""));
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
return _objectSpread2(_objectSpread2({}, globalCustomState), inlineCustomState);
|
|
666
|
+
}
|
|
667
|
+
function initializeCustomState(options) {
|
|
668
|
+
var _options$handlers;
|
|
669
|
+
return Object.fromEntries(Object.entries((_options$handlers = options.handlers) !== null && _options$handlers !== void 0 ? _options$handlers : {}).map(_ref8 => {
|
|
670
|
+
var [key, handler] = _ref8;
|
|
671
|
+
if (!options.currentState || !Object.prototype.hasOwnProperty.call(options.currentState, key) || handler.reset === undefined || handler.reset === true) {
|
|
672
|
+
return [key, handler.initialize()];
|
|
673
|
+
}
|
|
674
|
+
var currentState = options.currentState[key];
|
|
675
|
+
if (typeof handler.reset === 'function') {
|
|
676
|
+
return [key, handler.reset(currentState, {
|
|
677
|
+
result: options.result
|
|
678
|
+
})];
|
|
679
|
+
}
|
|
680
|
+
return [key, currentState];
|
|
681
|
+
}));
|
|
682
|
+
}
|
|
683
|
+
function updateCustomState(state, action, options) {
|
|
684
|
+
if (!options.handlers) {
|
|
685
|
+
return state;
|
|
686
|
+
}
|
|
687
|
+
return Object.fromEntries(Object.entries(options.handlers).map(_ref9 => {
|
|
688
|
+
var [key, handler] = _ref9;
|
|
689
|
+
var nextState = state[key];
|
|
690
|
+
if (action.type !== 'server' && action.type !== 'client:async' && handler.handleIntent) {
|
|
691
|
+
nextState = handler.handleIntent(nextState, {
|
|
692
|
+
intent: action.intent,
|
|
693
|
+
submission: action.result.submission
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
if (handler.handleResult && typeof action.result.error !== 'undefined') {
|
|
697
|
+
nextState = handler.handleResult(nextState, {
|
|
698
|
+
intent: action.intent,
|
|
699
|
+
result: action.result,
|
|
700
|
+
phase: action.type === 'client' || action.type === 'client:async' ? 'client' : 'server'
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
return [key, nextState];
|
|
704
|
+
}));
|
|
705
|
+
}
|
|
413
706
|
|
|
414
|
-
export { getDefaultListKey, getDefaultOptions, getDefaultPayload, getDefaultValue, getErrors, getField, getFieldErrors, getFieldList, getFieldset, getFormMetadata, getListKey, hasFieldError, initializeState, isDefaultChecked, isTouched, isValid, pruneListKeys, updateState };
|
|
707
|
+
export { defineCustomState, getApplyStatus, getDefaultListKey, getDefaultOptions, getDefaultPayload, getDefaultValue, getErrors, getField, getFieldErrors, getFieldList, getFields, getFieldset, getFormMetadata, getListKey, hasFieldError, initializeCustomState, initializeState, invalidateState, isDefaultChecked, isTouched, isValid, mergeCustomStateHandlers, moveState, pruneListKeys, updateCustomState, updateState };
|