Chore: Replace Form usage in LoginForm.tsx (#81326)

This commit is contained in:
Alex Khomenko 2024-01-26 13:04:44 +01:00 committed by GitHub
parent 3c4cfb1a70
commit 857bce25e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,10 @@
import { css } from '@emotion/css'; import { css } from '@emotion/css';
import React, { ReactElement, useId } from 'react'; import React, { ReactElement, useId } from 'react';
import { useForm } from 'react-hook-form';
import { GrafanaTheme2 } from '@grafana/data'; import { GrafanaTheme2 } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors'; import { selectors } from '@grafana/e2e-selectors';
import { Button, Form, Input, Field, useStyles2 } from '@grafana/ui'; import { Button, Input, Field, useStyles2 } from '@grafana/ui';
import { PasswordField } from '../PasswordField/PasswordField'; import { PasswordField } from '../PasswordField/PasswordField';
@ -21,12 +22,15 @@ export const LoginForm = ({ children, onSubmit, isLoggingIn, passwordHint, login
const styles = useStyles2(getStyles); const styles = useStyles2(getStyles);
const usernameId = useId(); const usernameId = useId();
const passwordId = useId(); const passwordId = useId();
const {
handleSubmit,
register,
formState: { errors },
} = useForm<FormModel>({ mode: 'onChange' });
return ( return (
<div className={styles.wrapper}> <div className={styles.wrapper}>
<Form onSubmit={onSubmit} validateOn="onChange"> <form onSubmit={handleSubmit(onSubmit)}>
{({ register, errors }) => (
<>
<Field label="Email or username" invalid={!!errors.user} error={errors.user?.message}> <Field label="Email or username" invalid={!!errors.user} error={errors.user?.message}>
<Input <Input
{...register('user', { required: 'Email or username is required' })} {...register('user', { required: 'Email or username is required' })}
@ -54,9 +58,7 @@ export const LoginForm = ({ children, onSubmit, isLoggingIn, passwordHint, login
{isLoggingIn ? 'Logging in...' : 'Log in'} {isLoggingIn ? 'Logging in...' : 'Log in'}
</Button> </Button>
{children} {children}
</> </form>
)}
</Form>
</div> </div>
); );
}; };