@conform-to/dom 1.0.4 → 1.0.6
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 +1 -1
- package/form.js +15 -2
- package/form.mjs +16 -3
- package/package.json +1 -1
- package/submission.d.ts +5 -1
- package/submission.js +17 -4
- package/submission.mjs +17 -5
package/README
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
╚══════╝ ╚═════╝ ╚═╝ ╚══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
|
|
9
9
|
|
|
10
10
|
|
|
11
|
-
Version 1.0.
|
|
11
|
+
Version 1.0.6 / License MIT / Copyright (c) 2024 Edmund Hung
|
|
12
12
|
|
|
13
13
|
A type-safe form validation library utilizing web fundamentals to progressively enhance HTML Forms with full support for server frameworks like Remix and Next.js.
|
|
14
14
|
|
package/form.js
CHANGED
|
@@ -117,7 +117,14 @@ function handleIntent(meta, intent, fields, initialized) {
|
|
|
117
117
|
if (initialized) {
|
|
118
118
|
meta.initialValue = util.clone(meta.initialValue);
|
|
119
119
|
meta.key = util.clone(meta.key);
|
|
120
|
-
submission.setListState(meta.key, intent,
|
|
120
|
+
submission.setListState(meta.key, intent, defaultValue => {
|
|
121
|
+
if (!Array.isArray(defaultValue) && !formdata.isPlainObject(defaultValue)) {
|
|
122
|
+
return util.generateId();
|
|
123
|
+
}
|
|
124
|
+
return Object.assign(getDefaultKey(defaultValue), {
|
|
125
|
+
[submission.root]: util.generateId()
|
|
126
|
+
});
|
|
127
|
+
});
|
|
121
128
|
submission.setListValue(meta.initialValue, intent);
|
|
122
129
|
}
|
|
123
130
|
submission.setListState(meta.validated, intent);
|
|
@@ -134,6 +141,12 @@ function handleIntent(meta, intent, fields, initialized) {
|
|
|
134
141
|
}, {});
|
|
135
142
|
}
|
|
136
143
|
function updateValue(meta, name, value) {
|
|
144
|
+
if (name === '') {
|
|
145
|
+
meta.initialValue = value;
|
|
146
|
+
meta.value = value;
|
|
147
|
+
meta.key = getDefaultKey(value);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
137
150
|
meta.initialValue = util.clone(meta.initialValue);
|
|
138
151
|
meta.value = util.clone(meta.value);
|
|
139
152
|
meta.key = util.clone(meta.key);
|
|
@@ -354,7 +367,7 @@ function createFormContext(options) {
|
|
|
354
367
|
function resolveTarget(event) {
|
|
355
368
|
var form = getFormElement();
|
|
356
369
|
var element = event.target;
|
|
357
|
-
if (!form || !dom.isFieldElement(element) || element.form !== form || element.name === '') {
|
|
370
|
+
if (!form || !dom.isFieldElement(element) || element.form !== form || !element.form.isConnected || element.name === '') {
|
|
358
371
|
return null;
|
|
359
372
|
}
|
|
360
373
|
return element;
|
package/form.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHe
|
|
|
2
2
|
import { flatten, formatPaths, getPaths, getValue, isPlainObject, setValue, normalize, getFormData, isPrefix } from './formdata.mjs';
|
|
3
3
|
import { getFormAction, getFormEncType, getFormMethod, isFieldElement, requestSubmit } from './dom.mjs';
|
|
4
4
|
import { generateId, clone, invariant } from './util.mjs';
|
|
5
|
-
import { serialize, setListState, setListValue, setState, getSubmissionContext, INTENT, serializeIntent } from './submission.mjs';
|
|
5
|
+
import { serialize, setListState, setListValue, setState, getSubmissionContext, INTENT, serializeIntent, root } from './submission.mjs';
|
|
6
6
|
|
|
7
7
|
function createFormMeta(options, initialized) {
|
|
8
8
|
var _lastResult$initialVa, _options$constraint, _lastResult$state$val, _lastResult$state, _ref;
|
|
@@ -113,7 +113,14 @@ function handleIntent(meta, intent, fields, initialized) {
|
|
|
113
113
|
if (initialized) {
|
|
114
114
|
meta.initialValue = clone(meta.initialValue);
|
|
115
115
|
meta.key = clone(meta.key);
|
|
116
|
-
setListState(meta.key, intent,
|
|
116
|
+
setListState(meta.key, intent, defaultValue => {
|
|
117
|
+
if (!Array.isArray(defaultValue) && !isPlainObject(defaultValue)) {
|
|
118
|
+
return generateId();
|
|
119
|
+
}
|
|
120
|
+
return Object.assign(getDefaultKey(defaultValue), {
|
|
121
|
+
[root]: generateId()
|
|
122
|
+
});
|
|
123
|
+
});
|
|
117
124
|
setListValue(meta.initialValue, intent);
|
|
118
125
|
}
|
|
119
126
|
setListState(meta.validated, intent);
|
|
@@ -130,6 +137,12 @@ function handleIntent(meta, intent, fields, initialized) {
|
|
|
130
137
|
}, {});
|
|
131
138
|
}
|
|
132
139
|
function updateValue(meta, name, value) {
|
|
140
|
+
if (name === '') {
|
|
141
|
+
meta.initialValue = value;
|
|
142
|
+
meta.value = value;
|
|
143
|
+
meta.key = getDefaultKey(value);
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
133
146
|
meta.initialValue = clone(meta.initialValue);
|
|
134
147
|
meta.value = clone(meta.value);
|
|
135
148
|
meta.key = clone(meta.key);
|
|
@@ -350,7 +363,7 @@ function createFormContext(options) {
|
|
|
350
363
|
function resolveTarget(event) {
|
|
351
364
|
var form = getFormElement();
|
|
352
365
|
var element = event.target;
|
|
353
|
-
if (!form || !isFieldElement(element) || element.form !== form || element.name === '') {
|
|
366
|
+
if (!form || !isFieldElement(element) || element.form !== form || !element.form.isConnected || element.name === '') {
|
|
354
367
|
return null;
|
|
355
368
|
}
|
|
356
369
|
return element;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "A set of opinionated helpers built on top of the Constraint Validation API",
|
|
4
4
|
"homepage": "https://conform.guide",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.6",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"module": "index.mjs",
|
|
9
9
|
"types": "index.d.ts",
|
package/submission.d.ts
CHANGED
|
@@ -116,6 +116,10 @@ export declare function getIntent(serializedIntent: string | null | undefined):
|
|
|
116
116
|
export declare function serializeIntent<Schema>(intent: Intent<Schema>): string;
|
|
117
117
|
export declare function updateList(list: unknown, intent: InsertIntent | RemoveIntent | ReorderIntent): void;
|
|
118
118
|
export declare function setListValue(data: Record<string, unknown>, intent: InsertIntent | RemoveIntent | ReorderIntent): void;
|
|
119
|
+
/**
|
|
120
|
+
* A placeholder symbol for the root value of a nested object
|
|
121
|
+
*/
|
|
122
|
+
export declare const root: unique symbol;
|
|
119
123
|
export declare function setState(state: Record<string, unknown>, name: string, valueFn: (value: unknown) => unknown): void;
|
|
120
|
-
export declare function setListState(state: Record<string, unknown>, intent: InsertIntent | RemoveIntent | ReorderIntent, getDefaultValue?: () =>
|
|
124
|
+
export declare function setListState(state: Record<string, unknown>, intent: InsertIntent | RemoveIntent | ReorderIntent, getDefaultValue?: (defaultValue: any) => any): void;
|
|
121
125
|
export declare function serialize<Schema>(defaultValue: DefaultValue<Schema>): FormValue<Schema>;
|
package/submission.js
CHANGED
|
@@ -81,6 +81,15 @@ function parse(payload, options) {
|
|
|
81
81
|
break;
|
|
82
82
|
}
|
|
83
83
|
case 'insert':
|
|
84
|
+
{
|
|
85
|
+
setListValue(context.payload, {
|
|
86
|
+
type: intent.type,
|
|
87
|
+
payload: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, intent.payload), {}, {
|
|
88
|
+
defaultValue: serialize(intent.payload.defaultValue)
|
|
89
|
+
})
|
|
90
|
+
});
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
84
93
|
case 'remove':
|
|
85
94
|
case 'reorder':
|
|
86
95
|
{
|
|
@@ -168,7 +177,7 @@ function updateList(list, intent) {
|
|
|
168
177
|
util.invariant(Array.isArray(list), "Failed to update list. The value is not an array.");
|
|
169
178
|
switch (intent.type) {
|
|
170
179
|
case 'insert':
|
|
171
|
-
list.splice((_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : list.length, 0,
|
|
180
|
+
list.splice((_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : list.length, 0, intent.payload.defaultValue);
|
|
172
181
|
break;
|
|
173
182
|
case 'remove':
|
|
174
183
|
list.splice(intent.payload.index, 1);
|
|
@@ -187,9 +196,12 @@ function setListValue(data, intent) {
|
|
|
187
196
|
return list;
|
|
188
197
|
});
|
|
189
198
|
}
|
|
190
|
-
function setState(state, name, valueFn) {
|
|
191
|
-
var root = Symbol.for('root');
|
|
192
199
|
|
|
200
|
+
/**
|
|
201
|
+
* A placeholder symbol for the root value of a nested object
|
|
202
|
+
*/
|
|
203
|
+
var root = Symbol.for('root');
|
|
204
|
+
function setState(state, name, valueFn) {
|
|
193
205
|
// The keys are sorted in desc so that the root value is handled last
|
|
194
206
|
var keys = Object.keys(state).sort((prev, next) => next.localeCompare(prev));
|
|
195
207
|
var target = {};
|
|
@@ -239,7 +251,7 @@ function setListState(state, intent, getDefaultValue) {
|
|
|
239
251
|
updateList(list, {
|
|
240
252
|
type: intent.type,
|
|
241
253
|
payload: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, intent.payload), {}, {
|
|
242
|
-
defaultValue: getDefaultValue === null || getDefaultValue === void 0 ? void 0 : getDefaultValue()
|
|
254
|
+
defaultValue: getDefaultValue === null || getDefaultValue === void 0 ? void 0 : getDefaultValue(intent.payload.defaultValue)
|
|
243
255
|
})
|
|
244
256
|
});
|
|
245
257
|
break;
|
|
@@ -286,6 +298,7 @@ exports.getIntent = getIntent;
|
|
|
286
298
|
exports.getSubmissionContext = getSubmissionContext;
|
|
287
299
|
exports.parse = parse;
|
|
288
300
|
exports.replySubmission = replySubmission;
|
|
301
|
+
exports.root = root;
|
|
289
302
|
exports.serialize = serialize;
|
|
290
303
|
exports.serializeIntent = serializeIntent;
|
|
291
304
|
exports.setListState = setListState;
|
package/submission.mjs
CHANGED
|
@@ -77,6 +77,15 @@ function parse(payload, options) {
|
|
|
77
77
|
break;
|
|
78
78
|
}
|
|
79
79
|
case 'insert':
|
|
80
|
+
{
|
|
81
|
+
setListValue(context.payload, {
|
|
82
|
+
type: intent.type,
|
|
83
|
+
payload: _objectSpread2(_objectSpread2({}, intent.payload), {}, {
|
|
84
|
+
defaultValue: serialize(intent.payload.defaultValue)
|
|
85
|
+
})
|
|
86
|
+
});
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
80
89
|
case 'remove':
|
|
81
90
|
case 'reorder':
|
|
82
91
|
{
|
|
@@ -164,7 +173,7 @@ function updateList(list, intent) {
|
|
|
164
173
|
invariant(Array.isArray(list), "Failed to update list. The value is not an array.");
|
|
165
174
|
switch (intent.type) {
|
|
166
175
|
case 'insert':
|
|
167
|
-
list.splice((_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : list.length, 0,
|
|
176
|
+
list.splice((_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : list.length, 0, intent.payload.defaultValue);
|
|
168
177
|
break;
|
|
169
178
|
case 'remove':
|
|
170
179
|
list.splice(intent.payload.index, 1);
|
|
@@ -183,9 +192,12 @@ function setListValue(data, intent) {
|
|
|
183
192
|
return list;
|
|
184
193
|
});
|
|
185
194
|
}
|
|
186
|
-
function setState(state, name, valueFn) {
|
|
187
|
-
var root = Symbol.for('root');
|
|
188
195
|
|
|
196
|
+
/**
|
|
197
|
+
* A placeholder symbol for the root value of a nested object
|
|
198
|
+
*/
|
|
199
|
+
var root = Symbol.for('root');
|
|
200
|
+
function setState(state, name, valueFn) {
|
|
189
201
|
// The keys are sorted in desc so that the root value is handled last
|
|
190
202
|
var keys = Object.keys(state).sort((prev, next) => next.localeCompare(prev));
|
|
191
203
|
var target = {};
|
|
@@ -235,7 +247,7 @@ function setListState(state, intent, getDefaultValue) {
|
|
|
235
247
|
updateList(list, {
|
|
236
248
|
type: intent.type,
|
|
237
249
|
payload: _objectSpread2(_objectSpread2({}, intent.payload), {}, {
|
|
238
|
-
defaultValue: getDefaultValue === null || getDefaultValue === void 0 ? void 0 : getDefaultValue()
|
|
250
|
+
defaultValue: getDefaultValue === null || getDefaultValue === void 0 ? void 0 : getDefaultValue(intent.payload.defaultValue)
|
|
239
251
|
})
|
|
240
252
|
});
|
|
241
253
|
break;
|
|
@@ -275,4 +287,4 @@ function serialize(defaultValue) {
|
|
|
275
287
|
}
|
|
276
288
|
}
|
|
277
289
|
|
|
278
|
-
export { INTENT, STATE, createSubmission, getIntent, getSubmissionContext, parse, replySubmission, serialize, serializeIntent, setListState, setListValue, setState, updateList };
|
|
290
|
+
export { INTENT, STATE, createSubmission, getIntent, getSubmissionContext, parse, replySubmission, root, serialize, serializeIntent, setListState, setListValue, setState, updateList };
|