add eslint / prettier
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-02-23 14:18:51 +01:00
parent 4d1d2e6ed8
commit 33d1425fbb
24 changed files with 1294 additions and 398 deletions

View File

@@ -28,7 +28,7 @@ function hasErrors<TValues>(errors: FieldErrors<TValues>): boolean {
function pickTouchedErrors<TValues>(
errors: FieldErrors<TValues>,
touched: TouchedFields<TValues>
touched: TouchedFields<TValues>,
): FieldErrors<TValues> {
const next: FieldErrors<TValues> = {};
@@ -53,75 +53,80 @@ function touchAll<TValues extends Record<string, string>>(values: TValues): Touc
export function useValidatedFields<TValues extends Record<string, string>>({
initialValues,
validate
validate,
}: UseValidatedFieldsOptions<TValues>) {
const [values, setValues] = useState<TValues>(initialValues);
const [allErrors, setAllErrors] = useState<FieldErrors<TValues>>(() => validate(initialValues));
const [touched, setTouched] = useState<TouchedFields<TValues>>({});
const updateValues = useCallback((nextValues: TValues, options: SetValuesOptions = {}) => {
const { validate: shouldValidate = false, clearErrors = false } = options;
setValues(nextValues);
const updateValues = useCallback(
(nextValues: TValues, options: SetValuesOptions = {}) => {
const { validate: shouldValidate = false, clearErrors = false } = options;
setValues(nextValues);
if (shouldValidate || clearErrors) {
setAllErrors(validate(nextValues));
}
if (clearErrors) {
setTouched({});
}
}, [validate]);
const setFieldValue = useCallback(<K extends keyof TValues>(
key: K,
value: TValues[K],
options: SetFieldValueOptions = {}
) => {
const { validate: shouldValidate = true, touch = true } = options;
if (touch) {
setTouched((current) => ({
...current,
[key]: true
}));
}
setValues((current) => {
const nextValues = {
...current,
[key]: value
};
if (shouldValidate) {
if (shouldValidate || clearErrors) {
setAllErrors(validate(nextValues));
}
return nextValues;
});
}, [validate]);
if (clearErrors) {
setTouched({});
}
},
[validate],
);
const validateAll = useCallback((options: ValidateAllOptions = {}) => {
const { touchAll: shouldTouchAll = true } = options;
const nextErrors = validate(values);
const setFieldValue = useCallback(
<K extends keyof TValues>(key: K, value: TValues[K], options: SetFieldValueOptions = {}) => {
const { validate: shouldValidate = true, touch = true } = options;
setAllErrors(nextErrors);
if (touch) {
setTouched((current) => ({
...current,
[key]: true,
}));
}
if (shouldTouchAll) {
setTouched(touchAll(values));
}
setValues((current) => {
const nextValues = {
...current,
[key]: value,
};
return nextErrors;
}, [validate, values]);
if (shouldValidate) {
setAllErrors(validate(nextValues));
}
return nextValues;
});
},
[validate],
);
const validateAll = useCallback(
(options: ValidateAllOptions = {}) => {
const { touchAll: shouldTouchAll = true } = options;
const nextErrors = validate(values);
setAllErrors(nextErrors);
if (shouldTouchAll) {
setTouched(touchAll(values));
}
return nextErrors;
},
[validate, values],
);
const setFieldError = useCallback(<K extends keyof TValues>(key: K, message?: string) => {
setTouched((current) => ({
...current,
[key]: true
[key]: true,
}));
setAllErrors((current) => ({
...current,
[key]: message
[key]: message,
}));
}, []);
@@ -136,7 +141,7 @@ export function useValidatedFields<TValues extends Record<string, string>>({
setTouched((current) => ({
...current,
...nextTouched
...nextTouched,
}));
setAllErrors(nextErrors);
}, []);
@@ -161,6 +166,6 @@ export function useValidatedFields<TValues extends Record<string, string>>({
validateAll,
setFieldError,
setErrors: updateErrors,
clearErrors
clearErrors,
};
}