@conform-to/dom
Advanced tools
Comparing version 0.4.0-pre.0 to 0.4.0-pre.1
@@ -25,13 +25,12 @@ export declare type Primitive = null | undefined | string | number | boolean | Date; | ||
}; | ||
export declare type FormState<Schema = unknown> = { | ||
scope: string[]; | ||
export declare type Submission<Schema = unknown> = { | ||
type?: undefined; | ||
value: FieldValue<Schema>; | ||
error: Array<[string, string]>; | ||
}; | ||
export declare type Submission<Schema = unknown> = FormState<Schema> & ({ | ||
type?: undefined; | ||
} | { | ||
type: string; | ||
data: string; | ||
}); | ||
metadata: string; | ||
value: FieldValue<Schema>; | ||
error: Array<[string, string]>; | ||
}; | ||
export declare function isFieldElement(element: unknown): element is FieldElement; | ||
@@ -41,4 +40,5 @@ export declare function getPaths(name: string): Array<string | number>; | ||
export declare function getName(paths: Array<string | number>): string; | ||
export declare function shouldValidate(submission: Submission, name: string): boolean; | ||
export declare function hasError(error: Array<[string, string]>, name: string): boolean; | ||
export declare function reportValidity(form: HTMLFormElement, state: FormState): boolean; | ||
export declare function setFormError(form: HTMLFormElement, submission: Submission): void; | ||
export declare function setValue<T>(target: any, paths: Array<string | number>, valueFn: (prev?: T) => T): void; | ||
@@ -48,3 +48,3 @@ export declare function requestSubmit(form: HTMLFormElement, submitter?: HTMLButtonElement | HTMLInputElement): void; | ||
export declare function getFormElement(element: HTMLFormElement | HTMLFieldSetElement | HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | HTMLButtonElement | null): HTMLFormElement | null; | ||
export declare function focusFirstInvalidField(form: HTMLFormElement, fields?: string[]): void; | ||
export declare function focusFirstInvalidField(form: HTMLFormElement): void; | ||
export declare function getSubmissionType(name: string): string | null; | ||
@@ -51,0 +51,0 @@ export declare function parse<Schema extends Record<string, any>>(payload: FormData | URLSearchParams): Submission<Schema>; |
63
index.js
@@ -53,2 +53,5 @@ 'use strict'; | ||
} | ||
function shouldValidate(submission, name) { | ||
return submission.type !== 'validate' || submission.metadata === name; | ||
} | ||
function hasError(error, name) { | ||
@@ -60,16 +63,14 @@ return typeof error.find(_ref => { | ||
} | ||
function reportValidity(form, state) { | ||
var firstErrorByName = Object.fromEntries([...state.error].reverse()); | ||
function setFormError(form, submission) { | ||
var firstErrorByName = Object.fromEntries([...submission.error].reverse()); | ||
for (var element of form.elements) { | ||
var _firstErrorByName$ele; | ||
if (isFieldElement(element)) { | ||
var error = firstErrorByName[element.name]; | ||
if (!isFieldElement(element) || !state.scope.includes(element.name)) { | ||
continue; | ||
if (typeof error !== 'undefined' || shouldValidate(submission, element.name)) { | ||
element.setCustomValidity(error !== null && error !== void 0 ? error : ''); | ||
} | ||
} | ||
element.setCustomValidity((_firstErrorByName$ele = firstErrorByName[element.name]) !== null && _firstErrorByName$ele !== void 0 ? _firstErrorByName$ele : ''); | ||
} | ||
return form.reportValidity(); | ||
} | ||
@@ -118,3 +119,3 @@ function setValue(target, paths, valueFn) { | ||
} | ||
function focusFirstInvalidField(form, fields) { | ||
function focusFirstInvalidField(form) { | ||
var currentFocus = document.activeElement; | ||
@@ -129,3 +130,3 @@ | ||
// Focus on the first non button field | ||
if (!field.validity.valid && field.dataset.conformTouched && field.tagName !== 'BUTTON' && (!fields || fields.includes(field.name))) { | ||
if (!field.validity.valid && field.dataset.conformTouched && field.tagName !== 'BUTTON') { | ||
field.focus(); | ||
@@ -149,4 +150,3 @@ break; | ||
value: {}, | ||
error: [], | ||
scope: [''] | ||
error: [] | ||
}; | ||
@@ -169,21 +169,6 @@ | ||
type: submissionType, | ||
data: value | ||
metadata: value | ||
}); | ||
} else { | ||
var paths = getPaths(name); | ||
var scopes = paths.reduce((result, path) => { | ||
if (result.length === 0) { | ||
if (typeof path !== 'string') { | ||
throw new Error("Invalid name received: ".concat(name)); | ||
} | ||
result.push(path); | ||
} else { | ||
var [lastName] = result.slice(-1); | ||
result.push(getName([lastName, path])); | ||
} | ||
return result; | ||
}, []); | ||
submission.scope.push(...scopes); | ||
setValue(submission.value, paths, prev => { | ||
@@ -204,9 +189,2 @@ if (prev) { | ||
switch (submission.type) { | ||
case 'validate': | ||
if (typeof submission.data !== 'undefined' && submission.data !== '') { | ||
submission.scope = [submission.data]; | ||
} | ||
break; | ||
case 'list': | ||
@@ -218,6 +196,4 @@ submission = handleList(submission); | ||
submission.error.push(['', e instanceof Error ? e.message : 'Invalid payload received']); | ||
} // Remove duplicates | ||
} | ||
submission.scope = Array.from(new Set(submission.scope)); | ||
return submission; | ||
@@ -277,3 +253,3 @@ } | ||
var command = parseListCommand(submission.data); | ||
var command = parseListCommand(submission.metadata); | ||
var paths = getPaths(command.scope); | ||
@@ -287,5 +263,3 @@ setValue(submission.value, paths, list => { | ||
}); | ||
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, submission), {}, { | ||
scope: [command.scope] | ||
}); | ||
return submission; | ||
} | ||
@@ -304,6 +278,7 @@ | ||
exports.parseListCommand = parseListCommand; | ||
exports.reportValidity = reportValidity; | ||
exports.requestSubmit = requestSubmit; | ||
exports.requestValidate = requestValidate; | ||
exports.setFormError = setFormError; | ||
exports.setValue = setValue; | ||
exports.shouldValidate = shouldValidate; | ||
exports.updateList = updateList; |
@@ -49,2 +49,5 @@ import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js'; | ||
} | ||
function shouldValidate(submission, name) { | ||
return submission.type !== 'validate' || submission.metadata === name; | ||
} | ||
function hasError(error, name) { | ||
@@ -56,16 +59,14 @@ return typeof error.find(_ref => { | ||
} | ||
function reportValidity(form, state) { | ||
var firstErrorByName = Object.fromEntries([...state.error].reverse()); | ||
function setFormError(form, submission) { | ||
var firstErrorByName = Object.fromEntries([...submission.error].reverse()); | ||
for (var element of form.elements) { | ||
var _firstErrorByName$ele; | ||
if (isFieldElement(element)) { | ||
var error = firstErrorByName[element.name]; | ||
if (!isFieldElement(element) || !state.scope.includes(element.name)) { | ||
continue; | ||
if (typeof error !== 'undefined' || shouldValidate(submission, element.name)) { | ||
element.setCustomValidity(error !== null && error !== void 0 ? error : ''); | ||
} | ||
} | ||
element.setCustomValidity((_firstErrorByName$ele = firstErrorByName[element.name]) !== null && _firstErrorByName$ele !== void 0 ? _firstErrorByName$ele : ''); | ||
} | ||
return form.reportValidity(); | ||
} | ||
@@ -114,3 +115,3 @@ function setValue(target, paths, valueFn) { | ||
} | ||
function focusFirstInvalidField(form, fields) { | ||
function focusFirstInvalidField(form) { | ||
var currentFocus = document.activeElement; | ||
@@ -125,3 +126,3 @@ | ||
// Focus on the first non button field | ||
if (!field.validity.valid && field.dataset.conformTouched && field.tagName !== 'BUTTON' && (!fields || fields.includes(field.name))) { | ||
if (!field.validity.valid && field.dataset.conformTouched && field.tagName !== 'BUTTON') { | ||
field.focus(); | ||
@@ -145,4 +146,3 @@ break; | ||
value: {}, | ||
error: [], | ||
scope: [''] | ||
error: [] | ||
}; | ||
@@ -165,21 +165,6 @@ | ||
type: submissionType, | ||
data: value | ||
metadata: value | ||
}); | ||
} else { | ||
var paths = getPaths(name); | ||
var scopes = paths.reduce((result, path) => { | ||
if (result.length === 0) { | ||
if (typeof path !== 'string') { | ||
throw new Error("Invalid name received: ".concat(name)); | ||
} | ||
result.push(path); | ||
} else { | ||
var [lastName] = result.slice(-1); | ||
result.push(getName([lastName, path])); | ||
} | ||
return result; | ||
}, []); | ||
submission.scope.push(...scopes); | ||
setValue(submission.value, paths, prev => { | ||
@@ -200,9 +185,2 @@ if (prev) { | ||
switch (submission.type) { | ||
case 'validate': | ||
if (typeof submission.data !== 'undefined' && submission.data !== '') { | ||
submission.scope = [submission.data]; | ||
} | ||
break; | ||
case 'list': | ||
@@ -214,6 +192,4 @@ submission = handleList(submission); | ||
submission.error.push(['', e instanceof Error ? e.message : 'Invalid payload received']); | ||
} // Remove duplicates | ||
} | ||
submission.scope = Array.from(new Set(submission.scope)); | ||
return submission; | ||
@@ -273,3 +249,3 @@ } | ||
var command = parseListCommand(submission.data); | ||
var command = parseListCommand(submission.metadata); | ||
var paths = getPaths(command.scope); | ||
@@ -283,7 +259,5 @@ setValue(submission.value, paths, list => { | ||
}); | ||
return _objectSpread2(_objectSpread2({}, submission), {}, { | ||
scope: [command.scope] | ||
}); | ||
return submission; | ||
} | ||
export { focusFirstInvalidField, getFormData, getFormElement, getName, getPaths, getSubmissionType, handleList, hasError, isFieldElement, parse, parseListCommand, reportValidity, requestSubmit, requestValidate, setValue, updateList }; | ||
export { focusFirstInvalidField, getFormData, getFormElement, getName, getPaths, getSubmissionType, handleList, hasError, isFieldElement, parse, parseListCommand, requestSubmit, requestValidate, setFormError, setValue, shouldValidate, updateList }; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.4.0-pre.0", | ||
"version": "0.4.0-pre.1", | ||
"main": "index.js", | ||
@@ -8,0 +8,0 @@ "module": "module/index.js", |
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
23242
607