@conform-to/react 1.17.1 → 1.18.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/future/dom.d.ts +4 -17
- package/dist/future/dom.js +75 -122
- package/dist/future/dom.mjs +75 -119
- package/dist/future/forms.js +2 -2
- package/dist/future/forms.mjs +2 -2
- package/dist/future/hooks.d.ts +38 -24
- package/dist/future/hooks.js +317 -93
- package/dist/future/hooks.mjs +320 -97
- package/dist/future/index.d.ts +2 -2
- package/dist/future/index.js +1 -0
- package/dist/future/index.mjs +1 -1
- package/dist/future/intent.js +23 -23
- package/dist/future/intent.mjs +25 -25
- package/dist/future/state.d.ts +5 -5
- package/dist/future/state.js +41 -48
- package/dist/future/state.mjs +42 -50
- package/dist/future/types.d.ts +142 -29
- package/dist/future/util.d.ts +2 -2
- package/dist/future/util.js +10 -10
- package/dist/future/util.mjs +10 -10
- package/package.json +4 -3
package/dist/future/intent.js
CHANGED
|
@@ -92,7 +92,7 @@ function updateListKeys() {
|
|
|
92
92
|
var keys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
93
93
|
var keyToBeRemoved = arguments.length > 1 ? arguments[1] : undefined;
|
|
94
94
|
var updateKey = arguments.length > 2 ? arguments[2] : undefined;
|
|
95
|
-
var basePath = future.
|
|
95
|
+
var basePath = future.parsePath(keyToBeRemoved);
|
|
96
96
|
return util.transformKeys(keys, field => {
|
|
97
97
|
var _updateKey;
|
|
98
98
|
return future.getRelativePath(field, basePath) !== null ? null : (_updateKey = updateKey === null || updateKey === void 0 ? void 0 : updateKey(field)) !== null && _updateKey !== void 0 ? _updateKey : field;
|
|
@@ -135,7 +135,7 @@ var intentHandlers = {
|
|
|
135
135
|
error
|
|
136
136
|
} = _ref;
|
|
137
137
|
var name = (_intent$payload = intent.payload) !== null && _intent$payload !== void 0 ? _intent$payload : '';
|
|
138
|
-
var basePath = future.
|
|
138
|
+
var basePath = future.parsePath(name);
|
|
139
139
|
var allFields = error ?
|
|
140
140
|
// Consider fields / fieldset with errors as touched too
|
|
141
141
|
submission.fields.concat(Object.keys(error.fieldErrors)) : submission.fields;
|
|
@@ -157,8 +157,8 @@ var intentHandlers = {
|
|
|
157
157
|
},
|
|
158
158
|
resolve(value, options) {
|
|
159
159
|
var _options$value;
|
|
160
|
-
var name = future.
|
|
161
|
-
return util.
|
|
160
|
+
var name = future.appendPath(options.name, options.index);
|
|
161
|
+
return util.updatePathValue(value, name, (_options$value = options.value) !== null && _options$value !== void 0 ? _options$value : name === '' ? {} : null);
|
|
162
162
|
},
|
|
163
163
|
update(state, _ref2) {
|
|
164
164
|
var {
|
|
@@ -174,11 +174,11 @@ var intentHandlers = {
|
|
|
174
174
|
// Update the keys only for client updates to avoid double updates if there is no client validation
|
|
175
175
|
if (type === 'client') {
|
|
176
176
|
// TODO: Do we really need to update the keys here?
|
|
177
|
-
var name = future.
|
|
177
|
+
var name = future.appendPath(intent.payload.name, intent.payload.index);
|
|
178
178
|
// Remove all child keys
|
|
179
179
|
listKeys = name === '' ? {} : updateListKeys(state.listKeys, name);
|
|
180
180
|
}
|
|
181
|
-
var basePath = future.
|
|
181
|
+
var basePath = future.parsePath(intent.payload.name);
|
|
182
182
|
var touchedFields = state.touchedFields;
|
|
183
183
|
for (var field of submission.fields) {
|
|
184
184
|
if (basePath.length === 0 || future.getRelativePath(field, basePath) !== null) {
|
|
@@ -200,12 +200,12 @@ var intentHandlers = {
|
|
|
200
200
|
var result = value;
|
|
201
201
|
var itemValue = options.defaultValue;
|
|
202
202
|
if (options.from !== undefined) {
|
|
203
|
-
itemValue = future.
|
|
204
|
-
result = util.
|
|
203
|
+
itemValue = future.getPathValue(result, options.from);
|
|
204
|
+
result = util.updatePathValue(result, options.from, '');
|
|
205
205
|
}
|
|
206
|
-
var list = Array.from(util.
|
|
206
|
+
var list = Array.from(util.getPathArray(result, options.name));
|
|
207
207
|
insertItem(list, itemValue, (_options$index = options.index) !== null && _options$index !== void 0 ? _options$index : list.length);
|
|
208
|
-
return util.
|
|
208
|
+
return util.updatePathValue(result, options.name, list);
|
|
209
209
|
},
|
|
210
210
|
apply(result, options) {
|
|
211
211
|
var _result$error;
|
|
@@ -223,8 +223,8 @@ var intentHandlers = {
|
|
|
223
223
|
}
|
|
224
224
|
if (options.from !== undefined) {
|
|
225
225
|
var _options$index2, _result$error2;
|
|
226
|
-
var index = (_options$index2 = options.index) !== null && _options$index2 !== void 0 ? _options$index2 : util.
|
|
227
|
-
var insertedItemPath = future.
|
|
226
|
+
var index = (_options$index2 = options.index) !== null && _options$index2 !== void 0 ? _options$index2 : util.getPathArray(result.submission.payload, options.name).length;
|
|
227
|
+
var insertedItemPath = future.appendPath(options.name, index);
|
|
228
228
|
var insertedItemErrors = (_result$error2 = result.error) === null || _result$error2 === void 0 ? void 0 : _result$error2.fieldErrors[insertedItemPath];
|
|
229
229
|
if (insertedItemErrors !== null && insertedItemErrors !== void 0 && insertedItemErrors.length) {
|
|
230
230
|
var _result$error$fieldEr, _result$error3, _result$error$formErr, _result$error4, _result$error5;
|
|
@@ -255,7 +255,7 @@ var intentHandlers = {
|
|
|
255
255
|
return state$1;
|
|
256
256
|
}
|
|
257
257
|
var from = intent.payload.from;
|
|
258
|
-
var index = (_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : util.
|
|
258
|
+
var index = (_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : util.getPathArray(submission.payload, intent.payload.name).length;
|
|
259
259
|
var updateListIndex = util.createPathIndexUpdater(intent.payload.name, currentIndex => index <= currentIndex ? currentIndex + 1 : currentIndex);
|
|
260
260
|
var touchedFields = state$1.touchedFields;
|
|
261
261
|
var listKeys = state$1.listKeys;
|
|
@@ -267,7 +267,7 @@ var intentHandlers = {
|
|
|
267
267
|
var _state$listKeys$inten;
|
|
268
268
|
var selectedListKeys = Array.from((_state$listKeys$inten = state$1.listKeys[intent.payload.name]) !== null && _state$listKeys$inten !== void 0 ? _state$listKeys$inten : state.getDefaultListKey(state$1.resetKey, submission.payload, intent.payload.name));
|
|
269
269
|
insertItem(selectedListKeys, util.generateUniqueKey(), index);
|
|
270
|
-
listKeys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.
|
|
270
|
+
listKeys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.appendPath(intent.payload.name, index), updateListIndex)), {}, {
|
|
271
271
|
// Update existing list keys
|
|
272
272
|
[intent.payload.name]: selectedListKeys
|
|
273
273
|
});
|
|
@@ -288,9 +288,9 @@ var intentHandlers = {
|
|
|
288
288
|
return future.isPlainObject(options) && util.isString(options.name) && util.isNumber(options.index) && util.isOptional(options.onInvalid, v => v === 'revert' || v === 'insert');
|
|
289
289
|
},
|
|
290
290
|
resolve(value, options) {
|
|
291
|
-
var list = Array.from(util.
|
|
291
|
+
var list = Array.from(util.getPathArray(value, options.name));
|
|
292
292
|
removeItem(list, options.index);
|
|
293
|
-
return util.
|
|
293
|
+
return util.updatePathValue(value, options.name, list);
|
|
294
294
|
},
|
|
295
295
|
apply(result, options) {
|
|
296
296
|
var _result$error6;
|
|
@@ -310,10 +310,10 @@ var intentHandlers = {
|
|
|
310
310
|
});
|
|
311
311
|
case 'insert':
|
|
312
312
|
{
|
|
313
|
-
var list = Array.from(util.
|
|
313
|
+
var list = Array.from(util.getPathArray(result.targetValue, options.name));
|
|
314
314
|
insertItem(list, options.defaultValue, list.length);
|
|
315
315
|
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, result), {}, {
|
|
316
|
-
targetValue: util.
|
|
316
|
+
targetValue: util.updatePathValue(result.targetValue, options.name, list)
|
|
317
317
|
});
|
|
318
318
|
}
|
|
319
319
|
}
|
|
@@ -349,14 +349,14 @@ var intentHandlers = {
|
|
|
349
349
|
var _state$listKeys$inten2;
|
|
350
350
|
var selectedListKeys = Array.from((_state$listKeys$inten2 = state$1.listKeys[intent.payload.name]) !== null && _state$listKeys$inten2 !== void 0 ? _state$listKeys$inten2 : state.getDefaultListKey(state$1.resetKey, currentValue, intent.payload.name));
|
|
351
351
|
removeItem(selectedListKeys, intent.payload.index);
|
|
352
|
-
listKeys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.
|
|
352
|
+
listKeys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.appendPath(intent.payload.name, intent.payload.index), updateListIndex)), {}, {
|
|
353
353
|
// Update existing list keys
|
|
354
354
|
[intent.payload.name]: selectedListKeys
|
|
355
355
|
});
|
|
356
356
|
if (ctx.cancelled) {
|
|
357
357
|
var index = selectedListKeys.length;
|
|
358
358
|
insertItem(selectedListKeys, util.generateUniqueKey(), index);
|
|
359
|
-
listKeys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.
|
|
359
|
+
listKeys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.appendPath(intent.payload.name, index), updateListIndex)), {}, {
|
|
360
360
|
// Update existing list keys
|
|
361
361
|
[intent.payload.name]: selectedListKeys
|
|
362
362
|
});
|
|
@@ -375,9 +375,9 @@ var intentHandlers = {
|
|
|
375
375
|
return future.isPlainObject(options) && util.isString(options.name) && util.isNumber(options.from) && util.isNumber(options.to);
|
|
376
376
|
},
|
|
377
377
|
resolve(value, options) {
|
|
378
|
-
var list = Array.from(util.
|
|
378
|
+
var list = Array.from(util.getPathArray(value, options.name));
|
|
379
379
|
reorderItems(list, options.from, options.to);
|
|
380
|
-
return util.
|
|
380
|
+
return util.updatePathValue(value, options.name, list);
|
|
381
381
|
},
|
|
382
382
|
update(state$1, _ref5) {
|
|
383
383
|
var {
|
|
@@ -409,7 +409,7 @@ var intentHandlers = {
|
|
|
409
409
|
var _state$listKeys$inten3;
|
|
410
410
|
var listKeys = Array.from((_state$listKeys$inten3 = state$1.listKeys[intent.payload.name]) !== null && _state$listKeys$inten3 !== void 0 ? _state$listKeys$inten3 : state.getDefaultListKey(state$1.resetKey, currentValue, intent.payload.name));
|
|
411
411
|
reorderItems(listKeys, intent.payload.from, intent.payload.to);
|
|
412
|
-
keys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.
|
|
412
|
+
keys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.appendPath(intent.payload.name, intent.payload.from), updateListIndex)), {}, {
|
|
413
413
|
// Update existing list keys
|
|
414
414
|
[intent.payload.name]: listKeys
|
|
415
415
|
});
|
package/dist/future/intent.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { objectSpread2 as _objectSpread2 } from '../_virtual/_rollupPluginBabelHelpers.mjs';
|
|
2
|
-
import {
|
|
3
|
-
import { isOptional, isUndefined, isNullable, appendUniqueItem, merge,
|
|
2
|
+
import { parsePath, getRelativePath, isPlainObject, appendPath, getPathValue } from '@conform-to/dom/future';
|
|
3
|
+
import { isOptional, isUndefined, isNullable, appendUniqueItem, merge, updatePathValue, isString, getPathArray, createPathIndexUpdater, compactMap, generateUniqueKey, isNumber, transformKeys } from './util.mjs';
|
|
4
4
|
import { getDefaultListKey } from './state.mjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -88,7 +88,7 @@ function updateListKeys() {
|
|
|
88
88
|
var keys = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
89
89
|
var keyToBeRemoved = arguments.length > 1 ? arguments[1] : undefined;
|
|
90
90
|
var updateKey = arguments.length > 2 ? arguments[2] : undefined;
|
|
91
|
-
var basePath =
|
|
91
|
+
var basePath = parsePath(keyToBeRemoved);
|
|
92
92
|
return transformKeys(keys, field => {
|
|
93
93
|
var _updateKey;
|
|
94
94
|
return getRelativePath(field, basePath) !== null ? null : (_updateKey = updateKey === null || updateKey === void 0 ? void 0 : updateKey(field)) !== null && _updateKey !== void 0 ? _updateKey : field;
|
|
@@ -131,7 +131,7 @@ var intentHandlers = {
|
|
|
131
131
|
error
|
|
132
132
|
} = _ref;
|
|
133
133
|
var name = (_intent$payload = intent.payload) !== null && _intent$payload !== void 0 ? _intent$payload : '';
|
|
134
|
-
var basePath =
|
|
134
|
+
var basePath = parsePath(name);
|
|
135
135
|
var allFields = error ?
|
|
136
136
|
// Consider fields / fieldset with errors as touched too
|
|
137
137
|
submission.fields.concat(Object.keys(error.fieldErrors)) : submission.fields;
|
|
@@ -153,8 +153,8 @@ var intentHandlers = {
|
|
|
153
153
|
},
|
|
154
154
|
resolve(value, options) {
|
|
155
155
|
var _options$value;
|
|
156
|
-
var name =
|
|
157
|
-
return
|
|
156
|
+
var name = appendPath(options.name, options.index);
|
|
157
|
+
return updatePathValue(value, name, (_options$value = options.value) !== null && _options$value !== void 0 ? _options$value : name === '' ? {} : null);
|
|
158
158
|
},
|
|
159
159
|
update(state, _ref2) {
|
|
160
160
|
var {
|
|
@@ -170,11 +170,11 @@ var intentHandlers = {
|
|
|
170
170
|
// Update the keys only for client updates to avoid double updates if there is no client validation
|
|
171
171
|
if (type === 'client') {
|
|
172
172
|
// TODO: Do we really need to update the keys here?
|
|
173
|
-
var name =
|
|
173
|
+
var name = appendPath(intent.payload.name, intent.payload.index);
|
|
174
174
|
// Remove all child keys
|
|
175
175
|
listKeys = name === '' ? {} : updateListKeys(state.listKeys, name);
|
|
176
176
|
}
|
|
177
|
-
var basePath =
|
|
177
|
+
var basePath = parsePath(intent.payload.name);
|
|
178
178
|
var touchedFields = state.touchedFields;
|
|
179
179
|
for (var field of submission.fields) {
|
|
180
180
|
if (basePath.length === 0 || getRelativePath(field, basePath) !== null) {
|
|
@@ -196,12 +196,12 @@ var intentHandlers = {
|
|
|
196
196
|
var result = value;
|
|
197
197
|
var itemValue = options.defaultValue;
|
|
198
198
|
if (options.from !== undefined) {
|
|
199
|
-
itemValue =
|
|
200
|
-
result =
|
|
199
|
+
itemValue = getPathValue(result, options.from);
|
|
200
|
+
result = updatePathValue(result, options.from, '');
|
|
201
201
|
}
|
|
202
|
-
var list = Array.from(
|
|
202
|
+
var list = Array.from(getPathArray(result, options.name));
|
|
203
203
|
insertItem(list, itemValue, (_options$index = options.index) !== null && _options$index !== void 0 ? _options$index : list.length);
|
|
204
|
-
return
|
|
204
|
+
return updatePathValue(result, options.name, list);
|
|
205
205
|
},
|
|
206
206
|
apply(result, options) {
|
|
207
207
|
var _result$error;
|
|
@@ -219,8 +219,8 @@ var intentHandlers = {
|
|
|
219
219
|
}
|
|
220
220
|
if (options.from !== undefined) {
|
|
221
221
|
var _options$index2, _result$error2;
|
|
222
|
-
var index = (_options$index2 = options.index) !== null && _options$index2 !== void 0 ? _options$index2 :
|
|
223
|
-
var insertedItemPath =
|
|
222
|
+
var index = (_options$index2 = options.index) !== null && _options$index2 !== void 0 ? _options$index2 : getPathArray(result.submission.payload, options.name).length;
|
|
223
|
+
var insertedItemPath = appendPath(options.name, index);
|
|
224
224
|
var insertedItemErrors = (_result$error2 = result.error) === null || _result$error2 === void 0 ? void 0 : _result$error2.fieldErrors[insertedItemPath];
|
|
225
225
|
if (insertedItemErrors !== null && insertedItemErrors !== void 0 && insertedItemErrors.length) {
|
|
226
226
|
var _result$error$fieldEr, _result$error3, _result$error$formErr, _result$error4, _result$error5;
|
|
@@ -251,7 +251,7 @@ var intentHandlers = {
|
|
|
251
251
|
return state;
|
|
252
252
|
}
|
|
253
253
|
var from = intent.payload.from;
|
|
254
|
-
var index = (_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index :
|
|
254
|
+
var index = (_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : getPathArray(submission.payload, intent.payload.name).length;
|
|
255
255
|
var updateListIndex = createPathIndexUpdater(intent.payload.name, currentIndex => index <= currentIndex ? currentIndex + 1 : currentIndex);
|
|
256
256
|
var touchedFields = state.touchedFields;
|
|
257
257
|
var listKeys = state.listKeys;
|
|
@@ -263,7 +263,7 @@ var intentHandlers = {
|
|
|
263
263
|
var _state$listKeys$inten;
|
|
264
264
|
var selectedListKeys = Array.from((_state$listKeys$inten = state.listKeys[intent.payload.name]) !== null && _state$listKeys$inten !== void 0 ? _state$listKeys$inten : getDefaultListKey(state.resetKey, submission.payload, intent.payload.name));
|
|
265
265
|
insertItem(selectedListKeys, generateUniqueKey(), index);
|
|
266
|
-
listKeys = _objectSpread2(_objectSpread2({}, updateListKeys(state.listKeys,
|
|
266
|
+
listKeys = _objectSpread2(_objectSpread2({}, updateListKeys(state.listKeys, appendPath(intent.payload.name, index), updateListIndex)), {}, {
|
|
267
267
|
// Update existing list keys
|
|
268
268
|
[intent.payload.name]: selectedListKeys
|
|
269
269
|
});
|
|
@@ -284,9 +284,9 @@ var intentHandlers = {
|
|
|
284
284
|
return isPlainObject(options) && isString(options.name) && isNumber(options.index) && isOptional(options.onInvalid, v => v === 'revert' || v === 'insert');
|
|
285
285
|
},
|
|
286
286
|
resolve(value, options) {
|
|
287
|
-
var list = Array.from(
|
|
287
|
+
var list = Array.from(getPathArray(value, options.name));
|
|
288
288
|
removeItem(list, options.index);
|
|
289
|
-
return
|
|
289
|
+
return updatePathValue(value, options.name, list);
|
|
290
290
|
},
|
|
291
291
|
apply(result, options) {
|
|
292
292
|
var _result$error6;
|
|
@@ -306,10 +306,10 @@ var intentHandlers = {
|
|
|
306
306
|
});
|
|
307
307
|
case 'insert':
|
|
308
308
|
{
|
|
309
|
-
var list = Array.from(
|
|
309
|
+
var list = Array.from(getPathArray(result.targetValue, options.name));
|
|
310
310
|
insertItem(list, options.defaultValue, list.length);
|
|
311
311
|
return _objectSpread2(_objectSpread2({}, result), {}, {
|
|
312
|
-
targetValue:
|
|
312
|
+
targetValue: updatePathValue(result.targetValue, options.name, list)
|
|
313
313
|
});
|
|
314
314
|
}
|
|
315
315
|
}
|
|
@@ -345,14 +345,14 @@ var intentHandlers = {
|
|
|
345
345
|
var _state$listKeys$inten2;
|
|
346
346
|
var selectedListKeys = Array.from((_state$listKeys$inten2 = state.listKeys[intent.payload.name]) !== null && _state$listKeys$inten2 !== void 0 ? _state$listKeys$inten2 : getDefaultListKey(state.resetKey, currentValue, intent.payload.name));
|
|
347
347
|
removeItem(selectedListKeys, intent.payload.index);
|
|
348
|
-
listKeys = _objectSpread2(_objectSpread2({}, updateListKeys(state.listKeys,
|
|
348
|
+
listKeys = _objectSpread2(_objectSpread2({}, updateListKeys(state.listKeys, appendPath(intent.payload.name, intent.payload.index), updateListIndex)), {}, {
|
|
349
349
|
// Update existing list keys
|
|
350
350
|
[intent.payload.name]: selectedListKeys
|
|
351
351
|
});
|
|
352
352
|
if (ctx.cancelled) {
|
|
353
353
|
var index = selectedListKeys.length;
|
|
354
354
|
insertItem(selectedListKeys, generateUniqueKey(), index);
|
|
355
|
-
listKeys = _objectSpread2(_objectSpread2({}, updateListKeys(state.listKeys,
|
|
355
|
+
listKeys = _objectSpread2(_objectSpread2({}, updateListKeys(state.listKeys, appendPath(intent.payload.name, index), updateListIndex)), {}, {
|
|
356
356
|
// Update existing list keys
|
|
357
357
|
[intent.payload.name]: selectedListKeys
|
|
358
358
|
});
|
|
@@ -371,9 +371,9 @@ var intentHandlers = {
|
|
|
371
371
|
return isPlainObject(options) && isString(options.name) && isNumber(options.from) && isNumber(options.to);
|
|
372
372
|
},
|
|
373
373
|
resolve(value, options) {
|
|
374
|
-
var list = Array.from(
|
|
374
|
+
var list = Array.from(getPathArray(value, options.name));
|
|
375
375
|
reorderItems(list, options.from, options.to);
|
|
376
|
-
return
|
|
376
|
+
return updatePathValue(value, options.name, list);
|
|
377
377
|
},
|
|
378
378
|
update(state, _ref5) {
|
|
379
379
|
var {
|
|
@@ -405,7 +405,7 @@ var intentHandlers = {
|
|
|
405
405
|
var _state$listKeys$inten3;
|
|
406
406
|
var listKeys = Array.from((_state$listKeys$inten3 = state.listKeys[intent.payload.name]) !== null && _state$listKeys$inten3 !== void 0 ? _state$listKeys$inten3 : getDefaultListKey(state.resetKey, currentValue, intent.payload.name));
|
|
407
407
|
reorderItems(listKeys, intent.payload.from, intent.payload.to);
|
|
408
|
-
keys = _objectSpread2(_objectSpread2({}, updateListKeys(state.listKeys,
|
|
408
|
+
keys = _objectSpread2(_objectSpread2({}, updateListKeys(state.listKeys, appendPath(intent.payload.name, intent.payload.from), updateListIndex)), {}, {
|
|
409
409
|
// Update existing list keys
|
|
410
410
|
[intent.payload.name]: listKeys
|
|
411
411
|
});
|
package/dist/future/state.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type FieldName, type
|
|
1
|
+
import { type FieldName, type Serialize, FormError } from '@conform-to/dom/future';
|
|
2
2
|
import type { FieldMetadata, Fieldset, FormContext, FormMetadata, FormState, FormAction, UnknownIntent, IntentHandler, BaseFieldMetadata, BaseFormMetadata, DefineConditionalField } from './types';
|
|
3
3
|
export declare function initializeState<ErrorShape>(options?: {
|
|
4
4
|
defaultValue?: Record<string, unknown> | null | undefined;
|
|
@@ -20,6 +20,7 @@ export declare function updateState<ErrorShape>(state: FormState<ErrorShape>, ac
|
|
|
20
20
|
* Minimizes UI state loss by only invalidating keys when necessary.
|
|
21
21
|
*/
|
|
22
22
|
export declare function pruneListKeys(listKeys: Record<string, string[]>, targetValue: Record<string, unknown>): Record<string, string[]>;
|
|
23
|
+
export declare function getDefaultPayload(context: FormContext<any>, name: string, serialize?: Serialize): unknown;
|
|
23
24
|
export declare function getDefaultValue(context: FormContext<any>, name: string, serialize?: Serialize): string;
|
|
24
25
|
export declare function getDefaultOptions(context: FormContext<any>, name: string, serialize?: Serialize): string[];
|
|
25
26
|
export declare function isDefaultChecked(context: FormContext<any>, name: string, serialize?: Serialize): boolean;
|
|
@@ -34,12 +35,11 @@ export declare function getDefaultListKey(prefix: string, initialValue: Record<s
|
|
|
34
35
|
export declare function getListKey(context: FormContext<any>, name: string): string[];
|
|
35
36
|
export declare function getErrors<ErrorShape>(state: FormState<ErrorShape>, name?: string): ErrorShape[] | undefined;
|
|
36
37
|
export declare function getFieldErrors<ErrorShape>(state: FormState<ErrorShape>, name?: string): Record<string, ErrorShape[]>;
|
|
37
|
-
export declare function isValid(state: FormState<any>, name?: string): boolean;
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
40
|
-
* e.g. "array[0].key" falls back to "array[].key" if specific constraint not found.
|
|
39
|
+
* Checks if fieldErrors contains any errors at the given name or any child path.
|
|
41
40
|
*/
|
|
42
|
-
export declare function
|
|
41
|
+
export declare function hasFieldError(error: FormError<any>, name: string): boolean;
|
|
42
|
+
export declare function isValid(state: FormState<any>, name?: string): boolean;
|
|
43
43
|
export declare function getFormMetadata<ErrorShape, CustomFormMetadata extends Record<string, unknown> = {}, CustomFieldMetadata extends Record<string, unknown> = {}>(context: FormContext<ErrorShape>, options?: {
|
|
44
44
|
serialize?: Serialize | undefined;
|
|
45
45
|
extendFormMetadata?: ((metadata: BaseFormMetadata<ErrorShape>) => CustomFormMetadata) | undefined;
|
package/dist/future/state.js
CHANGED
|
@@ -82,7 +82,7 @@ function updateState(state, action) {
|
|
|
82
82
|
function pruneListKeys(listKeys, targetValue) {
|
|
83
83
|
var result = listKeys;
|
|
84
84
|
for (var [name, keys] of Object.entries(listKeys)) {
|
|
85
|
-
var list = util.
|
|
85
|
+
var list = util.getPathArray(targetValue, name);
|
|
86
86
|
|
|
87
87
|
// Reset list keys only if the length has changed
|
|
88
88
|
// to minimize potential UI state loss due to key changes
|
|
@@ -98,10 +98,16 @@ function pruneListKeys(listKeys, targetValue) {
|
|
|
98
98
|
}
|
|
99
99
|
return result;
|
|
100
100
|
}
|
|
101
|
-
function
|
|
101
|
+
function getDefaultPayload(context, name) {
|
|
102
102
|
var _ref, _context$state$server;
|
|
103
103
|
var serialize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : future.serialize;
|
|
104
|
-
var value = future.
|
|
104
|
+
var value = future.getPathValue((_ref = (_context$state$server = context.state.serverValue) !== null && _context$state$server !== void 0 ? _context$state$server : context.state.targetValue) !== null && _ref !== void 0 ? _ref : context.state.defaultValue, name);
|
|
105
|
+
return future.normalize(value, serialize);
|
|
106
|
+
}
|
|
107
|
+
function getDefaultValue(context, name) {
|
|
108
|
+
var _ref2, _context$state$server2;
|
|
109
|
+
var serialize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : future.serialize;
|
|
110
|
+
var value = future.getPathValue((_ref2 = (_context$state$server2 = context.state.serverValue) !== null && _context$state$server2 !== void 0 ? _context$state$server2 : context.state.targetValue) !== null && _ref2 !== void 0 ? _ref2 : context.state.defaultValue, name);
|
|
105
111
|
var serializedValue = serialize(value);
|
|
106
112
|
if (typeof serializedValue === 'string') {
|
|
107
113
|
return serializedValue;
|
|
@@ -109,9 +115,9 @@ function getDefaultValue(context, name) {
|
|
|
109
115
|
return '';
|
|
110
116
|
}
|
|
111
117
|
function getDefaultOptions(context, name) {
|
|
112
|
-
var
|
|
118
|
+
var _ref3, _context$state$server3;
|
|
113
119
|
var serialize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : future.serialize;
|
|
114
|
-
var value = future.
|
|
120
|
+
var value = future.getPathValue((_ref3 = (_context$state$server3 = context.state.serverValue) !== null && _context$state$server3 !== void 0 ? _context$state$server3 : context.state.targetValue) !== null && _ref3 !== void 0 ? _ref3 : context.state.defaultValue, name);
|
|
115
121
|
var serializedValue = serialize(value);
|
|
116
122
|
if (Array.isArray(serializedValue) && serializedValue.every(item => typeof item === 'string')) {
|
|
117
123
|
return serializedValue;
|
|
@@ -122,12 +128,12 @@ function getDefaultOptions(context, name) {
|
|
|
122
128
|
return [];
|
|
123
129
|
}
|
|
124
130
|
function isDefaultChecked(context, name) {
|
|
125
|
-
var
|
|
131
|
+
var _ref4, _context$state$server4;
|
|
126
132
|
var serialize = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : future.serialize;
|
|
127
|
-
var value = future.
|
|
133
|
+
var value = future.getPathValue((_ref4 = (_context$state$server4 = context.state.serverValue) !== null && _context$state$server4 !== void 0 ? _context$state$server4 : context.state.targetValue) !== null && _ref4 !== void 0 ? _ref4 : context.state.defaultValue, name);
|
|
128
134
|
var serializedValue = serialize(value);
|
|
129
135
|
if (typeof serializedValue === 'string') {
|
|
130
|
-
return serializedValue ===
|
|
136
|
+
return serializedValue === serialize(true);
|
|
131
137
|
}
|
|
132
138
|
return false;
|
|
133
139
|
}
|
|
@@ -143,15 +149,15 @@ function isTouched(state) {
|
|
|
143
149
|
if (state.touchedFields.includes(name)) {
|
|
144
150
|
return true;
|
|
145
151
|
}
|
|
146
|
-
var paths = future.
|
|
152
|
+
var paths = future.parsePath(name);
|
|
147
153
|
return state.touchedFields.some(field => field !== name && future.getRelativePath(field, paths) !== null);
|
|
148
154
|
}
|
|
149
155
|
function getDefaultListKey(prefix, initialValue, name) {
|
|
150
|
-
return util.
|
|
156
|
+
return util.getPathArray(initialValue, name).map((_, index) => "".concat(prefix, "-").concat(future.appendPath(name, index)));
|
|
151
157
|
}
|
|
152
158
|
function getListKey(context, name) {
|
|
153
|
-
var _context$state$listKe, _context$state$listKe2,
|
|
154
|
-
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, (
|
|
159
|
+
var _context$state$listKe, _context$state$listKe2, _ref5, _context$state$server5;
|
|
160
|
+
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, (_ref5 = (_context$state$server5 = context.state.serverValue) !== null && _context$state$server5 !== void 0 ? _context$state$server5 : context.state.targetValue) !== null && _ref5 !== void 0 ? _ref5 : context.state.defaultValue, name);
|
|
155
161
|
}
|
|
156
162
|
function getErrors(state, name) {
|
|
157
163
|
var _state$serverError;
|
|
@@ -169,7 +175,7 @@ function getFieldErrors(state, name) {
|
|
|
169
175
|
var result = {};
|
|
170
176
|
var error = (_state$serverError2 = state.serverError) !== null && _state$serverError2 !== void 0 ? _state$serverError2 : state.clientError;
|
|
171
177
|
if (error) {
|
|
172
|
-
var basePath = future.
|
|
178
|
+
var basePath = future.parsePath(name);
|
|
173
179
|
for (var field of Object.keys(error.fieldErrors)) {
|
|
174
180
|
var relativePath = future.getRelativePath(field, basePath);
|
|
175
181
|
|
|
@@ -179,12 +185,23 @@ function getFieldErrors(state, name) {
|
|
|
179
185
|
}
|
|
180
186
|
var _error = getErrors(state, field);
|
|
181
187
|
if (typeof _error !== 'undefined') {
|
|
182
|
-
result[future.
|
|
188
|
+
result[future.formatPath(relativePath)] = _error;
|
|
183
189
|
}
|
|
184
190
|
}
|
|
185
191
|
}
|
|
186
192
|
return result;
|
|
187
193
|
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Checks if fieldErrors contains any errors at the given name or any child path.
|
|
197
|
+
*/
|
|
198
|
+
function hasFieldError(error, name) {
|
|
199
|
+
var basePath = future.parsePath(name);
|
|
200
|
+
return Object.keys(error.fieldErrors).some(field => {
|
|
201
|
+
var _error$fieldErrors$fi;
|
|
202
|
+
return future.getRelativePath(field, basePath) !== null && ((_error$fieldErrors$fi = error.fieldErrors[field]) === null || _error$fieldErrors$fi === void 0 ? void 0 : _error$fieldErrors$fi.length);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
188
205
|
function isValid(state, name) {
|
|
189
206
|
var _state$serverError3;
|
|
190
207
|
var error = (_state$serverError3 = state.serverError) !== null && _state$serverError3 !== void 0 ? _state$serverError3 : state.clientError;
|
|
@@ -193,7 +210,7 @@ function isValid(state, name) {
|
|
|
193
210
|
if (!error) {
|
|
194
211
|
return true;
|
|
195
212
|
}
|
|
196
|
-
var basePath = future.
|
|
213
|
+
var basePath = future.parsePath(name);
|
|
197
214
|
for (var field of Object.keys(error.fieldErrors)) {
|
|
198
215
|
// When checking a specific field, only check that field and its children
|
|
199
216
|
if (name && !future.getRelativePath(field, basePath)) {
|
|
@@ -213,34 +230,6 @@ function isValid(state, name) {
|
|
|
213
230
|
}
|
|
214
231
|
return true;
|
|
215
232
|
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Gets validation constraint for a field, with fallback to parent array patterns.
|
|
219
|
-
* e.g. "array[0].key" falls back to "array[].key" if specific constraint not found.
|
|
220
|
-
*/
|
|
221
|
-
function getConstraint(context, name) {
|
|
222
|
-
var _context$constraint;
|
|
223
|
-
var constraint = (_context$constraint = context.constraint) === null || _context$constraint === void 0 ? void 0 : _context$constraint[name];
|
|
224
|
-
if (!constraint) {
|
|
225
|
-
var path = future.getPathSegments(name);
|
|
226
|
-
for (var i = path.length - 1; i >= 0; i--) {
|
|
227
|
-
var segment = path[i];
|
|
228
|
-
// Try searching a less specific path for the constraint
|
|
229
|
-
// e.g. `array[0].anotherArray[1].key` -> `array[0].anotherArray[].key` -> `array[].anotherArray[].key`
|
|
230
|
-
if (typeof segment === 'number') {
|
|
231
|
-
// This overrides the current number segment with an empty string
|
|
232
|
-
// which will be treated as an empty bracket
|
|
233
|
-
path[i] = '';
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
var alternative = future.formatPathSegments(path);
|
|
238
|
-
if (name !== alternative) {
|
|
239
|
-
constraint = getConstraint(context, alternative);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
return constraint;
|
|
243
|
-
}
|
|
244
233
|
function getFormMetadata(context, options) {
|
|
245
234
|
var _options$extendFormMe, _options$extendFormMe2;
|
|
246
235
|
var metadata = {
|
|
@@ -301,7 +290,7 @@ function getFormMetadata(context, options) {
|
|
|
301
290
|
return extended;
|
|
302
291
|
}
|
|
303
292
|
function getField(context, options) {
|
|
304
|
-
var _extendFieldMetadata;
|
|
293
|
+
var _context$constraint, _extendFieldMetadata;
|
|
305
294
|
var {
|
|
306
295
|
key,
|
|
307
296
|
name,
|
|
@@ -313,7 +302,7 @@ function getField(context, options) {
|
|
|
313
302
|
})
|
|
314
303
|
} = options;
|
|
315
304
|
var id = "".concat(context.formId, "-field-").concat(name.replace(/[^a-zA-Z0-9._-]/g, '_'));
|
|
316
|
-
var constraint =
|
|
305
|
+
var constraint = (_context$constraint = context.constraint) === null || _context$constraint === void 0 ? void 0 : _context$constraint[name];
|
|
317
306
|
var metadata = {
|
|
318
307
|
key,
|
|
319
308
|
name,
|
|
@@ -339,6 +328,9 @@ function getField(context, options) {
|
|
|
339
328
|
get defaultChecked() {
|
|
340
329
|
return isDefaultChecked(context, name, serialize);
|
|
341
330
|
},
|
|
331
|
+
get defaultPayload() {
|
|
332
|
+
return getDefaultPayload(context, name, serialize);
|
|
333
|
+
},
|
|
342
334
|
get touched() {
|
|
343
335
|
return isTouched(context.state, name);
|
|
344
336
|
},
|
|
@@ -400,7 +392,7 @@ function getFieldset(context, options) {
|
|
|
400
392
|
extendFieldMetadata: options === null || options === void 0 ? void 0 : options.extendFieldMetadata
|
|
401
393
|
});
|
|
402
394
|
return getField(context, {
|
|
403
|
-
name: future.
|
|
395
|
+
name: future.appendPath(options === null || options === void 0 ? void 0 : options.name, name),
|
|
404
396
|
serialize: options.serialize,
|
|
405
397
|
extendFieldMetadata: options.extendFieldMetadata,
|
|
406
398
|
form: options.form
|
|
@@ -418,7 +410,7 @@ function getFieldList(context, options) {
|
|
|
418
410
|
var keys = getListKey(context, options.name);
|
|
419
411
|
return keys.map((key, index) => {
|
|
420
412
|
return getField(context, {
|
|
421
|
-
name: future.
|
|
413
|
+
name: future.appendPath(options.name, index),
|
|
422
414
|
serialize: options.serialize,
|
|
423
415
|
extendFieldMetadata: options.extendFieldMetadata,
|
|
424
416
|
key
|
|
@@ -426,9 +418,9 @@ function getFieldList(context, options) {
|
|
|
426
418
|
});
|
|
427
419
|
}
|
|
428
420
|
|
|
429
|
-
exports.getConstraint = getConstraint;
|
|
430
421
|
exports.getDefaultListKey = getDefaultListKey;
|
|
431
422
|
exports.getDefaultOptions = getDefaultOptions;
|
|
423
|
+
exports.getDefaultPayload = getDefaultPayload;
|
|
432
424
|
exports.getDefaultValue = getDefaultValue;
|
|
433
425
|
exports.getErrors = getErrors;
|
|
434
426
|
exports.getField = getField;
|
|
@@ -437,6 +429,7 @@ exports.getFieldList = getFieldList;
|
|
|
437
429
|
exports.getFieldset = getFieldset;
|
|
438
430
|
exports.getFormMetadata = getFormMetadata;
|
|
439
431
|
exports.getListKey = getListKey;
|
|
432
|
+
exports.hasFieldError = hasFieldError;
|
|
440
433
|
exports.initializeState = initializeState;
|
|
441
434
|
exports.isDefaultChecked = isDefaultChecked;
|
|
442
435
|
exports.isTouched = isTouched;
|