@conform-to/react 1.19.3 → 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/intent.js
CHANGED
|
@@ -5,72 +5,116 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var _rollupPluginBabelHelpers = require('../_virtual/_rollupPluginBabelHelpers.js');
|
|
6
6
|
var future = require('@conform-to/dom/future');
|
|
7
7
|
var util = require('./util.js');
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
function defineIntent(definition) {
|
|
10
|
+
var _definition$parse;
|
|
11
|
+
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, definition), {}, {
|
|
12
|
+
parse: (_definition$parse = definition === null || definition === void 0 ? void 0 : definition.parse) !== null && _definition$parse !== void 0 ? _definition$parse : function () {
|
|
13
|
+
if (arguments.length > 1) {
|
|
14
|
+
throw new Error('Invalid intent arguments');
|
|
15
|
+
}
|
|
16
|
+
return arguments.length <= 0 ? undefined : arguments[0];
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function mergeIntentHandlers(defaultHandlers, customHandlers) {
|
|
21
|
+
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, defaultHandlers), customHandlers);
|
|
22
|
+
}
|
|
23
|
+
var undefinedArg = '$$__undefined__$$';
|
|
24
|
+
function deserializeIntentArgs(value) {
|
|
25
|
+
return JSON.parse("[".concat(value, "]"), (_, value) => value === undefinedArg ? undefined : value);
|
|
26
|
+
}
|
|
9
27
|
|
|
10
28
|
/**
|
|
11
|
-
* Serializes intent to string format
|
|
29
|
+
* Serializes a transport intent to string format.
|
|
12
30
|
*/
|
|
13
31
|
function serializeIntent(intent) {
|
|
14
|
-
|
|
32
|
+
var args = intent.args.slice();
|
|
33
|
+
while (args.length > 0 && args[args.length - 1] === undefined) {
|
|
34
|
+
args.pop();
|
|
35
|
+
}
|
|
36
|
+
if (args.length === 0) {
|
|
15
37
|
return intent.type;
|
|
16
38
|
}
|
|
17
|
-
|
|
39
|
+
var serializedArgs = JSON.stringify(args, function (_, value) {
|
|
40
|
+
if (value === undefined && Array.isArray(this)) {
|
|
41
|
+
return undefinedArg;
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
});
|
|
45
|
+
return "".concat(intent.type, "(").concat(serializedArgs.slice(1, -1), ")");
|
|
18
46
|
}
|
|
19
47
|
|
|
20
48
|
/**
|
|
21
|
-
* Parses serialized intent string
|
|
49
|
+
* Parses the serialized intent string into a transport intent.
|
|
22
50
|
*/
|
|
23
|
-
function deserializeIntent(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
var serializedPayload;
|
|
27
|
-
var openParenIndex = value.indexOf('(');
|
|
28
|
-
if (openParenIndex > 0 && value[value.length - 1] === ')') {
|
|
29
|
-
type = value.slice(0, openParenIndex);
|
|
30
|
-
serializedPayload = value.slice(openParenIndex + 1, -1);
|
|
51
|
+
function deserializeIntent(serializedIntent) {
|
|
52
|
+
if (serializedIntent === '') {
|
|
53
|
+
return undefined;
|
|
31
54
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
55
|
+
var type = serializedIntent;
|
|
56
|
+
var args = [];
|
|
57
|
+
var openParenIndex = serializedIntent.indexOf('(');
|
|
58
|
+
if (openParenIndex > 0 && serializedIntent[serializedIntent.length - 1] === ')') {
|
|
59
|
+
type = serializedIntent.slice(0, openParenIndex);
|
|
60
|
+
var serializedArgs = serializedIntent.slice(openParenIndex + 1, -1);
|
|
61
|
+
if (serializedArgs !== '') {
|
|
62
|
+
try {
|
|
63
|
+
args = deserializeIntentArgs(serializedArgs);
|
|
64
|
+
} catch (_unused) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
37
67
|
}
|
|
38
68
|
}
|
|
39
69
|
return {
|
|
40
70
|
type,
|
|
41
|
-
|
|
71
|
+
args
|
|
42
72
|
};
|
|
43
73
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
74
|
+
function parseIntent(intentValue, options) {
|
|
75
|
+
if (intentValue === null) {
|
|
76
|
+
return {
|
|
77
|
+
type: 'submit',
|
|
78
|
+
payload: undefined
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
var transportIntent = deserializeIntent(intentValue);
|
|
82
|
+
if (!transportIntent) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
var handler = options.handlers[transportIntent.type];
|
|
86
|
+
if (!handler) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
return {
|
|
91
|
+
type: transportIntent.type,
|
|
92
|
+
payload: handler.parse(...transportIntent.args)
|
|
93
|
+
};
|
|
94
|
+
} catch (_unused2) {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
49
98
|
function resolveIntent(submission, options) {
|
|
50
|
-
var
|
|
51
|
-
|
|
99
|
+
var handlers = options.handlers;
|
|
100
|
+
var intent = options.intent;
|
|
101
|
+
var handler = intent ? handlers[intent.type] : null;
|
|
102
|
+
if (!intent || !(handler !== null && handler !== void 0 && handler.resolve)) {
|
|
52
103
|
return submission.payload;
|
|
53
104
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return handler.resolve(submission.payload, intent.payload);
|
|
59
|
-
}
|
|
60
|
-
return submission.payload;
|
|
105
|
+
return handler.resolve({
|
|
106
|
+
value: submission.payload,
|
|
107
|
+
payload: intent.payload
|
|
108
|
+
});
|
|
61
109
|
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Resolves an intent after validation by calling the handler's onResolve.
|
|
65
|
-
* Mutates the result with updated value/error and returns whether the intent was cancelled.
|
|
66
|
-
*/
|
|
67
110
|
function applyIntent(result, intent, options) {
|
|
68
111
|
if (intent) {
|
|
69
|
-
var
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
112
|
+
var handler = options.handlers[intent.type];
|
|
113
|
+
if (handler !== null && handler !== void 0 && handler.apply) {
|
|
114
|
+
return handler.apply({
|
|
115
|
+
result,
|
|
116
|
+
payload: intent.payload
|
|
117
|
+
});
|
|
74
118
|
}
|
|
75
119
|
}
|
|
76
120
|
return result;
|
|
@@ -84,362 +128,316 @@ function removeItem(list, index) {
|
|
|
84
128
|
function reorderItems(list, fromIndex, toIndex) {
|
|
85
129
|
list.splice(toIndex, 0, ...list.splice(fromIndex, 1));
|
|
86
130
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return util.transformKeys(keys, field => {
|
|
97
|
-
var _updateKey;
|
|
98
|
-
return future.getRelativePath(field, basePath) !== null ? null : (_updateKey = updateKey === null || updateKey === void 0 ? void 0 : updateKey(field)) !== null && _updateKey !== void 0 ? _updateKey : field;
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Built-in action handlers for form intents:
|
|
104
|
-
* - reset: clears form data
|
|
105
|
-
* - validate: marks fields as touched for validation display
|
|
106
|
-
* - update: updates specific field values
|
|
107
|
-
* - insert/remove/reorder: manages array field operations
|
|
108
|
-
*/
|
|
109
|
-
var intentHandlers = {
|
|
110
|
-
reset: {
|
|
111
|
-
validate(options) {
|
|
112
|
-
return util.isOptional(options, future.isPlainObject) && (util.isUndefined(options === null || options === void 0 ? void 0 : options.defaultValue) || util.isNullable(options === null || options === void 0 ? void 0 : options.defaultValue, future.isPlainObject));
|
|
113
|
-
},
|
|
114
|
-
resolve(_, options) {
|
|
115
|
-
if ((options === null || options === void 0 ? void 0 : options.defaultValue) === null) {
|
|
116
|
-
return {};
|
|
117
|
-
}
|
|
118
|
-
return options === null || options === void 0 ? void 0 : options.defaultValue;
|
|
119
|
-
},
|
|
120
|
-
apply(result) {
|
|
121
|
-
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, result), {}, {
|
|
122
|
-
reset: true
|
|
123
|
-
});
|
|
131
|
+
var submit = defineIntent({
|
|
132
|
+
touch() {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
var reset = defineIntent({
|
|
137
|
+
parse(options) {
|
|
138
|
+
if (!util.isOptional(options, future.isPlainObject) || !util.isUndefined(options === null || options === void 0 ? void 0 : options.defaultValue) && !util.isNullable(options === null || options === void 0 ? void 0 : options.defaultValue, future.isPlainObject)) {
|
|
139
|
+
throw new Error('Invalid reset intent arguments');
|
|
124
140
|
}
|
|
141
|
+
return options;
|
|
125
142
|
},
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
var {
|
|
133
|
-
submission,
|
|
134
|
-
intent,
|
|
135
|
-
error
|
|
136
|
-
} = _ref;
|
|
137
|
-
var name = (_intent$payload = intent.payload) !== null && _intent$payload !== void 0 ? _intent$payload : '';
|
|
138
|
-
var basePath = future.parsePath(name);
|
|
139
|
-
var allFields = error ?
|
|
140
|
-
// Consider fields / fieldset with errors as touched too
|
|
141
|
-
submission.fields.concat(Object.keys(error.fieldErrors)) : submission.fields;
|
|
142
|
-
var touchedFields = util.appendUniqueItem(state.touchedFields, name);
|
|
143
|
-
for (var field of allFields) {
|
|
144
|
-
// Add all child fields to the touched fields too
|
|
145
|
-
if (future.getRelativePath(field, basePath) !== null) {
|
|
146
|
-
touchedFields = util.appendUniqueItem(touchedFields, field);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
return util.merge(state, {
|
|
150
|
-
touchedFields
|
|
151
|
-
});
|
|
143
|
+
resolve(_ref) {
|
|
144
|
+
var {
|
|
145
|
+
payload
|
|
146
|
+
} = _ref;
|
|
147
|
+
if ((payload === null || payload === void 0 ? void 0 : payload.defaultValue) === null) {
|
|
148
|
+
return {};
|
|
152
149
|
}
|
|
150
|
+
return payload === null || payload === void 0 ? void 0 : payload.defaultValue;
|
|
153
151
|
},
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
intent
|
|
168
|
-
} = _ref2;
|
|
169
|
-
if (type === 'server') {
|
|
170
|
-
return state;
|
|
171
|
-
}
|
|
172
|
-
var listKeys = state.listKeys;
|
|
173
|
-
|
|
174
|
-
// Update the keys only for client updates to avoid double updates if there is no client validation
|
|
175
|
-
if (type === 'client') {
|
|
176
|
-
// TODO: Do we really need to update the keys here?
|
|
177
|
-
var name = future.appendPath(intent.payload.name, intent.payload.index);
|
|
178
|
-
// Remove all child keys
|
|
179
|
-
listKeys = name === '' ? {} : updateListKeys(state.listKeys, name);
|
|
180
|
-
}
|
|
181
|
-
var basePath = future.parsePath(intent.payload.name);
|
|
182
|
-
var touchedFields = state.touchedFields;
|
|
183
|
-
for (var field of submission.fields) {
|
|
184
|
-
if (basePath.length === 0 || future.getRelativePath(field, basePath) !== null) {
|
|
185
|
-
touchedFields = util.appendUniqueItem(touchedFields, field);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state), {}, {
|
|
189
|
-
listKeys,
|
|
190
|
-
touchedFields
|
|
191
|
-
});
|
|
152
|
+
apply(_ref2) {
|
|
153
|
+
var {
|
|
154
|
+
result
|
|
155
|
+
} = _ref2;
|
|
156
|
+
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, result), {}, {
|
|
157
|
+
reset: true
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
var validate = defineIntent({
|
|
162
|
+
parse(name) {
|
|
163
|
+
if (!util.isOptional(name, util.isString)) {
|
|
164
|
+
throw new Error('Invalid validate intent arguments');
|
|
192
165
|
}
|
|
166
|
+
return name;
|
|
193
167
|
},
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
168
|
+
touch(_ref3) {
|
|
169
|
+
var {
|
|
170
|
+
name,
|
|
171
|
+
payload
|
|
172
|
+
} = _ref3;
|
|
173
|
+
return future.getRelativePath(name, payload !== null && payload !== void 0 ? payload : '') !== null;
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
var update = defineIntent({
|
|
177
|
+
parse(options) {
|
|
178
|
+
if (!future.isPlainObject(options) || !util.isOptional(options.name, util.isString) || !util.isOptional(options.index, util.isNumber) || util.isUndefined(options.value)) {
|
|
179
|
+
throw new Error('Invalid update intent arguments');
|
|
180
|
+
}
|
|
181
|
+
return options;
|
|
182
|
+
},
|
|
183
|
+
resolve(_ref4) {
|
|
184
|
+
var _payload$value;
|
|
185
|
+
var {
|
|
186
|
+
value,
|
|
187
|
+
payload
|
|
188
|
+
} = _ref4;
|
|
189
|
+
var fieldName = future.appendPath(payload.name, payload.index);
|
|
190
|
+
var nextValue = (_payload$value = payload.value) !== null && _payload$value !== void 0 ? _payload$value : fieldName === '' ? {} : null;
|
|
191
|
+
return util.updatePathValue(value, fieldName, nextValue);
|
|
192
|
+
},
|
|
193
|
+
touch(_ref5) {
|
|
194
|
+
var {
|
|
195
|
+
name,
|
|
196
|
+
payload
|
|
197
|
+
} = _ref5;
|
|
198
|
+
var fieldName = future.appendPath(payload.name, payload.index);
|
|
199
|
+
return future.getRelativePath(name, fieldName) !== null;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
var insert = defineIntent({
|
|
203
|
+
parse(options) {
|
|
204
|
+
if (!future.isPlainObject(options) || !util.isString(options.name) || !util.isOptional(options.index, util.isNumber) || !util.isOptional(options.from, util.isString) || !util.isOptional(options.onInvalid, mode => mode === 'revert')) {
|
|
205
|
+
throw new Error('Invalid insert intent arguments');
|
|
206
|
+
}
|
|
207
|
+
return options;
|
|
208
|
+
},
|
|
209
|
+
resolve(_ref6) {
|
|
210
|
+
var _payload$index;
|
|
211
|
+
var {
|
|
212
|
+
value,
|
|
213
|
+
payload
|
|
214
|
+
} = _ref6;
|
|
215
|
+
var result = value;
|
|
216
|
+
var itemValue = payload.defaultValue;
|
|
217
|
+
if (payload.from !== undefined) {
|
|
218
|
+
itemValue = future.getPathValue(result, payload.from);
|
|
219
|
+
result = util.updatePathValue(result, payload.from, '');
|
|
220
|
+
}
|
|
221
|
+
var list = Array.from(util.getPathArray(result, payload.name));
|
|
222
|
+
insertItem(list, itemValue, (_payload$index = payload.index) !== null && _payload$index !== void 0 ? _payload$index : list.length);
|
|
223
|
+
return util.updatePathValue(result, payload.name, list);
|
|
224
|
+
},
|
|
225
|
+
apply(_ref7) {
|
|
226
|
+
var _result$error;
|
|
227
|
+
var {
|
|
228
|
+
result,
|
|
229
|
+
payload
|
|
230
|
+
} = _ref7;
|
|
231
|
+
// Warn if validation result is not yet available
|
|
232
|
+
if (typeof result.error === 'undefined' && (payload.onInvalid || payload.from)) {
|
|
233
|
+
// eslint-disable-next-line no-console
|
|
234
|
+
console.warn('intent.insert() with `onInvalid` or `from` requires the validation result to be available synchronously. ' + 'These options are ignored because the error is not yet known.');
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
var listError = (_result$error = result.error) === null || _result$error === void 0 ? void 0 : _result$error.fieldErrors[payload.name];
|
|
238
|
+
if (payload.onInvalid === 'revert' && listError != null) {
|
|
239
|
+
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, result), {}, {
|
|
240
|
+
targetValue: undefined
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
if (payload.from !== undefined) {
|
|
244
|
+
var _payload$index2, _result$error2, _result$error3;
|
|
245
|
+
var index = (_payload$index2 = payload.index) !== null && _payload$index2 !== void 0 ? _payload$index2 : util.getPathArray(result.submission.payload, payload.name).length;
|
|
246
|
+
var insertedItemPath = future.appendPath(payload.name, index);
|
|
247
|
+
var insertedItemError = (_result$error2 = result.error) === null || _result$error2 === void 0 ? void 0 : _result$error2.fieldErrors[insertedItemPath];
|
|
248
|
+
var fromFieldError = (_result$error3 = result.error) === null || _result$error3 === void 0 ? void 0 : _result$error3.fieldErrors[payload.from];
|
|
249
|
+
if (fromFieldError != null) {
|
|
250
|
+
var _result$error$formErr, _result$error4, _result$error5;
|
|
220
251
|
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, result), {}, {
|
|
221
|
-
targetValue: undefined
|
|
252
|
+
targetValue: undefined,
|
|
253
|
+
error: {
|
|
254
|
+
formErrors: (_result$error$formErr = (_result$error4 = result.error) === null || _result$error4 === void 0 ? void 0 : _result$error4.formErrors) !== null && _result$error$formErr !== void 0 ? _result$error$formErr : null,
|
|
255
|
+
fieldErrors: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, (_result$error5 = result.error) === null || _result$error5 === void 0 ? void 0 : _result$error5.fieldErrors), {}, {
|
|
256
|
+
[insertedItemPath]: null
|
|
257
|
+
})
|
|
258
|
+
}
|
|
222
259
|
});
|
|
223
260
|
}
|
|
224
|
-
if (
|
|
225
|
-
var
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
fieldErrors: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, (_result$error5 = result.error) === null || _result$error5 === void 0 ? void 0 : _result$error5.fieldErrors), {}, {
|
|
237
|
-
[insertedItemPath]: null
|
|
238
|
-
})
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
if (insertedItemError != null) {
|
|
243
|
-
var _result$error$formErr2, _result$error6, _result$error7;
|
|
244
|
-
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, result), {}, {
|
|
245
|
-
targetValue: undefined,
|
|
246
|
-
error: {
|
|
247
|
-
formErrors: (_result$error$formErr2 = (_result$error6 = result.error) === null || _result$error6 === void 0 ? void 0 : _result$error6.formErrors) !== null && _result$error$formErr2 !== void 0 ? _result$error$formErr2 : null,
|
|
248
|
-
fieldErrors: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, (_result$error7 = result.error) === null || _result$error7 === void 0 ? void 0 : _result$error7.fieldErrors), {}, {
|
|
249
|
-
[options.from]: insertedItemError,
|
|
250
|
-
[insertedItemPath]: null
|
|
251
|
-
})
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
return result;
|
|
257
|
-
},
|
|
258
|
-
update(state$1, _ref3) {
|
|
259
|
-
var _intent$payload$index;
|
|
260
|
-
var {
|
|
261
|
-
type,
|
|
262
|
-
submission,
|
|
263
|
-
intent,
|
|
264
|
-
ctx
|
|
265
|
-
} = _ref3;
|
|
266
|
-
if (type === 'server') {
|
|
267
|
-
return state$1;
|
|
268
|
-
}
|
|
269
|
-
var from = intent.payload.from;
|
|
270
|
-
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;
|
|
271
|
-
var updateListIndex = util.createPathIndexUpdater(intent.payload.name, currentIndex => index <= currentIndex ? currentIndex + 1 : currentIndex);
|
|
272
|
-
var touchedFields = state$1.touchedFields;
|
|
273
|
-
var listKeys = state$1.listKeys;
|
|
274
|
-
if (!ctx.cancelled) {
|
|
275
|
-
touchedFields = util.compactMap(state$1.touchedFields, updateListIndex);
|
|
276
|
-
|
|
277
|
-
// Update the keys only for client updates to avoid double updates if there is no client validation
|
|
278
|
-
if (type === 'client') {
|
|
279
|
-
var _state$listKeys$inten;
|
|
280
|
-
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));
|
|
281
|
-
insertItem(selectedListKeys, util.generateUniqueKey(), index);
|
|
282
|
-
listKeys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.appendPath(intent.payload.name, index), updateListIndex)), {}, {
|
|
283
|
-
// Update existing list keys
|
|
284
|
-
[intent.payload.name]: selectedListKeys
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
touchedFields = util.appendUniqueItem(touchedFields, intent.payload.name);
|
|
289
|
-
if (from !== undefined) {
|
|
290
|
-
touchedFields = util.appendUniqueItem(touchedFields, from);
|
|
261
|
+
if (insertedItemError != null) {
|
|
262
|
+
var _result$error$formErr2, _result$error6, _result$error7;
|
|
263
|
+
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, result), {}, {
|
|
264
|
+
targetValue: undefined,
|
|
265
|
+
error: {
|
|
266
|
+
formErrors: (_result$error$formErr2 = (_result$error6 = result.error) === null || _result$error6 === void 0 ? void 0 : _result$error6.formErrors) !== null && _result$error$formErr2 !== void 0 ? _result$error$formErr2 : null,
|
|
267
|
+
fieldErrors: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, (_result$error7 = result.error) === null || _result$error7 === void 0 ? void 0 : _result$error7.fieldErrors), {}, {
|
|
268
|
+
[payload.from]: insertedItemError,
|
|
269
|
+
[insertedItemPath]: null
|
|
270
|
+
})
|
|
271
|
+
}
|
|
272
|
+
});
|
|
291
273
|
}
|
|
292
|
-
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state$1), {}, {
|
|
293
|
-
listKeys,
|
|
294
|
-
touchedFields
|
|
295
|
-
});
|
|
296
274
|
}
|
|
275
|
+
return result;
|
|
297
276
|
},
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
277
|
+
touch(_ref8) {
|
|
278
|
+
var {
|
|
279
|
+
name,
|
|
280
|
+
payload
|
|
281
|
+
} = _ref8;
|
|
282
|
+
return name === payload.name || name === payload.from;
|
|
283
|
+
},
|
|
284
|
+
move(_ref9) {
|
|
285
|
+
var {
|
|
286
|
+
name,
|
|
287
|
+
payload,
|
|
288
|
+
status
|
|
289
|
+
} = _ref9;
|
|
290
|
+
if (status !== 'applied' || typeof payload.index === 'undefined') {
|
|
291
|
+
return name;
|
|
292
|
+
}
|
|
293
|
+
return util.updatePathIndex(name, payload.name, currentIndex => payload.index !== undefined && payload.index <= currentIndex ? currentIndex + 1 : currentIndex);
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
var remove = defineIntent({
|
|
297
|
+
parse(options) {
|
|
298
|
+
if (!future.isPlainObject(options) || !util.isString(options.name) || !util.isNumber(options.index) || !util.isOptional(options.onInvalid, v => v === 'revert' || v === 'insert')) {
|
|
299
|
+
throw new Error('Invalid remove intent arguments');
|
|
300
|
+
}
|
|
301
|
+
return options;
|
|
302
|
+
},
|
|
303
|
+
resolve(_ref10) {
|
|
304
|
+
var {
|
|
305
|
+
value,
|
|
306
|
+
payload
|
|
307
|
+
} = _ref10;
|
|
308
|
+
var list = Array.from(util.getPathArray(value, payload.name));
|
|
309
|
+
removeItem(list, payload.index);
|
|
310
|
+
return util.updatePathValue(value, payload.name, list);
|
|
311
|
+
},
|
|
312
|
+
apply(_ref11) {
|
|
313
|
+
var _result$error8;
|
|
314
|
+
var {
|
|
315
|
+
result,
|
|
316
|
+
payload
|
|
317
|
+
} = _ref11;
|
|
318
|
+
// Warn if validation result is not yet available
|
|
319
|
+
if (typeof result.error === 'undefined' && payload.onInvalid) {
|
|
320
|
+
// eslint-disable-next-line no-console
|
|
321
|
+
console.warn('intent.remove() with `onInvalid` requires the validation result to be available synchronously. ' + 'This option is ignored because the error is not yet known.');
|
|
333
322
|
return result;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
ctx
|
|
341
|
-
} = _ref4;
|
|
342
|
-
if (type === 'server') {
|
|
343
|
-
return state$1;
|
|
344
|
-
}
|
|
345
|
-
var currentValue = submission.payload;
|
|
346
|
-
var updateListIndex = util.createPathIndexUpdater(intent.payload.name, currentIndex => {
|
|
347
|
-
if (intent.payload.index === currentIndex) {
|
|
348
|
-
return null;
|
|
349
|
-
}
|
|
350
|
-
return intent.payload.index < currentIndex ? currentIndex - 1 : currentIndex;
|
|
351
|
-
});
|
|
352
|
-
var touchedFields = state$1.touchedFields;
|
|
353
|
-
var listKeys = state$1.listKeys;
|
|
354
|
-
|
|
355
|
-
// If onInvalid is 'insert', we still remove the item and then insert a new item at the end
|
|
356
|
-
if (!ctx.cancelled || intent.payload.onInvalid === 'insert') {
|
|
357
|
-
touchedFields = util.compactMap(touchedFields, updateListIndex);
|
|
358
|
-
|
|
359
|
-
// Update the keys only for client updates to avoid double updates if there is no client validation
|
|
360
|
-
if (type === 'client') {
|
|
361
|
-
var _state$listKeys$inten2;
|
|
362
|
-
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));
|
|
363
|
-
removeItem(selectedListKeys, intent.payload.index);
|
|
364
|
-
listKeys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.appendPath(intent.payload.name, intent.payload.index), updateListIndex)), {}, {
|
|
365
|
-
// Update existing list keys
|
|
366
|
-
[intent.payload.name]: selectedListKeys
|
|
323
|
+
}
|
|
324
|
+
if (result.targetValue && (_result$error8 = result.error) !== null && _result$error8 !== void 0 && _result$error8.fieldErrors[payload.name]) {
|
|
325
|
+
switch (payload.onInvalid) {
|
|
326
|
+
case 'revert':
|
|
327
|
+
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, result), {}, {
|
|
328
|
+
targetValue: undefined
|
|
367
329
|
});
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
330
|
+
case 'insert':
|
|
331
|
+
{
|
|
332
|
+
var list = Array.from(util.getPathArray(result.targetValue, payload.name));
|
|
333
|
+
insertItem(list, payload.defaultValue, list.length);
|
|
334
|
+
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, result), {}, {
|
|
335
|
+
targetValue: util.updatePathValue(result.targetValue, payload.name, list)
|
|
374
336
|
});
|
|
375
337
|
}
|
|
376
|
-
}
|
|
377
338
|
}
|
|
378
|
-
touchedFields = util.appendUniqueItem(touchedFields, intent.payload.name);
|
|
379
|
-
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, state$1), {}, {
|
|
380
|
-
listKeys: listKeys,
|
|
381
|
-
touchedFields
|
|
382
|
-
});
|
|
383
339
|
}
|
|
340
|
+
return result;
|
|
384
341
|
},
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
var updateListIndex = util.createPathIndexUpdater(intent.payload.name, currentIndex => {
|
|
405
|
-
if (intent.payload.from === intent.payload.to) {
|
|
406
|
-
return currentIndex;
|
|
407
|
-
}
|
|
408
|
-
if (currentIndex === intent.payload.from) {
|
|
409
|
-
return intent.payload.to;
|
|
410
|
-
}
|
|
411
|
-
if (intent.payload.from < intent.payload.to) {
|
|
412
|
-
return currentIndex > intent.payload.from && currentIndex <= intent.payload.to ? currentIndex - 1 : currentIndex;
|
|
413
|
-
}
|
|
414
|
-
return currentIndex >= intent.payload.to && currentIndex < intent.payload.from ? currentIndex + 1 : currentIndex;
|
|
415
|
-
});
|
|
416
|
-
var touchedFields = util.appendUniqueItem(util.compactMap(state$1.touchedFields, updateListIndex), intent.payload.name);
|
|
417
|
-
var keys = state$1.listKeys;
|
|
418
|
-
|
|
419
|
-
// Update the keys only for client updates to avoid double updates if there is no client validation
|
|
420
|
-
if (type === 'client') {
|
|
421
|
-
var _state$listKeys$inten3;
|
|
422
|
-
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));
|
|
423
|
-
reorderItems(listKeys, intent.payload.from, intent.payload.to);
|
|
424
|
-
keys = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, updateListKeys(state$1.listKeys, future.appendPath(intent.payload.name, intent.payload.from), updateListIndex)), {}, {
|
|
425
|
-
// Update existing list keys
|
|
426
|
-
[intent.payload.name]: listKeys
|
|
427
|
-
});
|
|
342
|
+
touch(_ref12) {
|
|
343
|
+
var {
|
|
344
|
+
name,
|
|
345
|
+
payload
|
|
346
|
+
} = _ref12;
|
|
347
|
+
return name === payload.name;
|
|
348
|
+
},
|
|
349
|
+
move(_ref13) {
|
|
350
|
+
var {
|
|
351
|
+
name,
|
|
352
|
+
payload,
|
|
353
|
+
status
|
|
354
|
+
} = _ref13;
|
|
355
|
+
if (status === 'reverted') {
|
|
356
|
+
return name;
|
|
357
|
+
}
|
|
358
|
+
return util.updatePathIndex(name, payload.name, currentIndex => {
|
|
359
|
+
if (payload.index === currentIndex) {
|
|
360
|
+
return null;
|
|
428
361
|
}
|
|
429
|
-
return
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
362
|
+
return payload.index < currentIndex ? currentIndex - 1 : currentIndex;
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
});
|
|
366
|
+
var reorder = defineIntent({
|
|
367
|
+
parse(options) {
|
|
368
|
+
if (!future.isPlainObject(options) || !util.isString(options.name) || !util.isNumber(options.from) || !util.isNumber(options.to)) {
|
|
369
|
+
throw new Error('Invalid reorder intent arguments');
|
|
433
370
|
}
|
|
371
|
+
return options;
|
|
372
|
+
},
|
|
373
|
+
resolve(_ref14) {
|
|
374
|
+
var {
|
|
375
|
+
value,
|
|
376
|
+
payload
|
|
377
|
+
} = _ref14;
|
|
378
|
+
var list = Array.from(util.getPathArray(value, payload.name));
|
|
379
|
+
reorderItems(list, payload.from, payload.to);
|
|
380
|
+
return util.updatePathValue(value, payload.name, list);
|
|
381
|
+
},
|
|
382
|
+
touch(_ref15) {
|
|
383
|
+
var {
|
|
384
|
+
name,
|
|
385
|
+
payload
|
|
386
|
+
} = _ref15;
|
|
387
|
+
return name === payload.name;
|
|
388
|
+
},
|
|
389
|
+
move(_ref16) {
|
|
390
|
+
var {
|
|
391
|
+
name,
|
|
392
|
+
payload
|
|
393
|
+
} = _ref16;
|
|
394
|
+
return util.updatePathIndex(name, payload.name, currentIndex => {
|
|
395
|
+
if (payload.from === payload.to) {
|
|
396
|
+
return currentIndex;
|
|
397
|
+
}
|
|
398
|
+
if (currentIndex === payload.from) {
|
|
399
|
+
return payload.to;
|
|
400
|
+
}
|
|
401
|
+
if (payload.from < payload.to) {
|
|
402
|
+
return currentIndex > payload.from && currentIndex <= payload.to ? currentIndex - 1 : currentIndex;
|
|
403
|
+
}
|
|
404
|
+
return currentIndex >= payload.to && currentIndex < payload.from ? currentIndex + 1 : currentIndex;
|
|
405
|
+
});
|
|
434
406
|
}
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Default Intent handlers
|
|
411
|
+
* - reset: clears form data
|
|
412
|
+
* - validate: marks fields as touched for validation display
|
|
413
|
+
* - update: updates specific field values
|
|
414
|
+
* - insert/remove/reorder: manages array field operations
|
|
415
|
+
*/
|
|
416
|
+
var defaultIntentHandlers = {
|
|
417
|
+
submit,
|
|
418
|
+
reset,
|
|
419
|
+
validate,
|
|
420
|
+
update,
|
|
421
|
+
insert,
|
|
422
|
+
remove,
|
|
423
|
+
reorder
|
|
435
424
|
};
|
|
436
425
|
|
|
437
426
|
exports.applyIntent = applyIntent;
|
|
427
|
+
exports.defaultIntentHandlers = defaultIntentHandlers;
|
|
428
|
+
exports.defineIntent = defineIntent;
|
|
438
429
|
exports.deserializeIntent = deserializeIntent;
|
|
430
|
+
exports.insert = insert;
|
|
439
431
|
exports.insertItem = insertItem;
|
|
440
|
-
exports.
|
|
432
|
+
exports.mergeIntentHandlers = mergeIntentHandlers;
|
|
433
|
+
exports.parseIntent = parseIntent;
|
|
434
|
+
exports.remove = remove;
|
|
441
435
|
exports.removeItem = removeItem;
|
|
436
|
+
exports.reorder = reorder;
|
|
442
437
|
exports.reorderItems = reorderItems;
|
|
438
|
+
exports.reset = reset;
|
|
443
439
|
exports.resolveIntent = resolveIntent;
|
|
444
440
|
exports.serializeIntent = serializeIntent;
|
|
445
|
-
exports.
|
|
441
|
+
exports.submit = submit;
|
|
442
|
+
exports.update = update;
|
|
443
|
+
exports.validate = validate;
|