Login form
Email and password with validators and submitting state
import { Anchor, Button, Checkbox, Group, PasswordInput, Stack, TextInput } from '@mantine/core';
import { FormErrors, hasLength, isEmail, useForm } from '@mantine/form';
import { notifications } from '@mantine/notifications';
interface LoginValues {
email: string;
password: string;
rememberMe: boolean;
}
function login(values: LoginValues) {
return new Promise<LoginValues>((resolve) => {
setTimeout(() => resolve(values), 1500);
});
}
export function LoginForm() {
const form = useForm<LoginValues>({
initialValues: {
email: '',
password: '',
rememberMe: false,
},
validate: {
email: isEmail('Invalid email'),
password: hasLength({ min: 8 }, 'Password must be at least 8 characters'),
},
});
const handleSubmit = async (values: LoginValues) => {
const user = await login(values);
notifications.show({
title: 'Signed in',
message: `Welcome back, ${user.email}${user.rememberMe ? ', you will stay signed in on this device' : ''}`,
});
};
const handleErrors = (errors: FormErrors) => {
form.focusInput(Object.keys(errors)[0]);
};
return (
<form onSubmit={form.onSubmit(handleSubmit, handleErrors)}>
<Stack maw={420} mx="auto">
<TextInput
size="md"
label="Email"
placeholder="you@mantine.dev"
withAsterisk
{...form.getInputProps('email')}
/>
<PasswordInput
size="md"
label="Password"
placeholder="Your password"
withAsterisk
{...form.getInputProps('password')}
/>
<Group justify="space-between">
<Checkbox
size="md"
label="Remember me"
{...form.getInputProps('rememberMe', { type: 'checkbox' })}
/>
<Anchor component="button" type="button" size="md">
Forgot password?
</Anchor>
</Group>
<Group justify="flex-end">
<Button size="md" type="submit" loading={form.submitting}>
Sign in
</Button>
</Group>
</Stack>
</form>
);
}