Skip to content

Commit 294898d

Browse files
committed
fix(forms): only call onChange after the form was modified by the user
1 parent 739d4d5 commit 294898d

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/forms/FormAutocomplete.jsx

+14-13
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,24 @@ export function FormAutocomplete({
3030
const { isOpen, open, close } = useOpenState();
3131
const [ignoreBlur, setIgnoreBlur] = useState(false);
3232
const [isFocused, setFocus] = useState(false);
33-
const registerRef = useCallback(register, [register]);
3433
const searchInputRef = useRef(null);
3534

35+
const registerRef = useCallback(register, [register]);
3636
const disabled = normalizeDisabled(_disabled, getFormData());
3737

3838
const controlFeedback = getFormSubmitedAttempted() ? (isValid() ? 'is-valid' : 'is-invalid') : '';
3939

40+
const updateSearchInputValidation = useCallback(() => {
41+
searchInputRef.current.setCustomValidity(controlFeedback === 'is-invalid' ? 'invalid' : '');
42+
}, [controlFeedback]);
43+
44+
const clearSearchValue = useCallback(() => {
45+
if (isEmpty(value) && !isFocused) {
46+
setSearchValue('');
47+
setSelectedItem(null);
48+
}
49+
}, [isFocused, value]);
50+
4051
const onSearchInputType = useCallback(
4152
(_, nextSearchValue) => {
4253
setSearchValue(nextSearchValue);
@@ -70,6 +81,7 @@ export function FormAutocomplete({
7081
if (isEmpty(searchValue) && value) {
7182
setValue('');
7283
setSelectedItem(null);
84+
updateSearchInputValidation();
7385
}
7486

7587
if (ignoreBlur) {
@@ -78,7 +90,7 @@ export function FormAutocomplete({
7890
close();
7991
setFocus(false);
8092
}
81-
}, [close, ignoreBlur, searchValue, setValue, value]);
93+
}, [close, ignoreBlur, searchValue, setValue, updateSearchInputValidation, value]);
8294

8395
const enableSearchInput = useCallback(() => {
8496
if (disabled) {
@@ -105,17 +117,6 @@ export function FormAutocomplete({
105117
[close, setValue]
106118
);
107119

108-
const updateSearchInputValidation = useCallback(() => {
109-
searchInputRef.current.setCustomValidity(controlFeedback === 'is-invalid' ? 'invalid' : '');
110-
}, [controlFeedback]);
111-
112-
const clearSearchValue = useCallback(() => {
113-
if (isEmpty(value) && !isFocused) {
114-
setSearchValue('');
115-
setSelectedItem(null);
116-
}
117-
}, [isFocused, value]);
118-
119120
useEffect(updateSearchInputValidation, [updateSearchInputValidation]);
120121
useEffect(clearSearchValue, [clearSearchValue]);
121122

src/forms/helpers/useForm.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { debounce } from 'lodash-es';
66

77
function useFormState(initialState, { onChange, transform }) {
88
const [formState, setFormState] = useState(initialState);
9+
const [isDirty, setIsDirty] = useState(false);
910
const onChangeRef = useRef(debounce(onChange, 1000));
1011
const transformRef = useRef(
1112
debounce(
@@ -18,14 +19,17 @@ function useFormState(initialState, { onChange, transform }) {
1819
);
1920

2021
useEffect(() => {
21-
onChangeRef.current(formState);
22-
}, [formState]);
22+
if (isDirty) {
23+
onChangeRef.current(formState);
24+
}
25+
}, [formState, isDirty]);
2326

2427
return {
2528
getState() {
2629
return formState;
2730
},
2831
updateState(name, value) {
32+
setIsDirty(true);
2933
setFormState((prevFormState) => {
3034
const nextFormState = nextState(prevFormState, name, value);
3135

@@ -37,6 +41,7 @@ function useFormState(initialState, { onChange, transform }) {
3741
});
3842
},
3943
resetState() {
44+
setIsDirty(false);
4045
setFormState(initialState);
4146
},
4247
};

0 commit comments

Comments
 (0)