@conform-to/dom
Advanced tools
Comparing version 1.0.0-pre.2 to 1.0.0-pre.3
@@ -5,22 +5,22 @@ 'use strict'; | ||
function ownKeys(object, enumerableOnly) { | ||
var keys = Object.keys(object); | ||
function ownKeys(e, r) { | ||
var t = Object.keys(e); | ||
if (Object.getOwnPropertySymbols) { | ||
var symbols = Object.getOwnPropertySymbols(object); | ||
enumerableOnly && (symbols = symbols.filter(function (sym) { | ||
return Object.getOwnPropertyDescriptor(object, sym).enumerable; | ||
})), keys.push.apply(keys, symbols); | ||
var o = Object.getOwnPropertySymbols(e); | ||
r && (o = o.filter(function (r) { | ||
return Object.getOwnPropertyDescriptor(e, r).enumerable; | ||
})), t.push.apply(t, o); | ||
} | ||
return keys; | ||
return t; | ||
} | ||
function _objectSpread2(target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = null != arguments[i] ? arguments[i] : {}; | ||
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { | ||
_defineProperty(target, key, source[key]); | ||
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { | ||
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); | ||
function _objectSpread2(e) { | ||
for (var r = 1; r < arguments.length; r++) { | ||
var t = null != arguments[r] ? arguments[r] : {}; | ||
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { | ||
_defineProperty(e, r, t[r]); | ||
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { | ||
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); | ||
}); | ||
} | ||
return target; | ||
return e; | ||
} | ||
@@ -27,0 +27,0 @@ function _defineProperty(obj, key, value) { |
@@ -7,5 +7,5 @@ import { getFormAction, getFormEncType, getFormMethod } from './dom'; | ||
} ? T[K] : undefined; | ||
export type DefaultValue<Schema> = Schema extends string | number | boolean | Date | null | undefined ? Schema | string | undefined : Schema extends File ? undefined : Schema extends Array<infer Item> ? Array<DefaultValue<Item>> : Schema extends Record<string, any> ? { | ||
export type DefaultValue<Schema> = Schema extends string | number | boolean | Date | null | undefined ? Schema | string | null | undefined : Schema extends File ? null | undefined : Schema extends Array<infer Item> ? Array<DefaultValue<Item>> | null | undefined : Schema extends Record<string, any> ? { | ||
[Key in UnionKeyof<Schema>]?: DefaultValue<UnionKeyType<Schema, Key>>; | ||
} : string | undefined; | ||
} | null | undefined : string | null | undefined; | ||
export type FormValue<Schema> = Schema extends string | number | boolean | Date | null | undefined ? string | undefined : Schema extends File ? File | undefined : Schema extends Array<infer Item> ? Array<FormValue<Item>> | undefined : Schema extends Record<string, any> ? { | ||
@@ -31,3 +31,3 @@ [Key in UnionKeyof<Schema>]?: FormValue<UnionKeyType<Schema, Key>>; | ||
}; | ||
export type FormContext<Error> = { | ||
export type FormMeta<Error> = { | ||
submissionStatus?: 'error' | 'success'; | ||
@@ -42,10 +42,3 @@ defaultValue: Record<string, unknown>; | ||
}; | ||
export type FormState<Error = unknown> = { | ||
submissionStatus?: 'error' | 'success'; | ||
defaultValue: Record<string, unknown>; | ||
initialValue: Record<string, unknown>; | ||
value: Record<string, unknown>; | ||
error: Record<string, Error>; | ||
constraint: Record<string, Constraint>; | ||
key: Record<string, string>; | ||
export type FormState<Error> = FormMeta<Error> & { | ||
valid: Record<string, boolean>; | ||
@@ -109,4 +102,4 @@ dirty: Record<string, boolean>; | ||
}; | ||
export type Form<Schema extends Record<string, any> = any, Error = string[], Value = Schema> = { | ||
id: string; | ||
export type FormContext<Schema extends Record<string, any> = any, Error = string[], Value = Schema> = { | ||
formId: string; | ||
submit(event: SubmitEvent): void; | ||
@@ -122,2 +115,2 @@ reset(event: Event): void; | ||
}; | ||
export declare function createForm<Schema extends Record<string, any>, Error, Value>(formId: string, options: FormOptions<Schema, Error, Value>): Form<Schema, Error, Value>; | ||
export declare function createFormContext<Schema extends Record<string, any>, Error, Value>(formId: string, options: FormOptions<Schema, Error, Value>): FormContext<Schema, Error, Value>; |
503
form.js
@@ -11,213 +11,153 @@ 'use strict'; | ||
function createForm(formId, options) { | ||
var subscribers = []; | ||
var latestOptions = options; | ||
var context = initializeFormContext(); | ||
var state = initializeFormState(context); | ||
function getFormElement() { | ||
var element = document.forms.namedItem(formId); | ||
util.invariant(element !== null, "Form#".concat(formId, " does not exist")); | ||
return element; | ||
function createFormMeta(options, initialized) { | ||
var _lastResult$initialVa, _options$constraint, _lastResult$state$val, _lastResult$state, _ref; | ||
var lastResult = !initialized ? options.lastResult : undefined; | ||
var defaultValue = options.defaultValue ? submission.serialize(options.defaultValue) : {}; | ||
var initialValue = (_lastResult$initialVa = lastResult === null || lastResult === void 0 ? void 0 : lastResult.initialValue) !== null && _lastResult$initialVa !== void 0 ? _lastResult$initialVa : defaultValue; | ||
var result = { | ||
submissionStatus: lastResult === null || lastResult === void 0 ? void 0 : lastResult.status, | ||
defaultValue, | ||
initialValue, | ||
value: initialValue, | ||
constraint: (_options$constraint = options.constraint) !== null && _options$constraint !== void 0 ? _options$constraint : {}, | ||
validated: (_lastResult$state$val = lastResult === null || lastResult === void 0 || (_lastResult$state = lastResult.state) === null || _lastResult$state === void 0 ? void 0 : _lastResult$state.validated) !== null && _lastResult$state$val !== void 0 ? _lastResult$state$val : {}, | ||
key: !initialized ? getDefaultKey(defaultValue) : _rollupPluginBabelHelpers.objectSpread2({ | ||
'': util.generateId() | ||
}, getDefaultKey(defaultValue)), | ||
// The `lastResult` should comes from the server which we won't expect the error to be null | ||
// We can consider adding a warning if it happens | ||
error: (_ref = lastResult === null || lastResult === void 0 ? void 0 : lastResult.error) !== null && _ref !== void 0 ? _ref : {} | ||
}; | ||
if (lastResult !== null && lastResult !== void 0 && lastResult.intent) { | ||
handleIntent(result, lastResult.intent); | ||
} | ||
function serialize(defaultValue) { | ||
if (formdata.isPlainObject(defaultValue)) { | ||
// @ts-expect-error FIXME | ||
return Object.entries(defaultValue).reduce((result, _ref) => { | ||
var [key, value] = _ref; | ||
result[key] = serialize(value); | ||
return result; | ||
}, {}); | ||
} else if (Array.isArray(defaultValue)) { | ||
// @ts-expect-error FIXME | ||
return defaultValue.map(serialize); | ||
} else if ( | ||
// @ts-ignore-error FIXME | ||
defaultValue instanceof Date) { | ||
// @ts-expect-error FIXME | ||
return defaultValue.toISOString(); | ||
} else if (typeof defaultValue === 'boolean') { | ||
// @ts-expect-error FIXME | ||
return defaultValue ? 'on' : undefined; | ||
} else { | ||
// @ts-expect-error FIXME | ||
return defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.toString(); | ||
return result; | ||
} | ||
function getDefaultKey(defaultValue, prefix) { | ||
return Object.entries(formdata.flatten(defaultValue, { | ||
prefix | ||
})).reduce((result, _ref2) => { | ||
var [key, value] = _ref2; | ||
if (Array.isArray(value)) { | ||
for (var i = 0; i < value.length; i++) { | ||
result[formdata.formatPaths([...formdata.getPaths(key), i])] = util.generateId(); | ||
} | ||
} | ||
} | ||
function initializeFormContext() { | ||
var _serialize, _options$lastResult$i, _options$lastResult, _options$lastResult2, _options$constraint, _options$lastResult$s, _options$lastResult3, _ref2, _options$lastResult4, _options$lastResult5; | ||
var defaultValue = (_serialize = serialize(options.defaultValue)) !== null && _serialize !== void 0 ? _serialize : {}; | ||
var value = (_options$lastResult$i = (_options$lastResult = options.lastResult) === null || _options$lastResult === void 0 ? void 0 : _options$lastResult.initialValue) !== null && _options$lastResult$i !== void 0 ? _options$lastResult$i : defaultValue; | ||
var result = { | ||
submissionStatus: (_options$lastResult2 = options.lastResult) === null || _options$lastResult2 === void 0 ? void 0 : _options$lastResult2.status, | ||
constraint: (_options$constraint = options.constraint) !== null && _options$constraint !== void 0 ? _options$constraint : {}, | ||
defaultValue, | ||
initialValue: value, | ||
validated: (_options$lastResult$s = (_options$lastResult3 = options.lastResult) === null || _options$lastResult3 === void 0 || (_options$lastResult3 = _options$lastResult3.state) === null || _options$lastResult3 === void 0 ? void 0 : _options$lastResult3.validated) !== null && _options$lastResult$s !== void 0 ? _options$lastResult$s : {}, | ||
key: getDefaultKey(defaultValue), | ||
value, | ||
// The `lastResult` should comes from the server which we won't expect the error to be null | ||
// We can consider adding a warning if it happens | ||
error: (_ref2 = (_options$lastResult4 = options.lastResult) === null || _options$lastResult4 === void 0 ? void 0 : _options$lastResult4.error) !== null && _ref2 !== void 0 ? _ref2 : {} | ||
}; | ||
if ((_options$lastResult5 = options.lastResult) !== null && _options$lastResult5 !== void 0 && _options$lastResult5.intents) { | ||
handleIntents(options.lastResult.intents, result); | ||
} | ||
return result; | ||
} | ||
function initializeFormState(context) { | ||
var defaultValue = formdata.flatten(context.defaultValue); | ||
var initialValue = context.initialValue === context.defaultValue ? defaultValue : formdata.flatten(context.initialValue); | ||
var value = context.value === context.initialValue ? defaultValue : formdata.flatten(context.value); | ||
return { | ||
defaultValue, | ||
initialValue, | ||
value, | ||
error: context.error, | ||
constraint: createConstraintProxy(context.constraint), | ||
key: createKeyProxy(context.key), | ||
valid: createValidProxy(context.error), | ||
dirty: createDirtyProxy(defaultValue, context.value) | ||
}; | ||
} | ||
function getDefaultKey(defaultValue, prefix) { | ||
var basePaths = formdata.getPaths(prefix !== null && prefix !== void 0 ? prefix : ''); | ||
return Object.entries(formdata.flatten(defaultValue)).reduce((result, _ref3) => { | ||
var [key, value] = _ref3; | ||
if (Array.isArray(value)) { | ||
for (var i = 0; i < value.length; i++) { | ||
result[formdata.formatPaths([...basePaths, ...formdata.getPaths(key), i])] = util.generateId(); | ||
}, {}); | ||
} | ||
function handleIntent(meta, intent, initialized) { | ||
switch (intent.type) { | ||
case 'replace': | ||
{ | ||
var _intent$payload$name; | ||
var _name = (_intent$payload$name = intent.payload.name) !== null && _intent$payload$name !== void 0 ? _intent$payload$name : ''; | ||
var value = intent.payload.value; | ||
updateValue(meta, _name, value); | ||
break; | ||
} | ||
case 'reset': | ||
{ | ||
if (typeof intent.payload.value === 'undefined' || intent.payload.value) { | ||
var _intent$payload$name2; | ||
var _name2 = (_intent$payload$name2 = intent.payload.name) !== null && _intent$payload$name2 !== void 0 ? _intent$payload$name2 : ''; | ||
var _value = formdata.getValue(meta.defaultValue, _name2); | ||
updateValue(meta, _name2, _value); | ||
} | ||
break; | ||
} | ||
return result; | ||
}, {}); | ||
case 'insert': | ||
case 'remove': | ||
case 'reorder': | ||
{ | ||
if (initialized) { | ||
meta.initialValue = util.clone(meta.initialValue); | ||
meta.key = util.clone(meta.key); | ||
submission.setListState(meta.key, intent, util.generateId); | ||
submission.setListValue(meta.initialValue, intent); | ||
} | ||
break; | ||
} | ||
} | ||
function updateValue(context, name, value) { | ||
context.initialValue = util.clone(context.initialValue); | ||
context.value = util.clone(context.value); | ||
context.key = util.clone(context.key); | ||
formdata.setValue(context.initialValue, name, () => value); | ||
formdata.setValue(context.value, name, () => value); | ||
if (formdata.isPlainObject(value) || Array.isArray(value)) { | ||
submission.setState(context.key, name, () => undefined); | ||
Object.assign(context.key, formdata.flatten(value, { | ||
resolve() { | ||
return util.generateId(); | ||
}, | ||
prefix: name | ||
})); | ||
} | ||
context.key[name] = util.generateId(); | ||
} | ||
function updateValue(meta, name, value) { | ||
meta.initialValue = util.clone(meta.initialValue); | ||
meta.value = util.clone(meta.value); | ||
meta.key = util.clone(meta.key); | ||
formdata.setValue(meta.initialValue, name, () => value); | ||
formdata.setValue(meta.value, name, () => value); | ||
if (formdata.isPlainObject(value) || Array.isArray(value)) { | ||
submission.setState(meta.key, name, () => undefined); | ||
Object.assign(meta.key, getDefaultKey(value, name)); | ||
} | ||
function handleIntents(intents, context, initialized) { | ||
for (var _intent of intents) { | ||
switch (_intent.type) { | ||
case 'replace': | ||
{ | ||
var _intent$payload$name; | ||
var name = (_intent$payload$name = _intent.payload.name) !== null && _intent$payload$name !== void 0 ? _intent$payload$name : ''; | ||
var value = _intent.payload.value; | ||
updateValue(context, name, value); | ||
break; | ||
} | ||
case 'reset': | ||
{ | ||
if (typeof _intent.payload.value === 'undefined' || _intent.payload.value) { | ||
var _intent$payload$name2; | ||
var _name = (_intent$payload$name2 = _intent.payload.name) !== null && _intent$payload$name2 !== void 0 ? _intent$payload$name2 : ''; | ||
var _value = formdata.getValue(context.defaultValue, _name); | ||
updateValue(context, _name, _value); | ||
} | ||
break; | ||
} | ||
case 'insert': | ||
case 'remove': | ||
case 'reorder': | ||
{ | ||
if (initialized) { | ||
context.initialValue = util.clone(context.initialValue); | ||
context.key = util.clone(context.key); | ||
submission.setListState(context.key, _intent, util.generateId); | ||
submission.setListValue(context.initialValue, _intent); | ||
} | ||
break; | ||
} | ||
} | ||
meta.key[name] = util.generateId(); | ||
} | ||
function createStateProxy(fn) { | ||
var cache = {}; | ||
return new Proxy(cache, { | ||
get(_, name, receiver) { | ||
var _cache$name; | ||
return (_cache$name = cache[name]) !== null && _cache$name !== void 0 ? _cache$name : cache[name] = fn(name, receiver); | ||
} | ||
} | ||
function createConstraintProxy(constraint) { | ||
var cache = {}; | ||
var proxy = new Proxy(constraint, { | ||
get(_, name) { | ||
if (typeof cache[name] === 'undefined') { | ||
var _result = constraint[name]; | ||
if (!_result) { | ||
var paths = formdata.getPaths(name); | ||
for (var i = paths.length - 1; i >= 0; i--) { | ||
var path = paths[i]; | ||
if (typeof path === 'number' && !Number.isNaN(path)) { | ||
paths[i] = Number.NaN; | ||
break; | ||
} | ||
} | ||
var alternative = formdata.formatPaths(paths); | ||
if (name !== alternative) { | ||
_result = proxy[alternative]; | ||
} | ||
} | ||
if (_result) { | ||
cache[name] = _result; | ||
} | ||
}); | ||
} | ||
function createValueProxy(value) { | ||
return createStateProxy((name, proxy) => { | ||
if (name === '') { | ||
return value; | ||
} | ||
var paths = formdata.getPaths(name); | ||
var basename = formdata.formatPaths(paths.slice(0, -1)); | ||
var key = formdata.formatPaths(paths.slice(-1)); | ||
var parentValue = proxy[basename]; | ||
return formdata.getValue(parentValue, key); | ||
}); | ||
} | ||
function createConstraintProxy(constraint) { | ||
return createStateProxy((name, proxy) => { | ||
var _result; | ||
var result = constraint[name]; | ||
if (!result) { | ||
var paths = formdata.getPaths(name); | ||
for (var i = paths.length - 1; i >= 0; i--) { | ||
var path = paths[i]; | ||
if (typeof path === 'number' && !Number.isNaN(path)) { | ||
paths[i] = Number.NaN; | ||
break; | ||
} | ||
return cache[name]; | ||
} | ||
}); | ||
return proxy; | ||
} | ||
function createKeyProxy(key) { | ||
var cache = {}; | ||
var keyProxy = new Proxy(key, { | ||
get(_, name) { | ||
if (typeof cache[name] === 'undefined') { | ||
var _key$name, _keyProxy$formatPaths; | ||
var currentKey = (_key$name = key[name]) !== null && _key$name !== void 0 ? _key$name : ''; | ||
var resultKey = name === '' ? currentKey : "".concat((_keyProxy$formatPaths = keyProxy[formdata.formatPaths(formdata.getPaths(name).slice(0, -1))]) !== null && _keyProxy$formatPaths !== void 0 ? _keyProxy$formatPaths : '', "/").concat(currentKey); | ||
if (resultKey) { | ||
cache[name] = resultKey; | ||
} | ||
} | ||
return cache[name]; | ||
var alternative = formdata.formatPaths(paths); | ||
if (name !== alternative) { | ||
result = proxy[alternative]; | ||
} | ||
}); | ||
return keyProxy; | ||
} | ||
function createValidProxy(error) { | ||
var cache = {}; | ||
return new Proxy({}, { | ||
get(_, name) { | ||
var _cache$name; | ||
return (_cache$name = cache[name]) !== null && _cache$name !== void 0 ? _cache$name : cache[name] = typeof error[name] === 'undefined'; | ||
} | ||
}); | ||
} | ||
function createDirtyProxy(defaultValue, value) { | ||
var cache = {}; | ||
return new Proxy({}, { | ||
get(_, name) { | ||
var _cache$name2; | ||
return (_cache$name2 = cache[name]) !== null && _cache$name2 !== void 0 ? _cache$name2 : cache[name] = JSON.stringify(defaultValue[name]) !== JSON.stringify(value[name]); | ||
} | ||
}); | ||
} | ||
function shouldNotify(config) { | ||
if (config.scope) { | ||
var _config$scope$prefix, _config$scope$name, _config$compareFn; | ||
var prefixes = (_config$scope$prefix = config.scope.prefix) !== null && _config$scope$prefix !== void 0 ? _config$scope$prefix : []; | ||
var names = (_config$scope$name = config.scope.name) !== null && _config$scope$name !== void 0 ? _config$scope$name : []; | ||
var compareFn = (_config$compareFn = config.compareFn) !== null && _config$compareFn !== void 0 ? _config$compareFn : (prev, next) => JSON.stringify(prev) !== JSON.stringify(next); | ||
var list = prefixes.length === 0 ? names : Array.from(new Set([...Object.keys(config.prev), ...Object.keys(config.next)])); | ||
var _loop = function _loop(name) { | ||
if (prefixes.length === 0 || names.includes(name) || prefixes.some(prefix => formdata.isPrefix(name, prefix))) { | ||
var _config$cache, _config$cache$name; | ||
(_config$cache$name = (_config$cache = config.cache)[name]) !== null && _config$cache$name !== void 0 ? _config$cache$name : _config$cache[name] = compareFn(config.prev[name], config.next[name]); | ||
if (config.cache[name]) { | ||
} | ||
return (_result = result) !== null && _result !== void 0 ? _result : {}; | ||
}); | ||
} | ||
function createKeyProxy(key) { | ||
return createStateProxy((name, proxy) => { | ||
var _key$name, _proxy$formatPaths; | ||
var currentKey = (_key$name = key[name]) !== null && _key$name !== void 0 ? _key$name : ''; | ||
var resultKey = name === '' ? currentKey : "".concat((_proxy$formatPaths = proxy[formdata.formatPaths(formdata.getPaths(name).slice(0, -1))]) !== null && _proxy$formatPaths !== void 0 ? _proxy$formatPaths : '', "/").concat(currentKey); | ||
return resultKey; | ||
}); | ||
} | ||
function createValidProxy(error) { | ||
return createStateProxy(name => typeof error[name] === 'undefined'); | ||
} | ||
function createDirtyProxy(defaultValue, value) { | ||
return createStateProxy(name => JSON.stringify(defaultValue[name]) !== JSON.stringify(value[name])); | ||
} | ||
function shouldNotify(config) { | ||
if (config.scope) { | ||
var _config$scope$prefix, _config$scope$name, _config$compareFn; | ||
var prefixes = (_config$scope$prefix = config.scope.prefix) !== null && _config$scope$prefix !== void 0 ? _config$scope$prefix : []; | ||
var names = (_config$scope$name = config.scope.name) !== null && _config$scope$name !== void 0 ? _config$scope$name : []; | ||
var compareFn = (_config$compareFn = config.compareFn) !== null && _config$compareFn !== void 0 ? _config$compareFn : (prev, next) => JSON.stringify(prev) !== JSON.stringify(next); | ||
var list = prefixes.length === 0 ? names : Array.from(new Set([...Object.keys(config.prev), ...Object.keys(config.next)])); | ||
var _loop = function _loop(_name3) { | ||
if (prefixes.length === 0 || names.includes(_name3) || prefixes.some(prefix => formdata.isPrefix(_name3, prefix))) { | ||
var _config$cache, _config$cache$_name; | ||
(_config$cache$_name = (_config$cache = config.cache)[_name3]) !== null && _config$cache$_name !== void 0 ? _config$cache$_name : _config$cache[_name3] = compareFn(config.prev[_name3], config.next[_name3]); | ||
if (config.cache[_name3]) { | ||
return { | ||
@@ -228,36 +168,48 @@ v: true | ||
} | ||
}; | ||
for (var name of list) { | ||
var _ret = _loop(name); | ||
if (typeof _ret === "object") return _ret.v; | ||
} | ||
}, | ||
_ret; | ||
for (var _name3 of list) { | ||
_ret = _loop(_name3); | ||
if (_ret) return _ret.v; | ||
} | ||
return false; | ||
} | ||
function updateContext(next) { | ||
var prev = context; | ||
return false; | ||
} | ||
function createFormState(next) { | ||
var prev = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : next; | ||
var state = arguments.length > 2 ? arguments[2] : undefined; | ||
var defaultValue = !state || prev.defaultValue !== next.defaultValue ? createValueProxy(next.defaultValue) : state.defaultValue; | ||
var initialValue = next.initialValue === next.defaultValue ? defaultValue : !state || prev.initialValue !== next.initialValue ? createValueProxy(next.initialValue) : state.initialValue; | ||
var value = next.value === next.initialValue ? initialValue : !state || prev.value !== next.value ? createValueProxy(next.value) : state.value; | ||
return { | ||
submissionStatus: next.submissionStatus, | ||
defaultValue, | ||
initialValue, | ||
value, | ||
error: !state || prev.error !== next.error ? next.error : state.error, | ||
validated: next.validated, | ||
constraint: !state || prev.constraint !== next.constraint ? createConstraintProxy(next.constraint) : state.constraint, | ||
key: !state || prev.key !== next.key ? createKeyProxy(next.key) : state.key, | ||
valid: !state || prev.error !== next.error ? createValidProxy(next.error) : state.valid, | ||
dirty: !state || prev.defaultValue !== next.defaultValue || prev.value !== next.value ? createDirtyProxy(next.defaultValue, next.value) : state.dirty | ||
}; | ||
} | ||
function createFormContext(formId, options) { | ||
var subscribers = []; | ||
var latestOptions = options; | ||
var meta = createFormMeta(options); | ||
var state = createFormState(meta); | ||
function getFormElement() { | ||
var element = document.forms.namedItem(formId); | ||
util.invariant(element !== null, "Form#".concat(formId, " does not exist")); | ||
return element; | ||
} | ||
function updateFormMeta(nextMeta) { | ||
var prevMeta = meta; | ||
var prevState = state; | ||
var nextState = createFormState(nextMeta, prevMeta, prevState); | ||
// Apply change before updating state | ||
context = next; | ||
var defaultValue = prev.defaultValue !== next.defaultValue ? formdata.flatten(next.defaultValue) : state.defaultValue; | ||
var initialValue = next.initialValue === next.defaultValue ? defaultValue : prev.initialValue !== next.initialValue ? formdata.flatten(next.initialValue) : state.initialValue; | ||
var value = next.value === next.initialValue ? initialValue : prev.value !== next.value ? formdata.flatten(next.value) : state.value; | ||
updateFormState({ | ||
submissionStatus: next.submissionStatus, | ||
defaultValue, | ||
initialValue, | ||
value, | ||
error: prev.error !== next.error ? next.error : state.error, | ||
constraint: prev.constraint !== next.constraint ? createConstraintProxy(next.constraint) : state.constraint, | ||
key: prev.key !== next.key ? createKeyProxy(next.key) : state.key, | ||
valid: prev.error !== next.error ? createValidProxy(next.error) : state.valid, | ||
dirty: prev.defaultValue !== next.defaultValue || prev.value !== next.value ? createDirtyProxy(next.defaultValue, next.value) : state.dirty | ||
}); | ||
} | ||
function compareBoolean() { | ||
var prev = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; | ||
var next = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
return prev !== next; | ||
} | ||
function updateFormState(next) { | ||
// Apply change before notifying subscribers | ||
meta = nextMeta; | ||
state = nextState; | ||
var cache = { | ||
@@ -271,22 +223,18 @@ value: {}, | ||
}; | ||
var prev = state; | ||
// Apply change before notifying subscribers | ||
state = next; | ||
for (var subscriber of subscribers) { | ||
var _subscriber$getSubjec; | ||
var subject = (_subscriber$getSubjec = subscriber.getSubject) === null || _subscriber$getSubjec === void 0 ? void 0 : _subscriber$getSubjec.call(subscriber); | ||
if (!subject || subject.status && prev.submissionStatus !== next.submissionStatus || shouldNotify({ | ||
prev: prev.error, | ||
next: next.error, | ||
if (!subject || subject.status && prevState.submissionStatus !== nextState.submissionStatus || shouldNotify({ | ||
prev: prevState.error, | ||
next: nextState.error, | ||
cache: cache.error, | ||
scope: subject.error | ||
}) || shouldNotify({ | ||
prev: prev.initialValue, | ||
next: next.initialValue, | ||
prev: prevState.initialValue, | ||
next: nextState.initialValue, | ||
cache: cache.initialValue, | ||
scope: subject.initialValue | ||
}) || shouldNotify({ | ||
prev: prev.key, | ||
next: next.key, | ||
prev: prevState.key, | ||
next: nextState.key, | ||
compareFn: (prev, next) => prev !== next, | ||
@@ -296,4 +244,4 @@ cache: cache.key, | ||
}) || shouldNotify({ | ||
prev: prev.valid, | ||
next: next.valid, | ||
prev: prevState.valid, | ||
next: nextState.valid, | ||
compareFn: compareBoolean, | ||
@@ -303,4 +251,4 @@ cache: cache.valid, | ||
}) || shouldNotify({ | ||
prev: prev.dirty, | ||
next: next.dirty, | ||
prev: prevState.dirty, | ||
next: nextState.dirty, | ||
compareFn: compareBoolean, | ||
@@ -310,4 +258,4 @@ cache: cache.dirty, | ||
}) || shouldNotify({ | ||
prev: prev.value, | ||
next: next.value, | ||
prev: prevState.value, | ||
next: nextState.value, | ||
cache: cache.value, | ||
@@ -320,2 +268,7 @@ scope: subject.value | ||
} | ||
function compareBoolean() { | ||
var prev = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; | ||
var next = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
return prev !== next; | ||
} | ||
function getStateInput(form) { | ||
@@ -336,3 +289,3 @@ var element = form.elements.namedItem(submission.STATE); | ||
return JSON.stringify({ | ||
validated: context.validated | ||
validated: meta.validated | ||
}); | ||
@@ -358,3 +311,3 @@ } | ||
var _latestOptions$onSubm, _latestOptions2; | ||
(_latestOptions$onSubm = (_latestOptions2 = latestOptions).onSubmit) === null || _latestOptions$onSubm === void 0 ? void 0 : _latestOptions$onSubm.call(_latestOptions2, event, context); | ||
(_latestOptions$onSubm = (_latestOptions2 = latestOptions).onSubmit) === null || _latestOptions$onSubm === void 0 || _latestOptions$onSubm.call(_latestOptions2, event, context); | ||
} else { | ||
@@ -372,3 +325,3 @@ try { | ||
} | ||
(_latestOptions$onSubm2 = (_latestOptions3 = latestOptions).onSubmit) === null || _latestOptions$onSubm2 === void 0 ? void 0 : _latestOptions$onSubm2.call(_latestOptions3, event, _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, { | ||
(_latestOptions$onSubm2 = (_latestOptions3 = latestOptions).onSubmit) === null || _latestOptions$onSubm2 === void 0 || _latestOptions$onSubm2.call(_latestOptions3, event, _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, { | ||
submission | ||
@@ -395,3 +348,3 @@ })); | ||
} = latestOptions; | ||
var validated = context.validated[element.name]; | ||
var validated = meta.validated[element.name]; | ||
return validated ? shouldRevalidate === eventName : shouldValidate === eventName; | ||
@@ -407,8 +360,7 @@ } | ||
var _result2 = submission.getSubmissionContext(formData); | ||
var value = formdata.flatten(_result2.payload); | ||
updateContext(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, { | ||
value | ||
updateFormMeta(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, meta), {}, { | ||
value: _result2.payload | ||
})); | ||
} else { | ||
submission.requestIntent(formId, [submission.intent.validate(element.name)]); | ||
submission.requestIntent(formId, submission.intent.validate(element.name)); | ||
} | ||
@@ -421,6 +373,5 @@ } | ||
} | ||
submission.requestIntent(formId, [submission.intent.validate(element.name)]); | ||
submission.requestIntent(formId, submission.intent.validate(element.name)); | ||
} | ||
function reset(event) { | ||
var _serialize2, _latestOptions$constr; | ||
var element = getFormElement(); | ||
@@ -430,14 +381,3 @@ if (event.type !== 'reset' || event.target !== element || event.defaultPrevented) { | ||
} | ||
var defaultValue = (_serialize2 = serialize(latestOptions.defaultValue)) !== null && _serialize2 !== void 0 ? _serialize2 : {}; | ||
updateContext({ | ||
key: _rollupPluginBabelHelpers.objectSpread2({ | ||
'': util.generateId() | ||
}, getDefaultKey(defaultValue)), | ||
defaultValue, | ||
initialValue: defaultValue, | ||
value: defaultValue, | ||
error: {}, | ||
validated: {}, | ||
constraint: (_latestOptions$constr = latestOptions.constraint) !== null && _latestOptions$constr !== void 0 ? _latestOptions$constr : {} | ||
}); | ||
updateFormMeta(createFormMeta(latestOptions, true)); | ||
} | ||
@@ -451,6 +391,5 @@ function report(result) { | ||
} | ||
var value = formdata.flatten(result.initialValue); | ||
var error = Object.entries((_result$error = result.error) !== null && _result$error !== void 0 ? _result$error : {}).reduce((result, _ref4) => { | ||
var [name, messages] = _ref4; | ||
var error = messages === null ? context.error[name] : messages; | ||
var error = Object.entries((_result$error = result.error) !== null && _result$error !== void 0 ? _result$error : {}).reduce((result, _ref3) => { | ||
var [name, newError] = _ref3; | ||
var error = newError === null ? meta.error[name] : newError; | ||
if (error) { | ||
@@ -461,12 +400,12 @@ result[name] = error; | ||
}, {}); | ||
var update = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, { | ||
var update = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, meta), {}, { | ||
submissionStatus: result.status, | ||
value, | ||
value: result.initialValue, | ||
error, | ||
validated: (_result$state$validat = (_result$state = result.state) === null || _result$state === void 0 ? void 0 : _result$state.validated) !== null && _result$state$validat !== void 0 ? _result$state$validat : {} | ||
}); | ||
if (result.intents) { | ||
handleIntents(result.intents, update, true); | ||
if (result.intent) { | ||
handleIntent(update, result.intent, true); | ||
} | ||
updateContext(update); | ||
updateFormMeta(update); | ||
@@ -476,3 +415,3 @@ // TODO: An option to configure the validationMessage | ||
if (dom.isFieldElement(element) && element.name !== '') { | ||
element.setCustomValidity(context.error[element.name] ? 'Invalid' : ''); | ||
element.setCustomValidity(error[element.name] ? 'Invalid' : ''); | ||
} | ||
@@ -506,3 +445,3 @@ } | ||
return { | ||
id: formId, | ||
formId, | ||
submit, | ||
@@ -520,2 +459,2 @@ reset, | ||
exports.createForm = createForm; | ||
exports.createFormContext = createFormContext; |
@@ -36,3 +36,3 @@ /** | ||
*/ | ||
export declare function getValue(target: Record<string, any>, name: string): unknown; | ||
export declare function getValue(target: unknown, name: string): unknown; | ||
/** | ||
@@ -39,0 +39,0 @@ * Check if the value is a plain object |
@@ -105,3 +105,9 @@ 'use strict'; | ||
} | ||
pointer = pointer[path]; | ||
if (isPlainObject(pointer) && typeof path === 'string') { | ||
pointer = pointer[path]; | ||
} else if (Array.isArray(pointer) && typeof path === 'number') { | ||
pointer = pointer[path]; | ||
} else { | ||
return; | ||
} | ||
} | ||
@@ -108,0 +114,0 @@ return pointer; |
@@ -1,5 +0,5 @@ | ||
export { type UnionKeyof, type UnionKeyType, type Constraint, type FormState, type FormId, type FieldName, type FormValue, type FormContext, type FormOptions, type Form, type SubscriptionSubject, type SubscriptionScope, createForm, } from './form'; | ||
export { type UnionKeyof, type UnionKeyType, type Constraint, type FormId, type FieldName, type DefaultValue, type FormValue, type FormOptions, type FormState, type FormContext, type SubscriptionSubject, type SubscriptionScope, createFormContext, } from './form'; | ||
export { type FieldElement, isFieldElement, requestSubmit } from './dom'; | ||
export { invariant } from './util'; | ||
export { type Submission, type SubmissionResult, type Intent, INTENT, STATE, intent, serializeIntents, requestIntent, parse, } from './submission'; | ||
export { type Submission, type SubmissionResult, type Intent, INTENT, STATE, intent, serializeIntent as serializeIntent, requestIntent, parse, } from './submission'; | ||
export { getPaths, formatPaths, isPrefix } from './formdata'; |
@@ -13,3 +13,3 @@ 'use strict'; | ||
exports.createForm = form.createForm; | ||
exports.createFormContext = form.createFormContext; | ||
exports.isFieldElement = dom.isFieldElement; | ||
@@ -23,5 +23,5 @@ exports.requestSubmit = dom.requestSubmit; | ||
exports.requestIntent = submission.requestIntent; | ||
exports.serializeIntents = submission.serializeIntents; | ||
exports.serializeIntent = submission.serializeIntent; | ||
exports.formatPaths = formdata.formatPaths; | ||
exports.getPaths = formdata.getPaths; | ||
exports.isPrefix = formdata.isPrefix; |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.0-pre.2", | ||
"version": "1.0.0-pre.3", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "module": "index.mjs", |
@@ -1,2 +0,2 @@ | ||
import type { DefaultValue, FieldName } from './form'; | ||
import type { DefaultValue, FieldName, FormValue } from './form'; | ||
export type SubmissionState = { | ||
@@ -6,3 +6,3 @@ validated: Record<string, boolean>; | ||
export type SubmissionContext<Value = null, Error = unknown> = { | ||
intents: Array<Intent> | null; | ||
intent: Intent | null; | ||
payload: Record<string, unknown>; | ||
@@ -31,3 +31,3 @@ fields: string[]; | ||
status?: 'error' | 'success'; | ||
intents?: Array<Intent>; | ||
intent?: Intent; | ||
initialValue?: Record<string, unknown>; | ||
@@ -57,3 +57,3 @@ error?: Record<string, Error | null>; | ||
export declare function parse<Value, Error>(payload: FormData | URLSearchParams, options: { | ||
resolve: (payload: Record<string, any>, intents: Array<Intent> | null) => { | ||
resolve: (payload: Record<string, any>, intent: Intent | null) => { | ||
value?: Value; | ||
@@ -64,3 +64,3 @@ error?: Record<string, Error | null> | null; | ||
export declare function parse<Value, Error>(payload: FormData | URLSearchParams, options: { | ||
resolve: (payload: Record<string, any>, intents: Array<Intent> | null) => Promise<{ | ||
resolve: (payload: Record<string, any>, intent: Intent | null) => Promise<{ | ||
value?: Value; | ||
@@ -71,3 +71,3 @@ error?: Record<string, Error | null> | null; | ||
export declare function parse<Value, Error>(payload: FormData | URLSearchParams, options: { | ||
resolve: (payload: Record<string, any>, intents: Array<Intent> | null) => { | ||
resolve: (payload: Record<string, any>, intent: Intent | null) => { | ||
value?: Value; | ||
@@ -115,3 +115,3 @@ error?: Record<string, Error | null> | null; | ||
name: FieldName<Schema>; | ||
defaultValue?: Schema extends Array<infer Item> ? Item : never; | ||
defaultValue?: Schema extends Array<infer Item> ? DefaultValue<Item> : never; | ||
index?: number; | ||
@@ -129,9 +129,10 @@ }; | ||
export type Intent<Schema = unknown> = ValidateIntent<Schema> | ResetIntent<Schema> | ReplaceIntent<Schema> | ReorderIntent<Schema extends Array<any> ? Schema : any> | RemoveIntent<Schema extends Array<any> ? Schema : any> | InsertIntent<Schema extends Array<any> ? Schema : any>; | ||
export declare function getIntents(intent: string | null | undefined): Array<Intent> | null; | ||
export declare function serializeIntents(intents: Array<Intent>): string; | ||
export declare function requestIntent(formId: string, intents: Array<Intent>): void; | ||
export declare function getIntent(serializedIntent: string | null | undefined): Intent | null; | ||
export declare function serializeIntent(intent: Intent): string; | ||
export declare function requestIntent(formId: string, intent: Intent): void; | ||
export declare function updateList(list: unknown, intent: InsertIntent | RemoveIntent | ReorderIntent): void; | ||
export declare function setListValue(data: Record<string, unknown>, intent: InsertIntent | RemoveIntent | ReorderIntent): void; | ||
export declare function setState(state: Record<string, unknown>, name: string, valueFn: (value: unknown) => unknown): void; | ||
export declare function setListState(state: Record<string, unknown>, intent: InsertIntent | RemoveIntent | ReorderIntent, getDefaultValue?: () => unknown): void; | ||
export declare function setListState(state: Record<string, unknown>, intent: InsertIntent | RemoveIntent | ReorderIntent, getDefaultValue?: () => string): void; | ||
export declare function serialize<Schema>(defaultValue: DefaultValue<Schema>): FormValue<Schema>; | ||
export declare const intent: { | ||
@@ -160,5 +161,5 @@ reset: <Schema>(payload: { | ||
name: FieldName<Schema_5 extends any[] ? Schema_5 : any>; | ||
defaultValue?: ((Schema_5 extends any[] ? Schema_5 : any) extends infer T ? T extends (Schema_5 extends any[] ? Schema_5 : any) ? T extends (infer Item)[] ? Item : never : never : never) | undefined; | ||
defaultValue?: ((Schema_5 extends any[] ? Schema_5 : any) extends infer T ? T extends (Schema_5 extends any[] ? Schema_5 : any) ? T extends (infer Item)[] ? DefaultValue<Item> : never : never : never) | undefined; | ||
index?: number | undefined; | ||
}) => InsertIntent<Schema_5 extends any[] ? Schema_5 : any>; | ||
}; |
@@ -27,3 +27,3 @@ 'use strict'; | ||
if (name === INTENT || name === STATE) { | ||
return "continue"; | ||
return 1; // continue | ||
} | ||
@@ -42,8 +42,7 @@ fields.push(name); | ||
for (var [name, next] of body.entries()) { | ||
var _ret = _loop(next); | ||
if (_ret === "continue") continue; | ||
if (_loop(next)) continue; | ||
} | ||
return { | ||
payload, | ||
intents: getIntents(intent), | ||
intent: getIntent(intent), | ||
state: state ? JSON.parse(state) : { | ||
@@ -59,87 +58,83 @@ validated: {} | ||
var context = getSubmissionContext(payload); | ||
if (context.intents) { | ||
var _loop2 = function _loop2() { | ||
switch (_intent.type) { | ||
case 'validate': | ||
context.state.validated[_intent.payload] = true; | ||
var intent = context.intent; | ||
if (intent) { | ||
switch (intent.type) { | ||
case 'validate': | ||
context.state.validated[intent.payload] = true; | ||
break; | ||
case 'replace': | ||
{ | ||
var { | ||
name, | ||
value: _value, | ||
validated | ||
} = intent.payload; | ||
if (name) { | ||
formdata.setValue(context.payload, name, () => _value); | ||
} else { | ||
// @ts-expect-error FIXME - it must be an object if there is no name | ||
context.payload = _value; | ||
} | ||
if (validated) { | ||
if (formdata.isPlainObject(_value) || Array.isArray(_value)) { | ||
// Clean up previous validated state | ||
setState(context.state.validated, name, () => undefined); | ||
Object.assign(context.state.validated, formdata.flatten(_value, { | ||
resolve() { | ||
return true; | ||
}, | ||
prefix: name | ||
})); | ||
} | ||
context.state.validated[name] = true; | ||
} else { | ||
if (formdata.isPlainObject(_value) || Array.isArray(_value)) { | ||
setState(context.state.validated, name, () => undefined); | ||
} | ||
delete context.state.validated[name]; | ||
} | ||
break; | ||
case 'replace': | ||
{ | ||
var { | ||
name, | ||
value: _value, | ||
validated | ||
} = _intent.payload; | ||
if (name) { | ||
formdata.setValue(context.payload, name, () => _value); | ||
} | ||
case 'reset': | ||
{ | ||
var { | ||
name: _name, | ||
value: _value2, | ||
validated: _validated | ||
} = intent.payload; | ||
if (typeof _value2 === 'undefined' || _value2) { | ||
if (_name) { | ||
formdata.setValue(context.payload, _name, () => undefined); | ||
} else { | ||
// @ts-expect-error FIXME - it must be an object if there is no name | ||
context.payload = _value; | ||
context.payload = {}; | ||
} | ||
if (validated) { | ||
if (formdata.isPlainObject(_value) || Array.isArray(_value)) { | ||
// Clean up previous validated state | ||
setState(context.state.validated, name, () => undefined); | ||
Object.assign(context.state.validated, formdata.flatten(_value, { | ||
resolve() { | ||
return true; | ||
}, | ||
prefix: name | ||
})); | ||
} | ||
context.state.validated[name] = true; | ||
} | ||
if (typeof _validated === 'undefined' || _validated) { | ||
if (_name) { | ||
setState(context.state.validated, _name, () => undefined); | ||
delete context.state.validated[_name]; | ||
} else { | ||
if (formdata.isPlainObject(_value) || Array.isArray(_value)) { | ||
setState(context.state.validated, name, () => undefined); | ||
} | ||
delete context.state.validated[name]; | ||
context.state.validated = {}; | ||
} | ||
break; | ||
} | ||
case 'reset': | ||
{ | ||
var { | ||
name: _name, | ||
value: _value2, | ||
validated: _validated | ||
} = _intent.payload; | ||
if (typeof _value2 === 'undefined' || _value2) { | ||
if (_name) { | ||
formdata.setValue(context.payload, _name, () => undefined); | ||
} else { | ||
context.payload = {}; | ||
} | ||
} | ||
if (typeof _validated === 'undefined' || _validated) { | ||
if (_name) { | ||
setState(context.state.validated, _name, () => undefined); | ||
delete context.state.validated[_name]; | ||
} else { | ||
context.state.validated = {}; | ||
} | ||
} | ||
break; | ||
} | ||
case 'insert': | ||
case 'remove': | ||
case 'reorder': | ||
{ | ||
setListValue(context.payload, _intent); | ||
setListState(context.state.validated, _intent); | ||
context.state.validated[_intent.payload.name] = true; | ||
break; | ||
} | ||
} | ||
}; | ||
for (var _intent of context.intents) { | ||
_loop2(); | ||
break; | ||
} | ||
case 'insert': | ||
case 'remove': | ||
case 'reorder': | ||
{ | ||
setListValue(context.payload, intent); | ||
setListState(context.state.validated, intent); | ||
context.state.validated[intent.payload.name] = true; | ||
break; | ||
} | ||
} | ||
} | ||
var result = options.resolve(context.payload, context.intents); | ||
var result = options.resolve(context.payload, intent); | ||
var mergeResolveResult = resolved => { | ||
var _resolved$value; | ||
var error = typeof resolved.error !== 'undefined' ? resolved.error : {}; | ||
if (!context.intents) { | ||
for (var name of [...context.fields, ...Object.keys(error !== null && error !== void 0 ? error : {})]) { | ||
context.state.validated[name] = true; | ||
if (!intent) { | ||
for (var _name2 of [...context.fields, ...Object.keys(error !== null && error !== void 0 ? error : {})]) { | ||
context.state.validated[_name2] = true; | ||
} | ||
@@ -158,4 +153,3 @@ } | ||
function createSubmission(context) { | ||
if (context.intents) { | ||
var _context$error; | ||
if (context.intent) { | ||
return { | ||
@@ -165,3 +159,3 @@ type: 'update', | ||
value: null, | ||
error: (_context$error = context.error) !== null && _context$error !== void 0 ? _context$error : {}, | ||
error: context.error, | ||
accept(options) { | ||
@@ -216,4 +210,4 @@ return acceptSubmission(context, options); | ||
function rejectSubmission(context, options) { | ||
var _context$error2, _simplify2; | ||
var error = Object.entries((_context$error2 = context.error) !== null && _context$error2 !== void 0 ? _context$error2 : {}).reduce((result, _ref) => { | ||
var _context$error, _simplify2; | ||
var error = Object.entries((_context$error = context.error) !== null && _context$error !== void 0 ? _context$error : {}).reduce((result, _ref) => { | ||
var [name, currentError] = _ref; | ||
@@ -231,4 +225,4 @@ if (context.state.validated[name]) { | ||
return { | ||
status: context.intents !== null ? undefined : 'error', | ||
intents: context.intents !== null ? context.intents : undefined, | ||
status: context.intent !== null ? undefined : 'error', | ||
intent: context.intent !== null ? context.intent : undefined, | ||
initialValue: (_simplify2 = formdata.simplify(context.payload)) !== null && _simplify2 !== void 0 ? _simplify2 : {}, | ||
@@ -239,25 +233,25 @@ error: formdata.simplify(error), | ||
} | ||
function getIntents(intent) { | ||
if (!intent) { | ||
function getIntent(serializedIntent) { | ||
if (!serializedIntent) { | ||
return null; | ||
} | ||
var intents = JSON.parse(intent); | ||
if (!Array.isArray(intents) || intents.length === 0 || !intents.every(intent => typeof intent.type === 'string' && typeof intent.payload !== 'undefined')) { | ||
var intent = JSON.parse(serializedIntent); | ||
if (typeof intent.type !== 'string' || typeof intent.payload === 'undefined') { | ||
throw new Error('Unknown intent'); | ||
} | ||
return intents; | ||
return intent; | ||
} | ||
function serializeIntents(intents) { | ||
return JSON.stringify(intents); | ||
function serializeIntent(intent) { | ||
return JSON.stringify(intent); | ||
} | ||
function requestIntent(formId, intents) { | ||
function requestIntent(formId, intent) { | ||
var form = document.forms.namedItem(formId); | ||
var submitter = document.createElement('button'); | ||
submitter.name = INTENT; | ||
submitter.value = serializeIntents(intents); | ||
submitter.value = serializeIntent(intent); | ||
submitter.hidden = true; | ||
submitter.formNoValidate = true; | ||
form === null || form === void 0 ? void 0 : form.appendChild(submitter); | ||
form === null || form === void 0 || form.appendChild(submitter); | ||
dom.requestSubmit(form, submitter); | ||
form === null || form === void 0 ? void 0 : form.removeChild(submitter); | ||
form === null || form === void 0 || form.removeChild(submitter); | ||
} | ||
@@ -269,3 +263,3 @@ function updateList(list, intent) { | ||
case 'insert': | ||
list.splice((_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : list.length, 0, intent.payload.defaultValue); | ||
list.splice((_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : list.length, 0, serialize(intent.payload.defaultValue)); | ||
break; | ||
@@ -295,3 +289,3 @@ case 'remove': | ||
var target = {}; | ||
var _loop3 = function _loop3() { | ||
var _loop2 = function _loop2() { | ||
var value = state[key]; | ||
@@ -317,10 +311,8 @@ if (formdata.isPrefix(key, name) && key !== name) { | ||
for (var key of keys) { | ||
_loop3(); | ||
_loop2(); | ||
} | ||
var result; | ||
formdata.setValue(target, name, currentValue => { | ||
result = valueFn(currentValue); | ||
return result; | ||
}); | ||
Object.assign(state, formdata.flatten(result, { | ||
var result = valueFn(formdata.getValue(target, name)); | ||
Object.assign(state, | ||
// @ts-expect-error FIXME flatten should be more flexible | ||
formdata.flatten(result, { | ||
resolve(data) { | ||
@@ -356,2 +348,30 @@ if (formdata.isPlainObject(data) || Array.isArray(data)) { | ||
} | ||
function serialize(defaultValue) { | ||
if (formdata.isPlainObject(defaultValue)) { | ||
// @ts-expect-error FIXME | ||
return Object.entries(defaultValue).reduce((result, _ref2) => { | ||
var [key, value] = _ref2; | ||
// @ts-ignore-error FIXME | ||
result[key] = serialize(value); | ||
return result; | ||
}, {}); | ||
} else if (Array.isArray(defaultValue)) { | ||
// @ts-expect-error FIXME | ||
return defaultValue.map(serialize); | ||
} else if ( | ||
// @ts-ignore-error FIXME | ||
defaultValue instanceof Date) { | ||
// @ts-expect-error FIXME | ||
return defaultValue.toISOString(); | ||
} else if (typeof defaultValue === 'boolean') { | ||
// @ts-expect-error FIXME | ||
return defaultValue ? 'on' : undefined; | ||
} else if (typeof defaultValue === 'number') { | ||
// @ts-expect-error FIXME | ||
return defaultValue.toString(); | ||
} else { | ||
// @ts-expect-error FIXME | ||
return defaultValue !== null && defaultValue !== void 0 ? defaultValue : undefined; | ||
} | ||
} | ||
var intent = new Proxy({}, { | ||
@@ -370,3 +390,3 @@ get(_, type) { | ||
exports.createSubmission = createSubmission; | ||
exports.getIntents = getIntents; | ||
exports.getIntent = getIntent; | ||
exports.getSubmissionContext = getSubmissionContext; | ||
@@ -378,3 +398,4 @@ exports.hideFields = hideFields; | ||
exports.requestIntent = requestIntent; | ||
exports.serializeIntents = serializeIntents; | ||
exports.serialize = serialize; | ||
exports.serializeIntent = serializeIntent; | ||
exports.setListState = setListState; | ||
@@ -381,0 +402,0 @@ exports.setListValue = setListValue; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102361
2738