diff --git a/frontend/src/components/common/fields/username-field.tsx b/frontend/src/components/common/fields/username-field.tsx index 917b909fb..175d1433d 100644 --- a/frontend/src/components/common/fields/username-field.tsx +++ b/frontend/src/components/common/fields/username-field.tsx @@ -4,42 +4,38 @@ * SPDX-License-Identifier: AGPL-3.0-only */ import type { CommonFieldProps } from './fields' -import React, { useMemo } from 'react' +import React from 'react' import { Form } from 'react-bootstrap' -import { Trans, useTranslation } from 'react-i18next' +import { useTranslation } from 'react-i18next' + +export interface UsernameFieldProps extends CommonFieldProps { + isInvalid?: boolean + isValid?: boolean +} /** * Renders an input field for the username when registering. * - * @param onChange Hook that is called when the entered username changes. + * @param onChange Callback that is called when the entered username changes. * @param value The currently entered username. + * @param isValid Is a valid field or not + * @param isInvalid Adds error style to label */ -export const UsernameField: React.FC = ({ onChange, value }) => { +export const UsernameField: React.FC = ({ onChange, value, isValid, isInvalid }) => { const { t } = useTranslation() - const isValid = useMemo(() => { - return value?.trim() !== '' - }, [value]) - return ( - - - - - - - - - + ) } diff --git a/frontend/src/components/common/fields/username-label-field.tsx b/frontend/src/components/common/fields/username-label-field.tsx new file mode 100644 index 000000000..675e89222 --- /dev/null +++ b/frontend/src/components/common/fields/username-label-field.tsx @@ -0,0 +1,32 @@ +/* + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { UsernameField } from './username-field' +import type { UsernameFieldProps } from './username-field' +import React from 'react' +import { Form } from 'react-bootstrap' +import { Trans } from 'react-i18next' + +/** + * Wraps and contains label and info for UsernameField + * + * @param onChange Callback that is called when the entered username changes. + * @param value The currently entered username. + * @param isValid Is a valid field or not + * @param isInvalid Adds error style to label + */ +export const UsernameLabelField: React.FC = (props) => { + return ( + + + + + + + + + + ) +} diff --git a/frontend/src/components/login-page/auth/fields/username-field.tsx b/frontend/src/components/login-page/auth/fields/username-field.tsx deleted file mode 100644 index 0179e1362..000000000 --- a/frontend/src/components/login-page/auth/fields/username-field.tsx +++ /dev/null @@ -1,39 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) - * - * SPDX-License-Identifier: AGPL-3.0-only - */ -import type { AuthFieldProps } from './fields' -import React from 'react' -import { Form } from 'react-bootstrap' -import { useTranslation } from 'react-i18next' - -export interface UsernameFieldProps extends AuthFieldProps { - value: string -} - -//TODO: This should be replaced with the common username component. See https://github.com/hedgedoc/hedgedoc/issues/4128 -/** - * Renders an input field for a username. - * - * @param onChange Hook that is called when the input is changed. - * @param invalid True indicates that the username is invalid, false otherwise. - * @param value the username value - */ -export const UsernameField: React.FC = ({ onChange, invalid, value }) => { - const { t } = useTranslation() - - return ( - - - - ) -} diff --git a/frontend/src/components/login-page/auth/via-ldap.tsx b/frontend/src/components/login-page/auth/via-ldap.tsx index 3b28d99e5..d3dcc1a72 100644 --- a/frontend/src/components/login-page/auth/via-ldap.tsx +++ b/frontend/src/components/login-page/auth/via-ldap.tsx @@ -6,8 +6,8 @@ import { doLdapLogin } from '../../../api/auth/ldap' import { useLowercaseOnInputChange } from '../../../hooks/common/use-lowercase-on-input-change' import { useOnInputChange } from '../../../hooks/common/use-on-input-change' +import { UsernameField } from '../../common/fields/username-field' import { PasswordField } from './fields/password-field' -import { UsernameField } from './fields/username-field' import { fetchAndSetUser } from './utils' import type { FormEvent } from 'react' import React, { useCallback, useState } from 'react' @@ -49,7 +49,7 @@ export const ViaLdap: React.FC = ({ providerName, identifier }) =>
- + diff --git a/frontend/src/components/login-page/auth/via-local.tsx b/frontend/src/components/login-page/auth/via-local.tsx index 29aef0ffa..e506aa129 100644 --- a/frontend/src/components/login-page/auth/via-local.tsx +++ b/frontend/src/components/login-page/auth/via-local.tsx @@ -7,10 +7,10 @@ import { doLocalLogin } from '../../../api/auth/local' import { ErrorToI18nKeyMapper } from '../../../api/common/error-to-i18n-key-mapper' import { useLowercaseOnInputChange } from '../../../hooks/common/use-lowercase-on-input-change' import { useOnInputChange } from '../../../hooks/common/use-on-input-change' +import { UsernameField } from '../../common/fields/username-field' import { useFrontendConfig } from '../../common/frontend-config-context/use-frontend-config' import { ShowIf } from '../../common/show-if/show-if' import { PasswordField } from './fields/password-field' -import { UsernameField } from './fields/username-field' import { fetchAndSetUser } from './utils' import Link from 'next/link' import type { FormEvent } from 'react' @@ -55,7 +55,7 @@ export const ViaLocal: React.FC = () => { - + diff --git a/frontend/src/pages/register.tsx b/frontend/src/pages/register.tsx index a95080d55..ce6f988e2 100644 --- a/frontend/src/pages/register.tsx +++ b/frontend/src/pages/register.tsx @@ -8,7 +8,7 @@ import type { ApiError } from '../api/common/api-error' import { DisplayNameField } from '../components/common/fields/display-name-field' import { NewPasswordField } from '../components/common/fields/new-password-field' import { PasswordAgainField } from '../components/common/fields/password-again-field' -import { UsernameField } from '../components/common/fields/username-field' +import { UsernameLabelField } from '../components/common/fields/username-label-field' import { useFrontendConfig } from '../components/common/frontend-config-context/use-frontend-config' import { Redirect } from '../components/common/redirect' import { LandingLayout } from '../components/landing-layout/landing-layout' @@ -63,6 +63,8 @@ export const RegisterPage: NextPage = () => { return error?.backendErrorName === 'PasswordTooWeakError' }, [error]) + const isValidUsername = useMemo(() => Boolean(username.trim()), [username]) + const onUsernameChange = useLowercaseOnInputChange(setUsername) const onDisplayNameChange = useOnInputChange(setDisplayName) const onPasswordChange = useOnInputChange(setPassword) @@ -87,7 +89,7 @@ export const RegisterPage: NextPage = () => { - +