@conform-to/dom
Advanced tools
Comparing version 1.0.0-pre.4 to 1.0.0-pre.5
21
dom.js
@@ -87,12 +87,13 @@ 'use strict'; | ||
util.invariant(!!form, 'Failed to submit the form. The element provided is null or undefined.'); | ||
if (typeof form.requestSubmit === 'function') { | ||
form.requestSubmit(submitter); | ||
} else { | ||
var event = new SubmitEvent('submit', { | ||
bubbles: true, | ||
cancelable: true, | ||
submitter | ||
}); | ||
form.dispatchEvent(event); | ||
} | ||
// if (typeof form.requestSubmit === 'function') { | ||
// form.requestSubmit(submitter); | ||
// } else { | ||
var event = new SubmitEvent('submit', { | ||
bubbles: true, | ||
cancelable: true, | ||
submitter | ||
}); | ||
form.dispatchEvent(event); | ||
// } | ||
} | ||
@@ -99,0 +100,0 @@ |
import { getFormAction, getFormEncType, getFormMethod } from './dom'; | ||
import { type Submission, type SubmissionResult } from './submission'; | ||
import { type FormControl, type Submission, type SubmissionResult } from './submission'; | ||
export type UnionKeyof<T> = T extends any ? keyof T : never; | ||
@@ -13,8 +13,10 @@ export type UnionKeyType<T, K extends UnionKeyof<T>> = T extends { | ||
} | undefined : unknown; | ||
export type FormId<Schema extends Record<string, unknown>, Error> = string & { | ||
export type FormId<Schema extends Record<string, unknown> = Record<string, unknown>, Error = string[]> = string & { | ||
__error?: Error; | ||
__schema?: Schema; | ||
}; | ||
export type FieldName<Schema> = string & { | ||
__schema?: Schema; | ||
export type FieldName<FieldSchema, Error = string[], FormSchema extends Record<string, unknown> = Record<string, unknown>> = string & { | ||
__field?: FieldSchema; | ||
__error?: Error; | ||
__form?: FormSchema; | ||
}; | ||
@@ -31,3 +33,3 @@ export type Constraint = { | ||
}; | ||
export type FormMeta<Error> = { | ||
export type FormMeta<FormError> = { | ||
submissionStatus?: 'error' | 'success'; | ||
@@ -37,3 +39,3 @@ defaultValue: Record<string, unknown>; | ||
value: Record<string, unknown>; | ||
error: Record<string, Error>; | ||
error: Record<string, FormError>; | ||
constraint: Record<string, Constraint>; | ||
@@ -43,8 +45,12 @@ key: Record<string, string | undefined>; | ||
}; | ||
export type FormState<Error> = FormMeta<Error> & { | ||
export type FormState<FormError> = FormMeta<FormError> & { | ||
valid: Record<string, boolean>; | ||
dirty: Record<string, boolean>; | ||
}; | ||
export type FormOptions<Schema, Error, Value = Schema> = { | ||
export type FormOptions<Schema, FormError, Value = Schema> = { | ||
/** | ||
* The id of the form. | ||
*/ | ||
formId: string; | ||
/** | ||
* An object representing the initial value of the form. | ||
@@ -60,3 +66,3 @@ */ | ||
*/ | ||
lastResult?: SubmissionResult<Error>; | ||
lastResult?: SubmissionResult<FormError>; | ||
/** | ||
@@ -83,3 +89,3 @@ * Define when conform should start validation. | ||
formData: FormData; | ||
}) => Submission<Schema, Error, Value>; | ||
}) => Submission<Schema, FormError, Value>; | ||
/** | ||
@@ -93,3 +99,3 @@ * A function to be called before the form is submitted. | ||
method: ReturnType<typeof getFormMethod>; | ||
submission?: Submission<Schema, Error, Value>; | ||
submission?: Submission<Schema, FormError, Value>; | ||
}) => void; | ||
@@ -106,3 +112,9 @@ }; | ||
}; | ||
export type FormContext<Schema extends Record<string, any> = any, Error = string[], Value = Schema> = { | ||
export type ControlButtonProps = { | ||
name: string; | ||
value: string; | ||
form: string; | ||
formNoValidate: boolean; | ||
}; | ||
export type FormContext<Schema extends Record<string, any> = any, FormError = string[], Value = Schema> = { | ||
formId: string; | ||
@@ -113,8 +125,10 @@ submit(event: SubmitEvent): void; | ||
blur(event: Event): void; | ||
report(result: SubmissionResult<Error>): void; | ||
update(options: Omit<FormOptions<Schema, Error, Value>, 'lastResult'>): void; | ||
report(result: SubmissionResult<FormError>): void; | ||
update(options: Omit<FormOptions<Schema, FormError, Value>, 'lastResult'>): void; | ||
subscribe(callback: () => void, getSubject?: () => SubscriptionSubject | undefined): () => void; | ||
getState(): FormState<Error>; | ||
dispatch(control: FormControl): void; | ||
getControlButtonProps(control: FormControl): ControlButtonProps; | ||
getState(): FormState<FormError>; | ||
getSerializedState(): string; | ||
}; | ||
export declare function createFormContext<Schema extends Record<string, any>, Error, Value>(formId: string, options: FormOptions<Schema, Error, Value>): FormContext<Schema, Error, Value>; | ||
export declare function createFormContext<Schema extends Record<string, any>, FormError, Value>(options: FormOptions<Schema, FormError, Value>): FormContext<Schema, FormError, Value>; |
176
form.js
@@ -30,4 +30,4 @@ 'use strict'; | ||
}; | ||
if (lastResult !== null && lastResult !== void 0 && lastResult.intent) { | ||
handleIntent(result, lastResult.intent); | ||
if (lastResult !== null && lastResult !== void 0 && lastResult.control) { | ||
handleControl(result, lastResult.control); | ||
} | ||
@@ -49,9 +49,9 @@ return result; | ||
} | ||
function handleIntent(meta, intent, initialized) { | ||
switch (intent.type) { | ||
function handleControl(meta, control, initialized) { | ||
switch (control.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; | ||
var _control$payload$name; | ||
var _name = (_control$payload$name = control.payload.name) !== null && _control$payload$name !== void 0 ? _control$payload$name : ''; | ||
var value = control.payload.value; | ||
updateValue(meta, _name, value); | ||
@@ -62,5 +62,5 @@ break; | ||
{ | ||
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 : ''; | ||
if (typeof control.payload.value === 'undefined' || control.payload.value) { | ||
var _control$payload$name2; | ||
var _name2 = (_control$payload$name2 = control.payload.name) !== null && _control$payload$name2 !== void 0 ? _control$payload$name2 : ''; | ||
var _value = formdata.getValue(meta.defaultValue, _name2); | ||
@@ -78,4 +78,4 @@ updateValue(meta, _name2, _value); | ||
meta.key = util.clone(meta.key); | ||
submission.setListState(meta.key, intent, util.generateId); | ||
submission.setListValue(meta.initialValue, intent); | ||
submission.setListState(meta.key, control, util.generateId); | ||
submission.setListValue(meta.initialValue, control); | ||
} | ||
@@ -108,5 +108,6 @@ break; | ||
function createValueProxy(value) { | ||
var val = formdata.simplify(value); | ||
return createStateProxy((name, proxy) => { | ||
if (name === '') { | ||
return value; | ||
return val; | ||
} | ||
@@ -161,14 +162,14 @@ var paths = formdata.getPaths(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)])); | ||
function shouldNotify(prev, next, cache, scope) { | ||
var compareFn = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : (prev, next) => JSON.stringify(prev) !== JSON.stringify(next); | ||
if (scope && prev !== next) { | ||
var _scope$prefix, _scope$name; | ||
var prefixes = (_scope$prefix = scope.prefix) !== null && _scope$prefix !== void 0 ? _scope$prefix : []; | ||
var names = (_scope$name = scope.name) !== null && _scope$name !== void 0 ? _scope$name : []; | ||
var list = prefixes.length === 0 ? names : Array.from(new Set([...Object.keys(prev), ...Object.keys(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]) { | ||
var _cache$_name; | ||
(_cache$_name = cache[_name3]) !== null && _cache$_name !== void 0 ? _cache$_name : cache[_name3] = compareFn(prev[_name3], next[_name3]); | ||
if (cache[_name3]) { | ||
return { | ||
@@ -204,6 +205,6 @@ v: true | ||
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 | ||
dirty: !state || prev.defaultValue !== next.defaultValue || prev.value !== next.value ? createDirtyProxy(defaultValue, value) : state.dirty | ||
}; | ||
} | ||
function createFormContext(formId, options) { | ||
function createFormContext(options) { | ||
var subscribers = []; | ||
@@ -214,4 +215,4 @@ var latestOptions = options; | ||
function getFormElement() { | ||
var element = document.forms.namedItem(formId); | ||
util.invariant(element !== null, "Form#".concat(formId, " does not exist")); | ||
var element = document.forms.namedItem(latestOptions.formId); | ||
util.invariant(element !== null, "Form#".concat(latestOptions.formId, " does not exist")); | ||
return element; | ||
@@ -238,36 +239,3 @@ } | ||
var subject = (_subscriber$getSubjec = subscriber.getSubject) === null || _subscriber$getSubjec === void 0 ? void 0 : _subscriber$getSubjec.call(subscriber); | ||
if (!subject || subject.status && prevState.submissionStatus !== nextState.submissionStatus || shouldNotify({ | ||
prev: prevState.error, | ||
next: nextState.error, | ||
cache: cache.error, | ||
scope: subject.error | ||
}) || shouldNotify({ | ||
prev: prevState.initialValue, | ||
next: nextState.initialValue, | ||
cache: cache.initialValue, | ||
scope: subject.initialValue | ||
}) || shouldNotify({ | ||
prev: prevState.key, | ||
next: nextState.key, | ||
compareFn: (prev, next) => prev !== next, | ||
cache: cache.key, | ||
scope: subject.key | ||
}) || shouldNotify({ | ||
prev: prevState.valid, | ||
next: nextState.valid, | ||
compareFn: compareBoolean, | ||
cache: cache.valid, | ||
scope: subject.valid | ||
}) || shouldNotify({ | ||
prev: prevState.dirty, | ||
next: nextState.dirty, | ||
compareFn: compareBoolean, | ||
cache: cache.dirty, | ||
scope: subject.dirty | ||
}) || shouldNotify({ | ||
prev: prevState.value, | ||
next: nextState.value, | ||
cache: cache.value, | ||
scope: subject.value | ||
})) { | ||
if (!subject || subject.status && prevState.submissionStatus !== nextState.submissionStatus || shouldNotify(prevState.error, nextState.error, cache.error, subject.error) || shouldNotify(prevState.initialValue, nextState.initialValue, cache.initialValue, subject.initialValue) || shouldNotify(prevState.key, nextState.key, cache.key, subject.key, (prev, next) => prev !== next) || shouldNotify(prevState.valid, nextState.valid, cache.valid, subject.valid, compareBoolean) || shouldNotify(prevState.dirty, nextState.dirty, cache.dirty, subject.dirty, compareBoolean) || shouldNotify(prevState.value, nextState.value, cache.value, subject.value)) { | ||
subscriber.callback(); | ||
@@ -304,3 +272,3 @@ } | ||
var submitter = event.submitter; | ||
util.invariant(form === getFormElement(), "The submit event is dispatched by form#".concat(form.id, " instead of form#").concat(formId)); | ||
util.invariant(form === getFormElement(), "The submit event is dispatched by form#".concat(form.id, " instead of form#").concat(latestOptions.formId)); | ||
var input = getStateInput(form); | ||
@@ -321,20 +289,15 @@ | ||
} else { | ||
try { | ||
var _latestOptions$onSubm2, _latestOptions3; | ||
var submission = latestOptions.onValidate({ | ||
form, | ||
formData, | ||
submitter | ||
}); | ||
if (!submission.value && submission.error !== null) { | ||
report(submission.reject()); | ||
event.preventDefault(); | ||
} | ||
(_latestOptions$onSubm2 = (_latestOptions3 = latestOptions).onSubmit) === null || _latestOptions$onSubm2 === void 0 || _latestOptions$onSubm2.call(_latestOptions3, event, _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, { | ||
submission | ||
})); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.warn('Client validation failed', error); | ||
var _latestOptions$onSubm2, _latestOptions3; | ||
var submission = latestOptions.onValidate({ | ||
form, | ||
formData, | ||
submitter | ||
}); | ||
if (submission.status !== 'success' && submission.error !== null) { | ||
report(submission.reply()); | ||
event.preventDefault(); | ||
} | ||
(_latestOptions$onSubm2 = (_latestOptions3 = latestOptions).onSubmit) === null || _latestOptions$onSubm2 === void 0 || _latestOptions$onSubm2.call(_latestOptions3, event, _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, { | ||
submission | ||
})); | ||
} | ||
@@ -370,3 +333,5 @@ } | ||
} else { | ||
submission.requestIntent(formId, submission.intent.validate(element.name)); | ||
dispatch(submission.control.validate({ | ||
name: element.name | ||
})); | ||
} | ||
@@ -379,3 +344,5 @@ } | ||
} | ||
submission.requestIntent(formId, submission.intent.validate(element.name)); | ||
dispatch(submission.control.validate({ | ||
name: element.name | ||
})); | ||
} | ||
@@ -410,17 +377,10 @@ function reset(event) { | ||
}); | ||
if (result.intent) { | ||
handleIntent(update, result.intent, true); | ||
if (result.control) { | ||
handleControl(update, result.control, true); | ||
} | ||
updateFormMeta(update); | ||
// TODO: An option to configure the validationMessage | ||
for (var element of formElement.elements) { | ||
if (dom.isFieldElement(element) && element.name !== '') { | ||
element.setCustomValidity(error[element.name] ? 'Invalid' : ''); | ||
} | ||
} | ||
if (result.status === 'error') { | ||
for (var _element of formElement.elements) { | ||
if (dom.isFieldElement(_element) && error[_element.name]) { | ||
_element.focus(); | ||
for (var element of formElement.elements) { | ||
if (dom.isFieldElement(element) && error[element.name]) { | ||
element.focus(); | ||
break; | ||
@@ -432,3 +392,7 @@ } | ||
function update(options) { | ||
var currentFormId = latestOptions.formId; | ||
latestOptions = options; | ||
if (latestOptions.formId !== currentFormId) { | ||
getFormElement().reset(); | ||
} | ||
} | ||
@@ -448,4 +412,26 @@ function subscribe(callback, getSubject) { | ||
} | ||
function dispatch(control) { | ||
var form = getFormElement(); | ||
var submitter = document.createElement('button'); | ||
var buttonProps = getControlButtonProps(control); | ||
submitter.name = buttonProps.name; | ||
submitter.value = buttonProps.value; | ||
submitter.hidden = true; | ||
submitter.formNoValidate = true; | ||
form === null || form === void 0 || form.appendChild(submitter); | ||
dom.requestSubmit(form, submitter); | ||
form === null || form === void 0 || form.removeChild(submitter); | ||
} | ||
function getControlButtonProps(control) { | ||
return { | ||
name: submission.CONTROL, | ||
value: submission.serializeControl(control), | ||
form: latestOptions.formId, | ||
formNoValidate: true | ||
}; | ||
} | ||
return { | ||
formId, | ||
get formId() { | ||
return latestOptions.formId; | ||
}, | ||
submit, | ||
@@ -455,2 +441,4 @@ reset, | ||
blur, | ||
dispatch, | ||
getControlButtonProps, | ||
report, | ||
@@ -457,0 +445,0 @@ update, |
@@ -159,3 +159,3 @@ 'use strict'; | ||
} | ||
if (value === null || isFile(value)) { | ||
if (typeof value === 'string' && value === '' || value === null || isFile(value)) { | ||
return; | ||
@@ -162,0 +162,0 @@ } |
@@ -1,5 +0,5 @@ | ||
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 UnionKeyof, type UnionKeyType, type Constraint, type ControlButtonProps, 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, serializeIntent as serializeIntent, requestIntent, parse, } from './submission'; | ||
export { type Submission, type SubmissionResult, type FormControl, CONTROL, STATE, control, serializeControl, parse, } from './submission'; | ||
export { getPaths, formatPaths, isPrefix } from './formdata'; |
@@ -17,10 +17,9 @@ 'use strict'; | ||
exports.invariant = util.invariant; | ||
exports.INTENT = submission.INTENT; | ||
exports.CONTROL = submission.CONTROL; | ||
exports.STATE = submission.STATE; | ||
exports.intent = submission.intent; | ||
exports.control = submission.control; | ||
exports.parse = submission.parse; | ||
exports.requestIntent = submission.requestIntent; | ||
exports.serializeIntent = submission.serializeIntent; | ||
exports.serializeControl = submission.serializeControl; | ||
exports.formatPaths = formdata.formatPaths; | ||
exports.getPaths = formdata.getPaths; | ||
exports.isPrefix = formdata.isPrefix; |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.0-pre.4", | ||
"version": "1.0.0-pre.5", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "module": "index.mjs", |
@@ -5,46 +5,39 @@ import type { DefaultValue, FieldName, FormValue } from './form'; | ||
}; | ||
export type SubmissionContext<Value = null, Error = unknown> = { | ||
intent: Intent | null; | ||
export type SubmissionContext<Value = null, FormError = unknown> = { | ||
control: FormControl | null; | ||
payload: Record<string, unknown>; | ||
fields: string[]; | ||
value: Value | null; | ||
error: Record<string, Error | null> | null; | ||
value?: Value; | ||
error?: Record<string, FormError | null> | null; | ||
state: SubmissionState; | ||
}; | ||
export type Submission<Schema, Error = unknown, Value = Schema> = { | ||
type: 'submit'; | ||
export type Submission<Schema, FormError = string[], Value = Schema> = { | ||
status: 'success'; | ||
payload: Record<string, unknown>; | ||
value: Value | null; | ||
error: Record<string, Error | null> | null; | ||
reject(options?: RejectOptions<Error>): SubmissionResult<Error>; | ||
accept(options?: AcceptOptions): SubmissionResult<Error>; | ||
value: Value; | ||
reply(options?: ReplyOptions<FormError>): SubmissionResult<FormError>; | ||
} | { | ||
type: 'update'; | ||
status: 'error' | undefined; | ||
payload: Record<string, unknown>; | ||
value: null; | ||
error: Record<string, Error | null> | null; | ||
reject(options?: RejectOptions<Error>): SubmissionResult<Error>; | ||
accept(options?: AcceptOptions): SubmissionResult<Error>; | ||
error: Record<string, FormError | null> | null; | ||
reply(options?: ReplyOptions<FormError>): SubmissionResult<FormError>; | ||
}; | ||
export type SubmissionResult<Error = unknown> = { | ||
export type SubmissionResult<FormError = string[]> = { | ||
status?: 'error' | 'success'; | ||
intent?: Intent; | ||
control?: FormControl; | ||
initialValue?: Record<string, unknown> | null; | ||
error?: Record<string, Error | null>; | ||
error?: Record<string, FormError | null>; | ||
state?: SubmissionState; | ||
}; | ||
export type AcceptOptions = { | ||
export type ReplyOptions<FormError> = { | ||
resetForm?: boolean; | ||
} | { | ||
formErrors?: FormError; | ||
fieldErrors?: Record<string, FormError>; | ||
hideFields?: string[]; | ||
}; | ||
export type RejectOptions<Error> = { | ||
formError?: Error; | ||
fieldError?: Record<string, Error>; | ||
hideFields?: string[]; | ||
}; | ||
/** | ||
* The name to be used when submitting an intent | ||
* The name to be used when submitting a form control | ||
*/ | ||
export declare const INTENT = "__intent__"; | ||
export declare const CONTROL = "__control__"; | ||
/** | ||
@@ -55,32 +48,32 @@ * The name to be used when submitting a state | ||
export declare function getSubmissionContext(body: FormData | URLSearchParams): SubmissionContext; | ||
export declare function parse<Value, Error>(payload: FormData | URLSearchParams, options: { | ||
resolve: (payload: Record<string, any>, intent: Intent | null) => { | ||
value?: Value; | ||
error?: Record<string, Error | null> | null; | ||
export declare function parse<FormValue, FormError>(payload: FormData | URLSearchParams, options: { | ||
resolve: (payload: Record<string, any>, control: FormControl | null) => { | ||
value?: FormValue; | ||
error?: Record<string, FormError | null> | null; | ||
}; | ||
}): Submission<Value, Error>; | ||
export declare function parse<Value, Error>(payload: FormData | URLSearchParams, options: { | ||
resolve: (payload: Record<string, any>, intent: Intent | null) => Promise<{ | ||
value?: Value; | ||
error?: Record<string, Error | null> | null; | ||
}): Submission<FormValue, FormError>; | ||
export declare function parse<FormValue, FormError>(payload: FormData | URLSearchParams, options: { | ||
resolve: (payload: Record<string, any>, control: FormControl | null) => Promise<{ | ||
value?: FormValue; | ||
error?: Record<string, FormError | null> | null; | ||
}>; | ||
}): Promise<Submission<Value, Error>>; | ||
export declare function parse<Value, Error>(payload: FormData | URLSearchParams, options: { | ||
resolve: (payload: Record<string, any>, intent: Intent | null) => { | ||
value?: Value; | ||
error?: Record<string, Error | null> | null; | ||
}): Promise<Submission<FormValue, FormError>>; | ||
export declare function parse<FormValue, FormError>(payload: FormData | URLSearchParams, options: { | ||
resolve: (payload: Record<string, any>, control: FormControl | null) => { | ||
value?: FormValue; | ||
error?: Record<string, FormError | null> | null; | ||
} | Promise<{ | ||
value?: Value; | ||
error?: Record<string, Error | null> | null; | ||
value?: FormValue; | ||
error?: Record<string, FormError | null> | null; | ||
}>; | ||
}): Submission<Value, Error> | Promise<Submission<Value, Error>>; | ||
export declare function createSubmission<Value, Error>(context: Required<SubmissionContext<Value, Error>>): Submission<Value, Error>; | ||
export declare function hideFields(payload: Record<string, unknown>, fields: string[]): void; | ||
export declare function acceptSubmission<Error>(context: Required<SubmissionContext<unknown, Error>>, options?: AcceptOptions): SubmissionResult<Error>; | ||
export declare function rejectSubmission<Error>(context: Required<SubmissionContext<unknown, Error>>, options?: RejectOptions<Error>): SubmissionResult<Error>; | ||
export type ValidateIntent<Schema = any> = { | ||
}): Submission<FormValue, FormError> | Promise<Submission<FormValue, FormError>>; | ||
export declare function createSubmission<FormValue, FormError>(context: SubmissionContext<FormValue, FormError>): Submission<FormValue, FormError>; | ||
export declare function replySubmission<FormError>(context: SubmissionContext<unknown, FormError>, options?: ReplyOptions<FormError>): SubmissionResult<FormError>; | ||
export type ValidateControl<Schema = any> = { | ||
type: 'validate'; | ||
payload: FieldName<Schema>; | ||
payload: { | ||
name?: FieldName<Schema>; | ||
}; | ||
}; | ||
export type ResetIntent<Schema = any> = { | ||
export type ResetControl<Schema = any> = { | ||
type: 'reset'; | ||
@@ -93,3 +86,3 @@ payload: { | ||
}; | ||
export type ReplaceIntent<Schema = unknown> = { | ||
export type ReplaceControl<Schema = unknown> = { | ||
type: 'replace'; | ||
@@ -102,3 +95,3 @@ payload: { | ||
}; | ||
export type RemoveIntent<Schema extends Array<any> = any> = { | ||
export type RemoveControl<Schema extends Array<any> = any> = { | ||
type: 'remove'; | ||
@@ -110,3 +103,3 @@ payload: { | ||
}; | ||
export type InsertIntent<Schema extends Array<any> = any> = { | ||
export type InsertControl<Schema extends Array<any> = any> = { | ||
type: 'insert'; | ||
@@ -119,3 +112,3 @@ payload: { | ||
}; | ||
export type ReorderIntent<Schema extends Array<any> = any> = { | ||
export type ReorderControl<Schema extends Array<any> = any> = { | ||
type: 'reorder'; | ||
@@ -128,12 +121,11 @@ payload: { | ||
}; | ||
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 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 type FormControl<Schema = unknown> = ValidateControl<Schema> | ResetControl<Schema> | ReplaceControl<Schema> | ReorderControl<Schema extends Array<any> ? Schema : any> | RemoveControl<Schema extends Array<any> ? Schema : any> | InsertControl<Schema extends Array<any> ? Schema : any>; | ||
export declare function getControl(serializedControl: string | null | undefined): FormControl | null; | ||
export declare function serializeControl(control: FormControl): string; | ||
export declare function updateList(list: unknown, control: InsertControl | RemoveControl | ReorderControl): void; | ||
export declare function setListValue(data: Record<string, unknown>, control: InsertControl | RemoveControl | ReorderControl): 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?: () => string): void; | ||
export declare function setListState(state: Record<string, unknown>, control: InsertControl | RemoveControl | ReorderControl, getDefaultValue?: () => string): void; | ||
export declare function serialize<Schema>(defaultValue: DefaultValue<Schema>): FormValue<Schema>; | ||
export declare const intent: { | ||
export declare const control: { | ||
reset: <Schema>(payload?: { | ||
@@ -143,4 +135,6 @@ name?: FieldName<Schema> | undefined; | ||
validated?: boolean | undefined; | ||
} | undefined) => ResetIntent<Schema>; | ||
validate: <Schema_1>(payload: FieldName<Schema_1>) => ValidateIntent<Schema_1>; | ||
} | undefined) => ResetControl<Schema>; | ||
validate: <Schema_1>(payload?: { | ||
name?: FieldName<Schema_1> | undefined; | ||
} | undefined) => ValidateControl<Schema_1>; | ||
replace: <Schema_2>(payload: { | ||
@@ -150,3 +144,3 @@ name: FieldName<Schema_2>; | ||
validated?: boolean | undefined; | ||
}) => ReplaceIntent<Schema_2>; | ||
}) => ReplaceControl<Schema_2>; | ||
reorder: <Schema_3>(payload: { | ||
@@ -156,7 +150,7 @@ name: FieldName<Schema_3 extends any[] ? Schema_3 : any>; | ||
to: number; | ||
}) => ReorderIntent<Schema_3 extends any[] ? Schema_3 : any>; | ||
}) => ReorderControl<Schema_3 extends any[] ? Schema_3 : any>; | ||
remove: <Schema_4>(payload: { | ||
name: FieldName<Schema_4 extends any[] ? Schema_4 : any>; | ||
index: number; | ||
}) => RemoveIntent<Schema_4 extends any[] ? Schema_4 : any>; | ||
}) => RemoveControl<Schema_4 extends any[] ? Schema_4 : any>; | ||
insert: <Schema_5>(payload: { | ||
@@ -166,3 +160,3 @@ name: FieldName<Schema_5 extends any[] ? Schema_5 : any>; | ||
index?: number | undefined; | ||
}) => InsertIntent<Schema_5 extends any[] ? Schema_5 : any>; | ||
}) => InsertControl<Schema_5 extends any[] ? Schema_5 : any>; | ||
}; |
@@ -6,3 +6,2 @@ 'use strict'; | ||
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js'); | ||
var dom = require('./dom.js'); | ||
var formdata = require('./formdata.js'); | ||
@@ -12,5 +11,5 @@ var util = require('./util.js'); | ||
/** | ||
* The name to be used when submitting an intent | ||
* The name to be used when submitting a form control | ||
*/ | ||
var INTENT = '__intent__'; | ||
var CONTROL = '__control__'; | ||
@@ -22,9 +21,9 @@ /** | ||
function getSubmissionContext(body) { | ||
var intent = body.get(INTENT); | ||
var control = body.get(CONTROL); | ||
var state = body.get(STATE); | ||
var payload = {}; | ||
var fields = []; | ||
util.invariant((typeof intent === 'string' || intent === null) && (typeof state === 'string' || state === null), "The input name \"".concat(INTENT, "\" and \"").concat(STATE, "\" are reserved by Conform. Please use another name for your input.")); | ||
util.invariant((typeof control === 'string' || control === null) && (typeof state === 'string' || state === null), "The input name \"".concat(CONTROL, "\" and \"").concat(STATE, "\" are reserved by Conform. Please use another name for your input.")); | ||
var _loop = function _loop(next) { | ||
if (name === INTENT || name === STATE) { | ||
if (name === CONTROL || name === STATE) { | ||
return 1; // continue | ||
@@ -48,9 +47,7 @@ } | ||
payload, | ||
intent: getIntent(intent), | ||
control: getControl(control), | ||
state: state ? JSON.parse(state) : { | ||
validated: {} | ||
}, | ||
fields, | ||
value: null, | ||
error: null | ||
fields | ||
}; | ||
@@ -60,7 +57,9 @@ } | ||
var context = getSubmissionContext(payload); | ||
var intent = context.intent; | ||
if (intent) { | ||
switch (intent.type) { | ||
var control = context.control; | ||
if (control) { | ||
switch (control.type) { | ||
case 'validate': | ||
context.state.validated[intent.payload] = true; | ||
if (control.payload.name) { | ||
context.state.validated[control.payload.name] = true; | ||
} | ||
break; | ||
@@ -73,3 +72,3 @@ case 'replace': | ||
validated | ||
} = intent.payload; | ||
} = control.payload; | ||
if (name) { | ||
@@ -107,3 +106,3 @@ formdata.setValue(context.payload, name, () => _value); | ||
validated: _validated | ||
} = intent.payload; | ||
} = control.payload; | ||
if (typeof _value2 === 'undefined' || _value2) { | ||
@@ -130,5 +129,5 @@ if (_name) { | ||
{ | ||
setListValue(context.payload, intent); | ||
setListState(context.state.validated, intent); | ||
context.state.validated[intent.payload.name] = true; | ||
setListValue(context.payload, control); | ||
setListState(context.state.validated, control); | ||
context.state.validated[control.payload.name] = true; | ||
break; | ||
@@ -138,7 +137,6 @@ } | ||
} | ||
var result = options.resolve(context.payload, intent); | ||
var result = options.resolve(context.payload, control); | ||
var mergeResolveResult = resolved => { | ||
var _resolved$value; | ||
var error = typeof resolved.error !== 'undefined' ? resolved.error : {}; | ||
if (!intent) { | ||
if (!control || control.type === 'validate' && !control.payload.name) { | ||
for (var _name2 of [...context.fields, ...Object.keys(error !== null && error !== void 0 ? error : {})]) { | ||
@@ -149,4 +147,4 @@ context.state.validated[_name2] = true; | ||
return createSubmission(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, context), {}, { | ||
value: (_resolved$value = resolved.value) !== null && _resolved$value !== void 0 ? _resolved$value : null, | ||
error | ||
value: resolved.value, | ||
error: resolved.error | ||
})); | ||
@@ -160,13 +158,9 @@ }; | ||
function createSubmission(context) { | ||
if (context.intent) { | ||
if (context.control || !context.value || context.error) { | ||
return { | ||
type: 'update', | ||
status: !context.control ? 'error' : undefined, | ||
payload: context.payload, | ||
value: null, | ||
error: context.error, | ||
accept(options) { | ||
return acceptSubmission(context, options); | ||
}, | ||
reject(options) { | ||
return rejectSubmission(context, options); | ||
error: typeof context.error !== 'undefined' ? context.error : {}, | ||
reply(options) { | ||
return replySubmission(context, options); | ||
} | ||
@@ -176,49 +170,18 @@ }; | ||
return { | ||
type: 'submit', | ||
status: 'success', | ||
payload: context.payload, | ||
value: context.value, | ||
error: context.error, | ||
accept(options) { | ||
return acceptSubmission(context, options); | ||
}, | ||
reject(options) { | ||
return rejectSubmission(context, options); | ||
reply(options) { | ||
return replySubmission(context, options); | ||
} | ||
}; | ||
} | ||
function hideFields(payload, fields) { | ||
for (var name of fields) { | ||
var _value3 = formdata.getValue(payload, name); | ||
if (typeof _value3 !== 'undefined') { | ||
formdata.setValue(payload, name, () => undefined); | ||
} | ||
} | ||
} | ||
function acceptSubmission(context, options) { | ||
var _simplify; | ||
if (options) { | ||
if ('resetForm' in options && options.resetForm) { | ||
return { | ||
status: 'success', | ||
initialValue: null | ||
}; | ||
} | ||
if ('hideFields' in options && options.hideFields) { | ||
hideFields(context.payload, options.hideFields); | ||
} | ||
} | ||
return { | ||
status: 'success', | ||
initialValue: (_simplify = formdata.simplify(context.payload)) !== null && _simplify !== void 0 ? _simplify : {}, | ||
error: formdata.simplify(context.error), | ||
state: context.state | ||
}; | ||
} | ||
function rejectSubmission(context, options) { | ||
var _context$intent, _context$error, _simplify2; | ||
switch ((_context$intent = context.intent) === null || _context$intent === void 0 ? void 0 : _context$intent.type) { | ||
function replySubmission(context) { | ||
var _context$control, _context$error, _simplify; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
switch ((_context$control = context.control) === null || _context$control === void 0 ? void 0 : _context$control.type) { | ||
case 'reset': | ||
{ | ||
var _context$intent$paylo; | ||
var name = (_context$intent$paylo = context.intent.payload.name) !== null && _context$intent$paylo !== void 0 ? _context$intent$paylo : ''; | ||
var _context$control$payl; | ||
var name = (_context$control$payl = context.control.payload.name) !== null && _context$control$payl !== void 0 ? _context$control$payl : ''; | ||
if (name === '') { | ||
@@ -231,67 +194,68 @@ return { | ||
} | ||
var error = Object.entries((_context$error = context.error) !== null && _context$error !== void 0 ? _context$error : {}).reduce((result, _ref) => { | ||
var [name, currentError] = _ref; | ||
if ('resetForm' in options && options.resetForm) { | ||
return { | ||
initialValue: null | ||
}; | ||
} | ||
if ('hideFields' in options && options.hideFields) { | ||
for (var _name3 of options.hideFields) { | ||
var _value3 = formdata.getValue(context.payload, _name3); | ||
if (typeof _value3 !== 'undefined') { | ||
formdata.setValue(context.payload, _name3, () => undefined); | ||
} | ||
} | ||
} | ||
var submissionError = Object.entries((_context$error = context.error) !== null && _context$error !== void 0 ? _context$error : {}).reduce((result, _ref) => { | ||
var [name, error] = _ref; | ||
if (context.state.validated[name]) { | ||
var _options$fieldError; | ||
var newError = name === '' ? options === null || options === void 0 ? void 0 : options.formError : options === null || options === void 0 || (_options$fieldError = options.fieldError) === null || _options$fieldError === void 0 ? void 0 : _options$fieldError[name]; | ||
result[name] = newError !== null && newError !== void 0 ? newError : currentError; | ||
result[name] = error; | ||
} | ||
return result; | ||
}, {}); | ||
if (options !== null && options !== void 0 && options.hideFields) { | ||
hideFields(context.payload, options.hideFields); | ||
} | ||
var extraError = 'formErrors' in options || 'fieldErrors' in options ? formdata.simplify(_rollupPluginBabelHelpers.objectSpread2({ | ||
'': options.formErrors | ||
}, options.fieldErrors)) : null; | ||
var error = formdata.simplify(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, submissionError), extraError)); | ||
return { | ||
status: context.intent !== null ? undefined : 'error', | ||
intent: context.intent !== null ? context.intent : undefined, | ||
initialValue: (_simplify2 = formdata.simplify(context.payload)) !== null && _simplify2 !== void 0 ? _simplify2 : {}, | ||
error: formdata.simplify(error), | ||
status: context.control ? undefined : error ? 'error' : 'success', | ||
control: context.control ? context.control : undefined, | ||
initialValue: (_simplify = formdata.simplify(context.payload)) !== null && _simplify !== void 0 ? _simplify : {}, | ||
error, | ||
state: context.state | ||
}; | ||
} | ||
function getIntent(serializedIntent) { | ||
if (!serializedIntent) { | ||
function getControl(serializedControl) { | ||
if (!serializedControl) { | ||
return null; | ||
} | ||
var intent = JSON.parse(serializedIntent); | ||
if (typeof intent.type !== 'string' || typeof intent.payload === 'undefined') { | ||
throw new Error('Unknown intent'); | ||
var control = JSON.parse(serializedControl); | ||
if (typeof control.type !== 'string' || typeof control.payload === 'undefined') { | ||
throw new Error('Unknown control'); | ||
} | ||
return intent; | ||
return control; | ||
} | ||
function serializeIntent(intent) { | ||
return JSON.stringify(intent); | ||
function serializeControl(control) { | ||
return JSON.stringify(control); | ||
} | ||
function requestIntent(formId, intent) { | ||
var form = document.forms.namedItem(formId); | ||
var submitter = document.createElement('button'); | ||
submitter.name = INTENT; | ||
submitter.value = serializeIntent(intent); | ||
submitter.hidden = true; | ||
submitter.formNoValidate = true; | ||
form === null || form === void 0 || form.appendChild(submitter); | ||
dom.requestSubmit(form, submitter); | ||
form === null || form === void 0 || form.removeChild(submitter); | ||
} | ||
function updateList(list, intent) { | ||
var _intent$payload$index; | ||
function updateList(list, control) { | ||
var _control$payload$inde; | ||
util.invariant(Array.isArray(list), "Failed to update list. The value is not an array."); | ||
switch (intent.type) { | ||
switch (control.type) { | ||
case 'insert': | ||
list.splice((_intent$payload$index = intent.payload.index) !== null && _intent$payload$index !== void 0 ? _intent$payload$index : list.length, 0, serialize(intent.payload.defaultValue)); | ||
list.splice((_control$payload$inde = control.payload.index) !== null && _control$payload$inde !== void 0 ? _control$payload$inde : list.length, 0, serialize(control.payload.defaultValue)); | ||
break; | ||
case 'remove': | ||
list.splice(intent.payload.index, 1); | ||
list.splice(control.payload.index, 1); | ||
break; | ||
case 'reorder': | ||
list.splice(intent.payload.to, 0, ...list.splice(intent.payload.from, 1)); | ||
list.splice(control.payload.to, 0, ...list.splice(control.payload.from, 1)); | ||
break; | ||
default: | ||
throw new Error('Unknown list intent received'); | ||
throw new Error('Unknown list control received'); | ||
} | ||
} | ||
function setListValue(data, intent) { | ||
formdata.setValue(data, intent.payload.name, value => { | ||
function setListValue(data, control) { | ||
formdata.setValue(data, control.payload.name, value => { | ||
var list = value !== null && value !== void 0 ? value : []; | ||
updateList(list, intent); | ||
updateList(list, control); | ||
return list; | ||
@@ -344,10 +308,10 @@ }); | ||
} | ||
function setListState(state, intent, getDefaultValue) { | ||
setState(state, intent.payload.name, value => { | ||
function setListState(state, control, getDefaultValue) { | ||
setState(state, control.payload.name, value => { | ||
var list = value !== null && value !== void 0 ? value : []; | ||
switch (intent.type) { | ||
switch (control.type) { | ||
case 'insert': | ||
updateList(list, { | ||
type: intent.type, | ||
payload: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, intent.payload), {}, { | ||
type: control.type, | ||
payload: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, control.payload), {}, { | ||
defaultValue: getDefaultValue === null || getDefaultValue === void 0 ? void 0 : getDefaultValue() | ||
@@ -358,3 +322,3 @@ }) | ||
default: | ||
updateList(list, intent); | ||
updateList(list, control); | ||
break; | ||
@@ -393,3 +357,3 @@ } | ||
} | ||
var intent = new Proxy({}, { | ||
var control = new Proxy({}, { | ||
get(_, type) { | ||
@@ -406,15 +370,12 @@ return function () { | ||
exports.INTENT = INTENT; | ||
exports.CONTROL = CONTROL; | ||
exports.STATE = STATE; | ||
exports.acceptSubmission = acceptSubmission; | ||
exports.control = control; | ||
exports.createSubmission = createSubmission; | ||
exports.getIntent = getIntent; | ||
exports.getControl = getControl; | ||
exports.getSubmissionContext = getSubmissionContext; | ||
exports.hideFields = hideFields; | ||
exports.intent = intent; | ||
exports.parse = parse; | ||
exports.rejectSubmission = rejectSubmission; | ||
exports.requestIntent = requestIntent; | ||
exports.replySubmission = replySubmission; | ||
exports.serialize = serialize; | ||
exports.serializeIntent = serializeIntent; | ||
exports.serializeControl = serializeControl; | ||
exports.setListState = setListState; | ||
@@ -421,0 +382,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
101460
2698