@conform-to/dom
Advanced tools
Comparing version 0.4.0-pre.1 to 0.4.0-pre.2
@@ -26,10 +26,6 @@ export declare type Primitive = null | undefined | string | number | boolean | Date; | ||
export declare type Submission<Schema = unknown> = { | ||
type?: undefined; | ||
context: string; | ||
intent?: string; | ||
value: FieldValue<Schema>; | ||
error: Array<[string, string]>; | ||
} | { | ||
type: string; | ||
metadata: string; | ||
value: FieldValue<Schema>; | ||
error: Array<[string, string]>; | ||
}; | ||
@@ -41,3 +37,4 @@ export declare function isFieldElement(element: unknown): element is FieldElement; | ||
export declare function shouldValidate(submission: Submission, name: string): boolean; | ||
export declare function hasError(error: Array<[string, string]>, name: string): boolean; | ||
export declare function hasError(error: Array<[string, string]>, names?: string[]): boolean; | ||
export declare function getFormError(form: HTMLFormElement, getFieldError?: (field: FieldElement) => Array<[string, string]>): Array<[string, string]>; | ||
export declare function setFormError(form: HTMLFormElement, submission: Submission): void; | ||
@@ -44,0 +41,0 @@ export declare function setValue<T>(target: any, paths: Array<string | number>, valueFn: (prev?: T) => T): void; |
47
index.js
@@ -54,10 +54,23 @@ 'use strict'; | ||
function shouldValidate(submission, name) { | ||
return submission.type !== 'validate' || submission.metadata === name; | ||
return submission.context === 'submit' || submission.context === 'validate' && submission.intent === name; | ||
} | ||
function hasError(error, name) { | ||
function hasError(error, names) { | ||
return typeof error.find(_ref => { | ||
var [fieldName, message] = _ref; | ||
return fieldName === name && message !== ''; | ||
return (typeof names === 'undefined' || names.includes(fieldName)) && message !== ''; | ||
}) !== 'undefined'; | ||
} | ||
function getFormError(form, getFieldError) { | ||
var error = []; | ||
for (var element of form.elements) { | ||
if (isFieldElement(element) && element.willValidate) { | ||
var _getFieldError; | ||
error.push(...((_getFieldError = getFieldError === null || getFieldError === void 0 ? void 0 : getFieldError(element)) !== null && _getFieldError !== void 0 ? _getFieldError : [[element.name, element.validationMessage]])); | ||
} | ||
} | ||
return error; | ||
} | ||
function setFormError(form, submission) { | ||
@@ -104,2 +117,3 @@ var firstErrorByName = Object.fromEntries([...submission.error].reverse()); | ||
button.value = field !== null && field !== void 0 ? field : ''; | ||
button.formNoValidate = true; | ||
button.hidden = true; | ||
@@ -126,7 +140,8 @@ form.appendChild(button); | ||
for (var field of form.elements) { | ||
if (isFieldElement(field)) { | ||
for (var _field of form.elements) { | ||
if (isFieldElement(_field)) { | ||
// Focus on the first non button field | ||
if (!field.validity.valid && field.dataset.conformTouched && field.tagName !== 'BUTTON') { | ||
field.focus(); | ||
if (!_field.validity.valid && _field.dataset.conformTouched && _field.tagName !== 'BUTTON') { | ||
_field.focus(); | ||
break; | ||
@@ -147,3 +162,5 @@ } | ||
function parse(payload) { | ||
var hasCommand = false; | ||
var submission = { | ||
context: 'submit', | ||
value: {}, | ||
@@ -162,3 +179,3 @@ error: [] | ||
if (typeof submission.type !== 'undefined') { | ||
if (hasCommand) { | ||
throw new Error('The conform command could only be set on a button'); | ||
@@ -168,5 +185,6 @@ } | ||
submission = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, submission), {}, { | ||
type: submissionType, | ||
metadata: value | ||
context: submissionType, | ||
intent: value | ||
}); | ||
hasCommand = true; | ||
} else { | ||
@@ -188,3 +206,3 @@ var paths = getPaths(name); | ||
switch (submission.type) { | ||
switch (submission.context) { | ||
case 'list': | ||
@@ -248,7 +266,9 @@ submission = handleList(submission); | ||
function handleList(submission) { | ||
if (submission.type !== 'list') { | ||
var _submission$intent; | ||
if (submission.context !== 'list') { | ||
return submission; | ||
} | ||
var command = parseListCommand(submission.metadata); | ||
var command = parseListCommand((_submission$intent = submission.intent) !== null && _submission$intent !== void 0 ? _submission$intent : ''); | ||
var paths = getPaths(command.scope); | ||
@@ -268,2 +288,3 @@ setValue(submission.value, paths, list => { | ||
exports.getFormElement = getFormElement; | ||
exports.getFormError = getFormError; | ||
exports.getName = getName; | ||
@@ -270,0 +291,0 @@ exports.getPaths = getPaths; |
@@ -50,10 +50,23 @@ import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js'; | ||
function shouldValidate(submission, name) { | ||
return submission.type !== 'validate' || submission.metadata === name; | ||
return submission.context === 'submit' || submission.context === 'validate' && submission.intent === name; | ||
} | ||
function hasError(error, name) { | ||
function hasError(error, names) { | ||
return typeof error.find(_ref => { | ||
var [fieldName, message] = _ref; | ||
return fieldName === name && message !== ''; | ||
return (typeof names === 'undefined' || names.includes(fieldName)) && message !== ''; | ||
}) !== 'undefined'; | ||
} | ||
function getFormError(form, getFieldError) { | ||
var error = []; | ||
for (var element of form.elements) { | ||
if (isFieldElement(element) && element.willValidate) { | ||
var _getFieldError; | ||
error.push(...((_getFieldError = getFieldError === null || getFieldError === void 0 ? void 0 : getFieldError(element)) !== null && _getFieldError !== void 0 ? _getFieldError : [[element.name, element.validationMessage]])); | ||
} | ||
} | ||
return error; | ||
} | ||
function setFormError(form, submission) { | ||
@@ -100,2 +113,3 @@ var firstErrorByName = Object.fromEntries([...submission.error].reverse()); | ||
button.value = field !== null && field !== void 0 ? field : ''; | ||
button.formNoValidate = true; | ||
button.hidden = true; | ||
@@ -122,7 +136,8 @@ form.appendChild(button); | ||
for (var field of form.elements) { | ||
if (isFieldElement(field)) { | ||
for (var _field of form.elements) { | ||
if (isFieldElement(_field)) { | ||
// Focus on the first non button field | ||
if (!field.validity.valid && field.dataset.conformTouched && field.tagName !== 'BUTTON') { | ||
field.focus(); | ||
if (!_field.validity.valid && _field.dataset.conformTouched && _field.tagName !== 'BUTTON') { | ||
_field.focus(); | ||
break; | ||
@@ -143,3 +158,5 @@ } | ||
function parse(payload) { | ||
var hasCommand = false; | ||
var submission = { | ||
context: 'submit', | ||
value: {}, | ||
@@ -158,3 +175,3 @@ error: [] | ||
if (typeof submission.type !== 'undefined') { | ||
if (hasCommand) { | ||
throw new Error('The conform command could only be set on a button'); | ||
@@ -164,5 +181,6 @@ } | ||
submission = _objectSpread2(_objectSpread2({}, submission), {}, { | ||
type: submissionType, | ||
metadata: value | ||
context: submissionType, | ||
intent: value | ||
}); | ||
hasCommand = true; | ||
} else { | ||
@@ -184,3 +202,3 @@ var paths = getPaths(name); | ||
switch (submission.type) { | ||
switch (submission.context) { | ||
case 'list': | ||
@@ -244,7 +262,9 @@ submission = handleList(submission); | ||
function handleList(submission) { | ||
if (submission.type !== 'list') { | ||
var _submission$intent; | ||
if (submission.context !== 'list') { | ||
return submission; | ||
} | ||
var command = parseListCommand(submission.metadata); | ||
var command = parseListCommand((_submission$intent = submission.intent) !== null && _submission$intent !== void 0 ? _submission$intent : ''); | ||
var paths = getPaths(command.scope); | ||
@@ -261,2 +281,2 @@ setValue(submission.value, paths, list => { | ||
export { focusFirstInvalidField, getFormData, getFormElement, getName, getPaths, getSubmissionType, handleList, hasError, isFieldElement, parse, parseListCommand, requestSubmit, requestValidate, setFormError, setValue, shouldValidate, updateList }; | ||
export { focusFirstInvalidField, getFormData, getFormElement, getFormError, 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.1", | ||
"version": "0.4.0-pre.2", | ||
"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
24826
635