@mantine/form
Advanced tools
Comparing version 5.1.7 to 5.2.0-alpha.0
@@ -56,4 +56,11 @@ 'use strict'; | ||
const [errors, _setErrors] = react.useState(filterErrors.filterErrors(initialErrors)); | ||
const _dirtyValues = react.useRef(initialValues); | ||
const _setDirtyValues = (_values) => { | ||
_dirtyValues.current = _values; | ||
}; | ||
const resetTouched = react.useCallback(() => setTouched({}), []); | ||
const resetDirty = react.useCallback(() => setDirty({}), []); | ||
const resetDirty = (_values) => { | ||
_setDirtyValues(_values || values); | ||
setDirty({}); | ||
}; | ||
const setErrors = react.useCallback((errs) => _setErrors((current) => filterErrors.filterErrors(typeof errs === "function" ? errs(current) : errs)), []); | ||
@@ -64,3 +71,3 @@ const clearErrors = react.useCallback(() => _setErrors({}), []); | ||
clearErrors(); | ||
resetDirty(); | ||
resetDirty(initialValues); | ||
resetTouched(); | ||
@@ -80,3 +87,3 @@ }, []); | ||
_setValues((current) => { | ||
const initialValue = getPath.getPath(path, initialValues); | ||
const initialValue = getPath.getPath(path, _dirtyValues.current); | ||
const isFieldDirty = !isEqual__default(initialValue, value); | ||
@@ -146,2 +153,3 @@ setDirty((currentDirty) => __spreadProps(__spreadValues({}, currentDirty), { [path]: isFieldDirty })); | ||
const isTouched = react.useCallback((path) => getStatus.getStatus(touched, path), [touched]); | ||
const isValid = react.useCallback((path) => path ? !validateFieldValue.validateFieldValue(path, rules, values).hasError : !validateValues.validateValues(rules, values).hasErrors, [values, rules]); | ||
return { | ||
@@ -170,3 +178,4 @@ values, | ||
resetTouched, | ||
resetDirty | ||
resetDirty, | ||
isValid | ||
}; | ||
@@ -173,0 +182,0 @@ } |
@@ -22,3 +22,3 @@ 'use strict'; | ||
if (typeof rule === "function") { | ||
acc[rulePath] = rule(value, values); | ||
acc[rulePath] = rule(value, values, rulePath); | ||
} | ||
@@ -25,0 +25,0 @@ if (typeof rule === "object" && Array.isArray(value)) { |
@@ -1,2 +0,2 @@ | ||
import { useState, useCallback } from 'react'; | ||
import { useState, useRef, useCallback } from 'react'; | ||
import isEqual from 'fast-deep-equal'; | ||
@@ -48,4 +48,11 @@ import { filterErrors } from './filter-errors/filter-errors.js'; | ||
const [errors, _setErrors] = useState(filterErrors(initialErrors)); | ||
const _dirtyValues = useRef(initialValues); | ||
const _setDirtyValues = (_values) => { | ||
_dirtyValues.current = _values; | ||
}; | ||
const resetTouched = useCallback(() => setTouched({}), []); | ||
const resetDirty = useCallback(() => setDirty({}), []); | ||
const resetDirty = (_values) => { | ||
_setDirtyValues(_values || values); | ||
setDirty({}); | ||
}; | ||
const setErrors = useCallback((errs) => _setErrors((current) => filterErrors(typeof errs === "function" ? errs(current) : errs)), []); | ||
@@ -56,3 +63,3 @@ const clearErrors = useCallback(() => _setErrors({}), []); | ||
clearErrors(); | ||
resetDirty(); | ||
resetDirty(initialValues); | ||
resetTouched(); | ||
@@ -72,3 +79,3 @@ }, []); | ||
_setValues((current) => { | ||
const initialValue = getPath(path, initialValues); | ||
const initialValue = getPath(path, _dirtyValues.current); | ||
const isFieldDirty = !isEqual(initialValue, value); | ||
@@ -138,2 +145,3 @@ setDirty((currentDirty) => __spreadProps(__spreadValues({}, currentDirty), { [path]: isFieldDirty })); | ||
const isTouched = useCallback((path) => getStatus(touched, path), [touched]); | ||
const isValid = useCallback((path) => path ? !validateFieldValue(path, rules, values).hasError : !validateValues(rules, values).hasErrors, [values, rules]); | ||
return { | ||
@@ -162,3 +170,4 @@ values, | ||
resetTouched, | ||
resetDirty | ||
resetDirty, | ||
isValid | ||
}; | ||
@@ -165,0 +174,0 @@ } |
@@ -18,3 +18,3 @@ import { filterErrors } from '../filter-errors/filter-errors.js'; | ||
if (typeof rule === "function") { | ||
acc[rulePath] = rule(value, values); | ||
acc[rulePath] = rule(value, values, rulePath); | ||
} | ||
@@ -21,0 +21,0 @@ if (typeof rule === "object" && Array.isArray(value)) { |
@@ -17,3 +17,3 @@ /// <reference types="react" /> | ||
} | ||
declare type Rule<Value, Values> = (value: Value, values: Values) => React.ReactNode; | ||
declare type Rule<Value, Values> = (value: Value, values: Values, path: string) => React.ReactNode; | ||
declare type FormRule<Value, Values> = Value extends Array<infer ListValue> ? Partial<{ | ||
@@ -49,2 +49,4 @@ [Key in keyof ListValue]: ListValue[Key] extends Array<infer NestedListItem> ? FormRulesRecord<NestedListItem> | Rule<ListValue[Key], Values> : FormRulesRecord<ListValue[Key]> | Rule<ListValue[Key], Values>; | ||
export declare type ResetStatus = () => void; | ||
export declare type ResetDirty<Values> = (values?: Values) => void; | ||
export declare type IsValid<Values> = <Field extends LooseKeys<Values>>(path?: Field) => boolean; | ||
export interface UseFormInput<Values> { | ||
@@ -82,5 +84,6 @@ initialValues?: Values; | ||
resetTouched: ResetStatus; | ||
resetDirty: ResetStatus; | ||
resetDirty: ResetDirty<Values>; | ||
isValid: IsValid<Values>; | ||
} | ||
export {}; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "@mantine/form", | ||
"description": "Mantine form management library", | ||
"version": "5.1.7", | ||
"version": "5.2.0-alpha.0", | ||
"main": "cjs/index.js", | ||
@@ -6,0 +6,0 @@ "module": "esm/index.js", |
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
128757
1091
1