Skip to content

Commit

Permalink
doc(FormItem): add email validation (#7374)
Browse files Browse the repository at this point in the history
Поправил валидацию почты в примере в документации
  • Loading branch information
EldarMuhamethanov authored and actions-user committed Aug 14, 2024
1 parent 3642946 commit ba82eb9
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions packages/vkui/src/components/FormItem/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,40 @@ const addressItems = [
{ label: 'Город', name: 'city' },
];

const ERRORS_MAP = {
empty: 'Пожалуйста, введите электронную почту',
incorrect: 'Электронная почта некорректна',
};

const validateEmail = (email) => {
return email.match(
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
);
};

const Example = () => {
const [email, setEmail] = React.useState('');
const [purpose, setPurpose] = React.useState('');
const [about, setAbout] = React.useState('');
const [emailError, setEmailError] = React.useState('empty');
const [showPatronymic, setShowPatronymic] = React.useState(true);

const updateEmail = (value) => {
setEmail(value);
if (!value) {
setEmailError('empty');
} else if (!validateEmail(value)) {
setEmailError('incorrect');
} else {
setEmailError('');
}
};

const onChange = (e) => {
const { name, value } = e.currentTarget;

const setStateAction = {
email: setEmail,
email: updateEmail,
purpose: setPurpose,
about: setAbout,
}[name];
Expand All @@ -98,10 +121,8 @@ const Example = () => {
<FormItem
htmlFor="email"
top="E-mail"
status={email ? 'valid' : 'error'}
bottom={
email ? 'Электронная почта введена верно!' : 'Пожалуйста, введите электронную почту'
}
status={emailError ? 'error' : 'valid'}
bottom={emailError ? ERRORS_MAP[emailError] : 'Электронная почта введена верно!'}
bottomId="email-type"
required
>
Expand Down

0 comments on commit ba82eb9

Please sign in to comment.