|
1 | 1 | 'use client'
|
2 | 2 |
|
3 |
| -import type { LoginWithUsernameOptions } from 'payload' |
| 3 | +import type { FieldPermissions, LoginWithUsernameOptions } from 'payload' |
4 | 4 |
|
5 |
| -import { EmailField, TextField, useTranslation } from '@payloadcms/ui' |
| 5 | +import { EmailField, RenderFields, TextField, useTranslation } from '@payloadcms/ui' |
6 | 6 | import { email, username } from 'payload/shared'
|
7 | 7 | import React from 'react'
|
8 | 8 |
|
9 | 9 | type Props = {
|
10 | 10 | loginWithUsername?: LoginWithUsernameOptions | false
|
11 | 11 | }
|
12 |
| -export const EmailAndUsernameFields: React.FC<Props> = ({ loginWithUsername }) => { |
| 12 | +function EmailFieldComponent(props: Props) { |
| 13 | + const { loginWithUsername } = props |
13 | 14 | const { t } = useTranslation()
|
14 | 15 |
|
15 | 16 | const requireEmail = !loginWithUsername || (loginWithUsername && loginWithUsername.requireEmail)
|
16 |
| - const requireUsername = loginWithUsername && loginWithUsername.requireUsername |
17 | 17 | const showEmailField =
|
18 | 18 | !loginWithUsername || loginWithUsername?.requireEmail || loginWithUsername?.allowEmailLogin
|
| 19 | + |
| 20 | + if (showEmailField) { |
| 21 | + return ( |
| 22 | + <EmailField |
| 23 | + autoComplete="off" |
| 24 | + label={t('general:email')} |
| 25 | + name="email" |
| 26 | + path="email" |
| 27 | + required={requireEmail} |
| 28 | + validate={email} |
| 29 | + /> |
| 30 | + ) |
| 31 | + } |
| 32 | + |
| 33 | + return null |
| 34 | +} |
| 35 | + |
| 36 | +function UsernameFieldComponent(props: Props) { |
| 37 | + const { loginWithUsername } = props |
| 38 | + const { t } = useTranslation() |
| 39 | + |
| 40 | + const requireUsername = loginWithUsername && loginWithUsername.requireUsername |
19 | 41 | const showUsernameField = Boolean(loginWithUsername)
|
20 | 42 |
|
| 43 | + if (showUsernameField) { |
| 44 | + return ( |
| 45 | + <TextField |
| 46 | + label={t('authentication:username')} |
| 47 | + name="username" |
| 48 | + path="username" |
| 49 | + required={requireUsername} |
| 50 | + validate={username} |
| 51 | + /> |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + return null |
| 56 | +} |
| 57 | + |
| 58 | +type RenderEmailAndUsernameFieldsProps = { |
| 59 | + className?: string |
| 60 | + loginWithUsername?: LoginWithUsernameOptions | false |
| 61 | + operation?: 'create' | 'update' |
| 62 | + permissions?: { |
| 63 | + [fieldName: string]: FieldPermissions |
| 64 | + } |
| 65 | + readOnly: boolean |
| 66 | +} |
| 67 | +export function RenderEmailAndUsernameFields(props: RenderEmailAndUsernameFieldsProps) { |
| 68 | + const { className, loginWithUsername, operation, permissions, readOnly } = props |
| 69 | + |
21 | 70 | return (
|
22 |
| - <React.Fragment> |
23 |
| - {showEmailField && ( |
24 |
| - <EmailField |
25 |
| - autoComplete="email" |
26 |
| - label={t('general:email')} |
27 |
| - name="email" |
28 |
| - path="email" |
29 |
| - required={requireEmail} |
30 |
| - validate={email} |
31 |
| - /> |
32 |
| - )} |
33 |
| - |
34 |
| - {showUsernameField && ( |
35 |
| - <TextField |
36 |
| - label={t('authentication:username')} |
37 |
| - name="username" |
38 |
| - path="username" |
39 |
| - required={requireUsername} |
40 |
| - validate={username} |
41 |
| - /> |
42 |
| - )} |
43 |
| - </React.Fragment> |
| 71 | + <RenderFields |
| 72 | + className={className} |
| 73 | + fieldMap={[ |
| 74 | + { |
| 75 | + name: 'email', |
| 76 | + type: 'text', |
| 77 | + CustomField: <EmailFieldComponent loginWithUsername={loginWithUsername} />, |
| 78 | + cellComponentProps: null, |
| 79 | + fieldComponentProps: { type: 'email', readOnly }, |
| 80 | + fieldIsPresentational: false, |
| 81 | + isFieldAffectingData: true, |
| 82 | + localized: false, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'username', |
| 86 | + type: 'text', |
| 87 | + CustomField: <UsernameFieldComponent loginWithUsername={loginWithUsername} />, |
| 88 | + cellComponentProps: null, |
| 89 | + fieldComponentProps: { type: 'text', readOnly }, |
| 90 | + fieldIsPresentational: false, |
| 91 | + isFieldAffectingData: true, |
| 92 | + localized: false, |
| 93 | + }, |
| 94 | + ]} |
| 95 | + forceRender |
| 96 | + operation={operation} |
| 97 | + path="" |
| 98 | + permissions={permissions} |
| 99 | + readOnly={readOnly} |
| 100 | + schemaPath="" |
| 101 | + /> |
44 | 102 | )
|
45 | 103 | }
|
0 commit comments