@conform-to/react
Advanced tools
Comparing version 0.4.0-pre.0 to 0.4.0-pre.1
@@ -1,2 +0,2 @@ | ||
import { type FieldConfig, type FieldElement, type FieldValue, type FieldsetConstraint, type FormState, type ListCommand, type Primitive, type Submission } from '@conform-to/dom'; | ||
import { type FieldConfig, type FieldElement, type FieldValue, type FieldsetConstraint, type ListCommand, type Primitive, type Submission } from '@conform-to/dom'; | ||
import { type InputHTMLAttributes, type FormEvent, type RefObject } from 'react'; | ||
@@ -10,2 +10,6 @@ interface FormContext<Schema extends Record<string, any>> { | ||
/** | ||
* Validation mode. Default to `client-only`. | ||
*/ | ||
mode?: 'client-only' | 'server-validation'; | ||
/** | ||
* Define when the error should be reported initially. | ||
@@ -24,3 +28,3 @@ * Support "onSubmit", "onChange", "onBlur". | ||
*/ | ||
state?: FormState<Schema>; | ||
state?: Submission<Schema>; | ||
/** | ||
@@ -41,3 +45,3 @@ * Enable native validation before hydation. | ||
*/ | ||
onValidate?: (context: FormContext<Schema>) => boolean; | ||
onValidate?: (context: FormContext<Schema>) => void; | ||
/** | ||
@@ -67,3 +71,3 @@ * The submit event handler of the form. It will be called | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#useform | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#useform | ||
*/ | ||
@@ -109,3 +113,3 @@ export declare function useForm<Schema extends Record<string, any>>(config?: FormConfig<Schema>): Form<Schema>; | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#usefieldset | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usefieldset | ||
*/ | ||
@@ -137,3 +141,3 @@ export declare function useFieldset<Schema extends Record<string, any>>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config?: FieldsetConfig<Schema>): Fieldset<Schema>; | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#usefieldlist | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usefieldlist | ||
*/ | ||
@@ -168,3 +172,3 @@ export declare function useFieldList<Payload = any>(ref: RefObject<HTMLFormElement | HTMLFieldSetElement>, config: FieldConfig<Array<Payload>>): [ | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#usecontrolledinput | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usecontrolledinput | ||
*/ | ||
@@ -171,0 +175,0 @@ export declare function useControlledInput<Element extends { |
134
hooks.js
@@ -14,3 +14,3 @@ 'use strict'; | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#useform | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#useform | ||
*/ | ||
@@ -31,11 +31,10 @@ function useForm() { | ||
var [fieldsetConfig, setFieldsetConfig] = react.useState(() => { | ||
var _config$state$error2, _config$state2, _config$state3, _config$state$value, _config$state4; | ||
var _config$state$error2, _config$state2, _config$state$value, _config$state3; | ||
var error = (_config$state$error2 = (_config$state2 = config.state) === null || _config$state2 === void 0 ? void 0 : _config$state2.error) !== null && _config$state$error2 !== void 0 ? _config$state$error2 : []; | ||
var scope = (_config$state3 = config.state) === null || _config$state3 === void 0 ? void 0 : _config$state3.scope; | ||
return { | ||
defaultValue: (_config$state$value = (_config$state4 = config.state) === null || _config$state4 === void 0 ? void 0 : _config$state4.value) !== null && _config$state$value !== void 0 ? _config$state$value : config.defaultValue, | ||
defaultValue: (_config$state$value = (_config$state3 = config.state) === null || _config$state3 === void 0 ? void 0 : _config$state3.value) !== null && _config$state$value !== void 0 ? _config$state$value : config.defaultValue, | ||
initialError: error.filter(_ref2 => { | ||
var [name] = _ref2; | ||
return name !== '' && dom.getSubmissionType(name) === null && (!scope || scope.includes(name)); | ||
return name !== '' && dom.getSubmissionType(name) === null; | ||
}) | ||
@@ -58,4 +57,6 @@ }; | ||
if (!dom.reportValidity(form, config.state)) { | ||
dom.focusFirstInvalidField(form, config.state.scope); | ||
dom.setFormError(form, config.state); | ||
if (!form.reportValidity()) { | ||
dom.focusFirstInvalidField(form); | ||
} | ||
@@ -204,9 +205,15 @@ | ||
if (typeof config.onValidate === 'function' && !config.noValidate && !submitter.formNoValidate) { | ||
try { | ||
if (!config.onValidate(context)) { | ||
try { | ||
if (!config.noValidate && !submitter.formNoValidate) { | ||
var _config$onValidate; | ||
(_config$onValidate = config.onValidate) === null || _config$onValidate === void 0 ? void 0 : _config$onValidate.call(config, context); | ||
if (!form.reportValidity()) { | ||
dom.focusFirstInvalidField(form); | ||
event.preventDefault(); | ||
} | ||
} catch (e) { | ||
} | ||
} catch (e) { | ||
if (e !== form) { | ||
console.warn(e); | ||
@@ -217,5 +224,9 @@ } | ||
if (!event.defaultPrevented) { | ||
var _config$onSubmit; | ||
if (config.mode !== 'server-validation' && submission.type === 'validate') { | ||
event.preventDefault(); | ||
} else { | ||
var _config$onSubmit; | ||
(_config$onSubmit = config.onSubmit) === null || _config$onSubmit === void 0 ? void 0 : _config$onSubmit.call(config, event, context); | ||
(_config$onSubmit = config.onSubmit) === null || _config$onSubmit === void 0 ? void 0 : _config$onSubmit.call(config, event, context); | ||
} | ||
} | ||
@@ -283,60 +294,8 @@ } | ||
react.useEffect(() => { | ||
/** | ||
* Reset the error state of each field if its validity is changed. | ||
* | ||
* This is a workaround as no official way is provided to notify | ||
* when the validity of the field is changed from `invalid` to `valid`. | ||
*/ | ||
var resetError = form => { | ||
setError(prev => { | ||
var _configRef$current$na, _configRef$current; | ||
var next = prev; | ||
var fieldsetName = (_configRef$current$na = (_configRef$current = configRef.current) === null || _configRef$current === void 0 ? void 0 : _configRef$current.name) !== null && _configRef$current$na !== void 0 ? _configRef$current$na : ''; | ||
for (var field of form.elements) { | ||
if (dom.isFieldElement(field) && field.name.startsWith(fieldsetName)) { | ||
var [key, ...paths] = dom.getPaths(fieldsetName.length > 0 ? field.name.slice(fieldsetName.length + 1) : field.name); | ||
if (typeof key === 'string' && paths.length === 0) { | ||
var _next$key, _next; | ||
var prevMessage = (_next$key = (_next = next) === null || _next === void 0 ? void 0 : _next[key]) !== null && _next$key !== void 0 ? _next$key : ''; | ||
var nextMessage = field.validationMessage; | ||
/** | ||
* Techincally, checking prevMessage not being empty while nextMessage being empty | ||
* is sufficient for our usecase. It checks if the message is changed instead to allow | ||
* the hook to be useful independently. | ||
*/ | ||
if (prevMessage !== '' && prevMessage !== nextMessage) { | ||
next = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, next), {}, { | ||
[key]: nextMessage | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
return next; | ||
}); | ||
}; | ||
var handleInput = event => { | ||
var form = dom.getFormElement(ref.current); | ||
var field = event.target; | ||
if (!form || !dom.isFieldElement(field) || field.form !== form) { | ||
return; | ||
} | ||
resetError(form); | ||
}; | ||
var invalidHandler = event => { | ||
var _configRef$current$na2, _configRef$current2; | ||
var _configRef$current$na, _configRef$current; | ||
var form = dom.getFormElement(ref.current); | ||
var field = event.target; | ||
var fieldsetName = (_configRef$current$na2 = (_configRef$current2 = configRef.current) === null || _configRef$current2 === void 0 ? void 0 : _configRef$current2.name) !== null && _configRef$current$na2 !== void 0 ? _configRef$current$na2 : ''; | ||
var fieldsetName = (_configRef$current$na = (_configRef$current = configRef.current) === null || _configRef$current === void 0 ? void 0 : _configRef$current.name) !== null && _configRef$current$na !== void 0 ? _configRef$current$na : ''; | ||
@@ -375,6 +334,35 @@ if (!form || !dom.isFieldElement(field) || field.form !== form || !field.name.startsWith(fieldsetName)) { | ||
return; | ||
} // This helps resetting error that fullfilled by the submitter | ||
} | ||
/** | ||
* Reset the error state of each field if its validity is changed. | ||
* | ||
* This is a workaround as no official way is provided to notify | ||
* when the validity of the field is changed from `invalid` to `valid`. | ||
*/ | ||
resetError(form); | ||
setError(prev => { | ||
var _configRef$current$na2, _configRef$current2; | ||
var next = prev; | ||
var fieldsetName = (_configRef$current$na2 = (_configRef$current2 = configRef.current) === null || _configRef$current2 === void 0 ? void 0 : _configRef$current2.name) !== null && _configRef$current$na2 !== void 0 ? _configRef$current$na2 : ''; | ||
for (var field of form.elements) { | ||
if (dom.isFieldElement(field) && field.name.startsWith(fieldsetName)) { | ||
var _next$key, _next; | ||
var key = fieldsetName ? field.name.slice(fieldsetName.length + 1) : field.name; | ||
var prevMessage = (_next$key = (_next = next) === null || _next === void 0 ? void 0 : _next[key]) !== null && _next$key !== void 0 ? _next$key : ''; | ||
var nextMessage = field.validationMessage; | ||
if (prevMessage !== '' && nextMessage === '') { | ||
next = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, next), {}, { | ||
[key]: '' | ||
}); | ||
} | ||
} | ||
} | ||
return next; | ||
}); | ||
}; | ||
@@ -398,5 +386,4 @@ | ||
setError({}); | ||
}; | ||
}; // The invalid event does not bubble and so listening on the capturing pharse is needed | ||
document.addEventListener('input', handleInput); // The invalid event does not bubble and so listening on the capturing pharse is needed | ||
@@ -407,3 +394,2 @@ document.addEventListener('invalid', invalidHandler, true); | ||
return () => { | ||
document.removeEventListener('input', handleInput); | ||
document.removeEventListener('invalid', invalidHandler, true); | ||
@@ -449,3 +435,3 @@ document.removeEventListener('submit', submitHandler); | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#usefieldlist | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usefieldlist | ||
*/ | ||
@@ -594,3 +580,3 @@ function useFieldList(ref, config) { | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#usecontrolledinput | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usecontrolledinput | ||
*/ | ||
@@ -597,0 +583,0 @@ function useControlledInput(config) { |
@@ -1,3 +0,3 @@ | ||
export { type FieldsetConstraint, type FormState, type Submission, hasError, isFieldElement, parse, reportValidity, } from '@conform-to/dom'; | ||
export { type FieldsetConstraint, type Submission, hasError, isFieldElement, parse, setFormError, shouldValidate, } from '@conform-to/dom'; | ||
export * from './hooks'; | ||
export * as conform from './helpers'; |
@@ -23,6 +23,10 @@ 'use strict'; | ||
}); | ||
Object.defineProperty(exports, 'reportValidity', { | ||
Object.defineProperty(exports, 'setFormError', { | ||
enumerable: true, | ||
get: function () { return dom.reportValidity; } | ||
get: function () { return dom.setFormError; } | ||
}); | ||
Object.defineProperty(exports, 'shouldValidate', { | ||
enumerable: true, | ||
get: function () { return dom.shouldValidate; } | ||
}); | ||
exports.useControlledInput = hooks.useControlledInput; | ||
@@ -29,0 +33,0 @@ exports.useFieldList = hooks.useFieldList; |
import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js'; | ||
import { getSubmissionType, reportValidity, focusFirstInvalidField, requestSubmit, isFieldElement, getFormData, parse, getPaths, getName, requestValidate, getFormElement, parseListCommand, updateList } from '@conform-to/dom'; | ||
import { getSubmissionType, setFormError, focusFirstInvalidField, requestSubmit, isFieldElement, getFormData, parse, getPaths, getName, requestValidate, getFormElement, parseListCommand, updateList } from '@conform-to/dom'; | ||
import { useRef, useState, useEffect } from 'react'; | ||
@@ -10,3 +10,3 @@ import { input } from './helpers.js'; | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#useform | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#useform | ||
*/ | ||
@@ -27,11 +27,10 @@ function useForm() { | ||
var [fieldsetConfig, setFieldsetConfig] = useState(() => { | ||
var _config$state$error2, _config$state2, _config$state3, _config$state$value, _config$state4; | ||
var _config$state$error2, _config$state2, _config$state$value, _config$state3; | ||
var error = (_config$state$error2 = (_config$state2 = config.state) === null || _config$state2 === void 0 ? void 0 : _config$state2.error) !== null && _config$state$error2 !== void 0 ? _config$state$error2 : []; | ||
var scope = (_config$state3 = config.state) === null || _config$state3 === void 0 ? void 0 : _config$state3.scope; | ||
return { | ||
defaultValue: (_config$state$value = (_config$state4 = config.state) === null || _config$state4 === void 0 ? void 0 : _config$state4.value) !== null && _config$state$value !== void 0 ? _config$state$value : config.defaultValue, | ||
defaultValue: (_config$state$value = (_config$state3 = config.state) === null || _config$state3 === void 0 ? void 0 : _config$state3.value) !== null && _config$state$value !== void 0 ? _config$state$value : config.defaultValue, | ||
initialError: error.filter(_ref2 => { | ||
var [name] = _ref2; | ||
return name !== '' && getSubmissionType(name) === null && (!scope || scope.includes(name)); | ||
return name !== '' && getSubmissionType(name) === null; | ||
}) | ||
@@ -54,4 +53,6 @@ }; | ||
if (!reportValidity(form, config.state)) { | ||
focusFirstInvalidField(form, config.state.scope); | ||
setFormError(form, config.state); | ||
if (!form.reportValidity()) { | ||
focusFirstInvalidField(form); | ||
} | ||
@@ -200,9 +201,15 @@ | ||
if (typeof config.onValidate === 'function' && !config.noValidate && !submitter.formNoValidate) { | ||
try { | ||
if (!config.onValidate(context)) { | ||
try { | ||
if (!config.noValidate && !submitter.formNoValidate) { | ||
var _config$onValidate; | ||
(_config$onValidate = config.onValidate) === null || _config$onValidate === void 0 ? void 0 : _config$onValidate.call(config, context); | ||
if (!form.reportValidity()) { | ||
focusFirstInvalidField(form); | ||
event.preventDefault(); | ||
} | ||
} catch (e) { | ||
} | ||
} catch (e) { | ||
if (e !== form) { | ||
console.warn(e); | ||
@@ -213,5 +220,9 @@ } | ||
if (!event.defaultPrevented) { | ||
var _config$onSubmit; | ||
if (config.mode !== 'server-validation' && submission.type === 'validate') { | ||
event.preventDefault(); | ||
} else { | ||
var _config$onSubmit; | ||
(_config$onSubmit = config.onSubmit) === null || _config$onSubmit === void 0 ? void 0 : _config$onSubmit.call(config, event, context); | ||
(_config$onSubmit = config.onSubmit) === null || _config$onSubmit === void 0 ? void 0 : _config$onSubmit.call(config, event, context); | ||
} | ||
} | ||
@@ -279,60 +290,8 @@ } | ||
useEffect(() => { | ||
/** | ||
* Reset the error state of each field if its validity is changed. | ||
* | ||
* This is a workaround as no official way is provided to notify | ||
* when the validity of the field is changed from `invalid` to `valid`. | ||
*/ | ||
var resetError = form => { | ||
setError(prev => { | ||
var _configRef$current$na, _configRef$current; | ||
var next = prev; | ||
var fieldsetName = (_configRef$current$na = (_configRef$current = configRef.current) === null || _configRef$current === void 0 ? void 0 : _configRef$current.name) !== null && _configRef$current$na !== void 0 ? _configRef$current$na : ''; | ||
for (var field of form.elements) { | ||
if (isFieldElement(field) && field.name.startsWith(fieldsetName)) { | ||
var [key, ...paths] = getPaths(fieldsetName.length > 0 ? field.name.slice(fieldsetName.length + 1) : field.name); | ||
if (typeof key === 'string' && paths.length === 0) { | ||
var _next$key, _next; | ||
var prevMessage = (_next$key = (_next = next) === null || _next === void 0 ? void 0 : _next[key]) !== null && _next$key !== void 0 ? _next$key : ''; | ||
var nextMessage = field.validationMessage; | ||
/** | ||
* Techincally, checking prevMessage not being empty while nextMessage being empty | ||
* is sufficient for our usecase. It checks if the message is changed instead to allow | ||
* the hook to be useful independently. | ||
*/ | ||
if (prevMessage !== '' && prevMessage !== nextMessage) { | ||
next = _objectSpread2(_objectSpread2({}, next), {}, { | ||
[key]: nextMessage | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
return next; | ||
}); | ||
}; | ||
var handleInput = event => { | ||
var form = getFormElement(ref.current); | ||
var field = event.target; | ||
if (!form || !isFieldElement(field) || field.form !== form) { | ||
return; | ||
} | ||
resetError(form); | ||
}; | ||
var invalidHandler = event => { | ||
var _configRef$current$na2, _configRef$current2; | ||
var _configRef$current$na, _configRef$current; | ||
var form = getFormElement(ref.current); | ||
var field = event.target; | ||
var fieldsetName = (_configRef$current$na2 = (_configRef$current2 = configRef.current) === null || _configRef$current2 === void 0 ? void 0 : _configRef$current2.name) !== null && _configRef$current$na2 !== void 0 ? _configRef$current$na2 : ''; | ||
var fieldsetName = (_configRef$current$na = (_configRef$current = configRef.current) === null || _configRef$current === void 0 ? void 0 : _configRef$current.name) !== null && _configRef$current$na !== void 0 ? _configRef$current$na : ''; | ||
@@ -371,6 +330,35 @@ if (!form || !isFieldElement(field) || field.form !== form || !field.name.startsWith(fieldsetName)) { | ||
return; | ||
} // This helps resetting error that fullfilled by the submitter | ||
} | ||
/** | ||
* Reset the error state of each field if its validity is changed. | ||
* | ||
* This is a workaround as no official way is provided to notify | ||
* when the validity of the field is changed from `invalid` to `valid`. | ||
*/ | ||
resetError(form); | ||
setError(prev => { | ||
var _configRef$current$na2, _configRef$current2; | ||
var next = prev; | ||
var fieldsetName = (_configRef$current$na2 = (_configRef$current2 = configRef.current) === null || _configRef$current2 === void 0 ? void 0 : _configRef$current2.name) !== null && _configRef$current$na2 !== void 0 ? _configRef$current$na2 : ''; | ||
for (var field of form.elements) { | ||
if (isFieldElement(field) && field.name.startsWith(fieldsetName)) { | ||
var _next$key, _next; | ||
var key = fieldsetName ? field.name.slice(fieldsetName.length + 1) : field.name; | ||
var prevMessage = (_next$key = (_next = next) === null || _next === void 0 ? void 0 : _next[key]) !== null && _next$key !== void 0 ? _next$key : ''; | ||
var nextMessage = field.validationMessage; | ||
if (prevMessage !== '' && nextMessage === '') { | ||
next = _objectSpread2(_objectSpread2({}, next), {}, { | ||
[key]: '' | ||
}); | ||
} | ||
} | ||
} | ||
return next; | ||
}); | ||
}; | ||
@@ -394,5 +382,4 @@ | ||
setError({}); | ||
}; | ||
}; // The invalid event does not bubble and so listening on the capturing pharse is needed | ||
document.addEventListener('input', handleInput); // The invalid event does not bubble and so listening on the capturing pharse is needed | ||
@@ -403,3 +390,2 @@ document.addEventListener('invalid', invalidHandler, true); | ||
return () => { | ||
document.removeEventListener('input', handleInput); | ||
document.removeEventListener('invalid', invalidHandler, true); | ||
@@ -445,3 +431,3 @@ document.removeEventListener('submit', submitHandler); | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#usefieldlist | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usefieldlist | ||
*/ | ||
@@ -590,3 +576,3 @@ function useFieldList(ref, config) { | ||
* | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.0/packages/conform-react/README.md#usecontrolledinput | ||
* @see https://github.com/edmundhung/conform/tree/v0.4.0-pre.1/packages/conform-react/README.md#usecontrolledinput | ||
*/ | ||
@@ -593,0 +579,0 @@ function useControlledInput(config) { |
@@ -1,4 +0,4 @@ | ||
export { hasError, isFieldElement, parse, reportValidity } from '@conform-to/dom'; | ||
export { hasError, isFieldElement, parse, setFormError, shouldValidate } from '@conform-to/dom'; | ||
export { useControlledInput, useFieldList, useFieldset, useForm } from './hooks.js'; | ||
import * as helpers from './helpers.js'; | ||
export { helpers as conform }; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.4.0-pre.0", | ||
"version": "0.4.0-pre.1", | ||
"main": "index.js", | ||
@@ -23,3 +23,3 @@ "module": "module/index.js", | ||
"dependencies": { | ||
"@conform-to/dom": "0.4.0-pre.0" | ||
"@conform-to/dom": "0.4.0-pre.1" | ||
}, | ||
@@ -26,0 +26,0 @@ "peerDependencies": { |
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
77554
1516
+ Added@conform-to/dom@0.4.0-pre.1(transitive)
- Removed@conform-to/dom@0.4.0-pre.0(transitive)
Updated@conform-to/dom@0.4.0-pre.1