Skip to content

Commit 9ab44a9

Browse files
authored
feat: add prop onValidateFailed to TextField (#2626)
* feat: add prop onValidateFailed to TextField * code-review
1 parent ba4124d commit 9ab44a9

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/incubator/TextField/textField.api.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
"type": "(isValid: boolean) => void",
5252
"description": "Callback for when field validity has changed"
5353
},
54+
{
55+
"name": "onValidationFailed",
56+
"type": "(failedValidatorIndex: number) => void",
57+
"description": "Callback for when field validated and failed"
58+
},
5459
{
5560
"name": "fieldStyle",
5661
"type": "ViewStyle | (context: FieldContextType, props) => ViewStyle",

src/incubator/TextField/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ export interface FieldStateProps extends InputProps {
3636
validateOnStart?: boolean;
3737
validateOnChange?: boolean;
3838
validateOnBlur?: boolean;
39+
/**
40+
* Callback for when field validated and failed
41+
*/
42+
onValidationFailed?: (failedValidatorIndex: number) => void;
3943
/**
4044
* A single or multiple validator. Can be a string (required, email) or custom function.
4145
*/
@@ -203,6 +207,10 @@ export type TextFieldProps = MarginModifiers &
203207
* Should validate when losing focus of TextField
204208
*/
205209
validateOnBlur?: boolean;
210+
/**
211+
* Callback for when field validated and failed
212+
*/
213+
onValidationFailed?: (failedValidatorIndex: number) => void;
206214
/**
207215
* Callback for when field validity has changed
208216
*/

src/incubator/TextField/useFieldState.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export default function useFieldState({
1111
validateOnBlur,
1212
validateOnChange,
1313
validateOnStart,
14+
onValidationFailed,
1415
onChangeValidity,
1516
...props
1617
}: FieldStateProps) {
@@ -68,9 +69,13 @@ export default function useFieldState({
6869
setIsValid(_isValid);
6970
setFailingValidatorIndex(_failingValidatorIndex);
7071

72+
if (!_isValid && !_.isUndefined(_failingValidatorIndex)) {
73+
onValidationFailed?.(_failingValidatorIndex);
74+
}
75+
7176
return _isValid;
7277
},
73-
[value, validate]);
78+
[value, validate, onValidationFailed]);
7479

7580
const onFocus = useCallback((...args: any) => {
7681
setIsFocused(true);

0 commit comments

Comments
 (0)