Skip to content

Commit c6f7e22

Browse files
authored
Revalidate field when value programmatically changed (#1503)
1 parent f0d37aa commit c6f7e22

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/incubator/TextField/useFieldState.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {useDidUpdate} from 'hooks';
55
import {Validator} from './types';
66
import {InputProps} from './Input';
77

8-
98
export interface FieldStateProps extends InputProps {
109
validateOnStart?: boolean;
1110
validateOnChange?: boolean;
@@ -17,7 +16,7 @@ export interface FieldStateProps extends InputProps {
1716
/**
1817
* Callback for when field validity has changed
1918
*/
20-
onChangeValidity?: (isValid: boolean) => void
19+
onChangeValidity?: (isValid: boolean) => void;
2120
}
2221

2322
export default function useFieldState({
@@ -41,10 +40,16 @@ export default function useFieldState({
4140

4241
useEffect(() => {
4342
if (props.value !== value) {
43+
4444
setValue(props.value);
45+
46+
if (validateOnChange) {
47+
validateField(props.value);
48+
}
4549
}
46-
/* On purpose listen only to props.value change */
47-
}, [props.value]);
50+
/* On purpose listen only to props.value change */
51+
/* eslint-disable-next-line react-hooks/exhaustive-deps*/
52+
}, [props.value, validateOnChange]);
4853

4954
useDidUpdate(() => {
5055
onChangeValidity?.(isValid);

0 commit comments

Comments
 (0)