-
Notifications
You must be signed in to change notification settings - Fork 2
/
TextInputValidation.tsx
140 lines (123 loc) · 4.24 KB
/
TextInputValidation.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import * as React from "react";
import { useCallback, useMemo, useState } from "react";
import { AccessibilityInfo, View } from "react-native";
import Animated from "react-native-reanimated";
import { IOColors } from "../../core/IOColors";
import {
enterTransitionInputIcon,
exitTransitionInputIcon
} from "../../core/IOTransitions";
import { triggerHaptic } from "../../functions";
import { IOIconSizeScale, IOIcons, Icon } from "../icons";
import { TextInputBase } from "./TextInputBase";
export type ValidationWithOptions = { isValid: boolean; errorMessage: string };
type TextInputValidationProps = Omit<
React.ComponentProps<typeof TextInputBase>,
"rightElement" | "status" | "bottomMessageColor" | "isPassword"
> & {
/**
* This function can return either a `boolean` or a `ValidationWithOptions` object.
* If a `boolean` is returned and the field is not valid, the value of the errorMessage prop will be displayed/announced.
* If a `ValidationWithOptions` object is returned and the field is not valid, the value displayed/announced will be the one contained within this object.
*/
onValidate: (value: string) => boolean | ValidationWithOptions;
/**
* In case of a dynamic `errorMessage`, use the `onValidate` function with a `ValidationWithOptions` object as the return value to ensure that screen readers announce the correct value.
*/
errorMessage: string;
};
function isValidationWithOptions(validation: boolean | ValidationWithOptions): validation is ValidationWithOptions {
return typeof validation === 'object' && 'isValid' in validation && 'errorMessage' in validation;
}
const feedbackIconSize: IOIconSizeScale = 24;
export const TextInputValidation = ({
onValidate,
errorMessage,
value,
bottomMessage,
onBlur,
onFocus,
...props
}: TextInputValidationProps) => {
const [isValid, setIsValid] = useState<boolean | undefined>(undefined);
const [errMessage, setErrMessage] = useState(errorMessage);
const getErrorFeedback = useCallback((isValid: boolean, message: string) => {
setIsValid(isValid);
setErrMessage(message);
if (!isValid) {
triggerHaptic("notificationError");
AccessibilityInfo.announceForAccessibilityWithOptions(message, {
queue: true
});
} else {
triggerHaptic("notificationSuccess");
}
}, []);
const onBlurHandler = useCallback(() => {
const validation = onValidate(value);
if (isValidationWithOptions(validation)) {
getErrorFeedback(validation.isValid, validation.errorMessage);
} else {
getErrorFeedback(validation, errorMessage);
}
onBlur?.();
}, [value, errorMessage, onBlur, onValidate, getErrorFeedback]);
const onFocusHandler = useCallback(() => {
setIsValid(undefined);
onFocus?.();
}, [onFocus]);
const labelError = useMemo(
() => (isValid === false && errMessage ? errMessage : bottomMessage),
[isValid, errMessage, bottomMessage]
);
const labelErrorColor: IOColors | undefined = useMemo(
() => (isValid === false && errMessage ? "error-600" : undefined),
[isValid, errMessage]
);
const feedbackIconAttrMap: Record<
string,
{ name: IOIcons; color: IOColors }
> = useMemo(
() => ({
valid: {
name: "success",
color: "green"
},
notValid: {
name: "errorFilled",
color: "error-600"
}
}),
[]
);
const feedbackIcon = useMemo(() => {
const validationStatus = isValid ? "valid" : "notValid";
return isValid !== undefined ? (
<Animated.View
entering={enterTransitionInputIcon}
exiting={exitTransitionInputIcon}
>
<Icon
name={feedbackIconAttrMap[validationStatus].name}
color={feedbackIconAttrMap[validationStatus].color}
size={feedbackIconSize}
/>
</Animated.View>
) : (
<View style={{ width: feedbackIconSize, height: feedbackIconSize }} />
);
}, [feedbackIconAttrMap, isValid]);
return (
<TextInputBase
{...props}
value={value}
status={isValid === false ? "error" : undefined}
bottomMessage={labelError}
bottomMessageColor={labelErrorColor}
rightElement={feedbackIcon}
onBlur={onBlurHandler}
onFocus={onFocusHandler}
/>
);
};
export default TextInputValidation;