-
Notifications
You must be signed in to change notification settings - Fork 54
/
Validation.stories.tsx
186 lines (167 loc) · 4.04 KB
/
Validation.stories.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { PhoneNumberUtil } from 'google-libphonenumber';
const phoneUtil = PhoneNumberUtil.getInstance();
const validatePhone = (phone: string) => {
try {
return phoneUtil.isValidNumber(phoneUtil.parseAndKeepRawInput(phone));
} catch (error) {
return false;
}
};
import { useFormik } from 'formik';
import React, { useEffect, useState } from 'react';
import { Controller, useForm } from 'react-hook-form';
import * as Yup from 'yup';
import { PhoneInput } from '../index';
const ValidationResult: React.FC<{
isValid: boolean;
}> = ({ isValid }) => {
return (
<code style={{ color: isValid === false ? 'red' : 'green' }}>
isValid: {String(isValid)}
</code>
);
};
export const Default = () => {
const [phone, setPhone] = useState('');
const isValid = validatePhone(phone);
return (
<div>
<p>phone: {phone}</p>
<PhoneInput
defaultCountry="ua"
value={phone}
onChange={(phone) => {
setPhone(phone);
}}
name="phone"
/>
<ValidationResult isValid={isValid} />
</div>
);
};
export const Prefilled = () => {
const [phone, setPhone] = useState('+12049999999');
const isValid = validatePhone(phone);
return (
<div>
<p>phone: {phone}</p>
<PhoneInput
defaultCountry="ua"
value={phone}
onChange={(phone) => {
setPhone(phone);
}}
name="phone"
/>
<ValidationResult isValid={isValid} />
</div>
);
};
export const ReactHookForm = () => {
const {
control,
trigger,
formState: { errors },
} = useForm({
defaultValues: { phone: '+1201' },
mode: 'onChange',
});
// Optional: validate on mount
useEffect(() => {
trigger();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return (
<div>
<Controller
name="phone"
control={control}
render={({ field }) => (
<div>
<p>phone: {field.value}</p>
<PhoneInput
defaultCountry="us"
value={field.value}
onChange={field.onChange}
name={field.name}
onBlur={field.onBlur}
/>
</div>
)}
rules={{
validate: (phone) => validatePhone(phone),
}}
/>
<ValidationResult isValid={!errors.phone} />
</div>
);
};
export const Formik = () => {
const { values, errors, setFieldTouched, setFieldValue } = useFormik({
initialValues: {
phone: '+1204',
},
onSubmit: () => undefined,
validateOnChange: true,
validateOnMount: true,
validate: ({ phone }) => {
const errors: Record<string, string> = {};
const isValid = validatePhone(phone);
if (!isValid) {
errors.phone = 'Phone is not valid';
}
return errors;
},
});
return (
<div>
<p>phone: {values.phone}</p>
<PhoneInput
defaultCountry="us"
value={values.phone}
onChange={(newPhone) => {
setFieldTouched('phone');
setFieldValue('phone', newPhone);
}}
name="phone"
/>
<ValidationResult isValid={!errors.phone} />
</div>
);
};
const formValidationSchema = Yup.object().shape({
phone: Yup.string()
.required('Required')
.test('phone', 'Phone number is invalid', (value) =>
validatePhone(value ?? ''),
),
});
export const FormikWithYup = () => {
const { values, errors, setFieldTouched, setFieldValue } = useFormik({
initialValues: {
phone: '+1205',
},
onSubmit: () => undefined,
validateOnChange: true,
validateOnMount: true,
validationSchema: formValidationSchema,
});
return (
<div>
<p>phone: {values.phone}</p>
<PhoneInput
defaultCountry="us"
value={values.phone}
onChange={(newPhone) => {
setFieldTouched('phone');
setFieldValue('phone', newPhone);
}}
name="phone"
/>
<ValidationResult isValid={!errors.phone} />
</div>
);
};
export default {
title: 'Validation',
};