Skip to content

Commit

Permalink
[@mantine/form] Fix onValuesChange not using updated function (#5901)
Browse files Browse the repository at this point in the history
  • Loading branch information
rtivital committed Mar 12, 2024
1 parent 08b0f53 commit 52e8469
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions packages/@mantine/form/src/use-form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,42 +128,48 @@ export function useForm<
[]
);

const setFieldValue: SetFieldValue<Values> = useCallback((path, payload) => {
const shouldValidate = shouldValidateOnChange(path, validateInputOnChange);
clearFieldDirty(path);
setTouched((currentTouched) => ({ ...currentTouched, [path]: true }));
_setValues((current) => {
const currentValue = getPath(path, current) as PathValue<Values, typeof path>;
const result = setPath(
path,
payload instanceof Function ? payload(currentValue) : payload,
current
);

if (shouldValidate) {
const validationResults = validateFieldValue(path, rules, result);
validationResults.hasError
? setFieldError(path, validationResults.error)
: clearFieldError(path);
}
const setFieldValue: SetFieldValue<Values> = useCallback(
(path, payload) => {
const shouldValidate = shouldValidateOnChange(path, validateInputOnChange);
clearFieldDirty(path);
setTouched((currentTouched) => ({ ...currentTouched, [path]: true }));
_setValues((current) => {
const currentValue = getPath(path, current) as PathValue<Values, typeof path>;
const result = setPath(
path,
payload instanceof Function ? payload(currentValue) : payload,
current
);

if (shouldValidate) {
const validationResults = validateFieldValue(path, rules, result);
validationResults.hasError
? setFieldError(path, validationResults.error)
: clearFieldError(path);
}

onValuesChange?.(result, current);
onValuesChange?.(result, current);

return result;
});
return result;
});

!shouldValidate && clearInputErrorOnChange && setFieldError(path, null);
}, []);
!shouldValidate && clearInputErrorOnChange && setFieldError(path, null);
},
[onValuesChange]
);

const setValues: SetValues<Values> = useCallback((payload) => {
_setValues((currentValues) => {
const valuesPartial = payload instanceof Function ? payload(currentValues) : payload;
const result = { ...currentValues, ...valuesPartial };
onValuesChange?.(result, currentValues);
return result;
});
clearInputErrorOnChange && clearErrors();
}, []);
const setValues: SetValues<Values> = useCallback(
(payload) => {
_setValues((currentValues) => {
const valuesPartial = payload instanceof Function ? payload(currentValues) : payload;
const result = { ...currentValues, ...valuesPartial };
onValuesChange?.(result, currentValues);
return result;
});
clearInputErrorOnChange && clearErrors();
},
[onValuesChange]
);

const reorderListItem: ReorderListItem<Values> = useCallback((path, payload) => {
clearFieldDirty(path);
Expand Down

0 comments on commit 52e8469

Please sign in to comment.