Skip to content

feat: add prop onValidateFailed to TextField #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/incubator/TextField/textField.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
"type": "(isValid: boolean) => void",
"description": "Callback for when field validity has changed"
},
{
"name": "onValidationFailed",
"type": "(failedValidatorIndex: number) => void",
"description": "Callback for when field validated and failed"
},
{
"name": "fieldStyle",
"type": "ViewStyle | (context: FieldContextType, props) => ViewStyle",
Expand Down
8 changes: 8 additions & 0 deletions src/incubator/TextField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ export interface FieldStateProps extends InputProps {
validateOnStart?: boolean;
validateOnChange?: boolean;
validateOnBlur?: boolean;
/**
* Callback for when field validated and failed
*/
onValidationFailed?: (failedValidatorIndex: number) => void;
/**
* A single or multiple validator. Can be a string (required, email) or custom function.
*/
Expand Down Expand Up @@ -203,6 +207,10 @@ export type TextFieldProps = MarginModifiers &
* Should validate when losing focus of TextField
*/
validateOnBlur?: boolean;
/**
* Callback for when field validated and failed
*/
onValidationFailed?: (failedValidatorIndex: number) => void;
/**
* Callback for when field validity has changed
*/
Expand Down
7 changes: 6 additions & 1 deletion src/incubator/TextField/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function useFieldState({
validateOnBlur,
validateOnChange,
validateOnStart,
onValidationFailed,
onChangeValidity,
...props
}: FieldStateProps) {
Expand Down Expand Up @@ -68,9 +69,13 @@ export default function useFieldState({
setIsValid(_isValid);
setFailingValidatorIndex(_failingValidatorIndex);

if (!_isValid && !_.isUndefined(_failingValidatorIndex)) {
onValidationFailed?.(_failingValidatorIndex);
}

return _isValid;
},
[value, validate]);
[value, validate, onValidationFailed]);

const onFocus = useCallback((...args: any) => {
setIsFocused(true);
Expand Down