Skip to content

Commit dbe317b

Browse files
committed
fix: AuthFormShared
1 parent ddb0a0a commit dbe317b

File tree

2 files changed

+61
-85
lines changed

2 files changed

+61
-85
lines changed

src/components/account/AuthFormShared.tsx

Lines changed: 57 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,60 @@
11
import { InputGroup, InputGroupProps2 } from '@blueprintjs/core'
2-
3-
import {
4-
ControllerProps,
5-
FieldValues,
6-
UseControllerProps,
7-
} from 'react-hook-form'
2+
import { ControllerProps, FieldValues, UseControllerProps } from 'react-hook-form'
83
import { useTranslation } from 'react-i18next'
9-
104
import { FormField, FormFieldProps } from 'components/FormField'
115
import { REGEX_EMAIL, REGEX_USERNAME } from 'utils/regexes'
126

137
export type RuleKeys = 'email' | 'password' | 'username' | 'registertoken'
148

15-
export function useAuthRules() {
16-
const { t } = useTranslation()
17-
18-
return {
19-
email: {
20-
required: t('components.account.AuthFormShared.email_required'),
21-
pattern: {
22-
value: REGEX_EMAIL,
23-
message: t('components.account.AuthFormShared.email_invalid'),
24-
},
25-
},
26-
password: {
27-
required: t('components.account.AuthFormShared.password_required'),
28-
minLength: {
29-
value: 8,
30-
message: t('components.account.AuthFormShared.password_min_length'),
31-
},
32-
maxLength: {
33-
value: 32,
34-
message: t('components.account.AuthFormShared.password_max_length'),
35-
},
36-
},
37-
username: {
38-
required: t('components.account.AuthFormShared.username_required'),
39-
minLength: {
40-
value: 4,
41-
message: t('components.account.AuthFormShared.username_min_length'),
42-
},
43-
maxLength: {
44-
value: 24,
45-
message: t('components.account.AuthFormShared.username_max_length'),
46-
},
47-
},
48-
registertoken: {
49-
required: t('components.account.AuthFormShared.token_required'),
50-
minLength: {
51-
value: 6,
52-
message: t('components.account.AuthFormShared.token_length'),
53-
},
54-
maxLength: {
55-
value: 6,
56-
message: t('components.account.AuthFormShared.token_length'),
57-
},
58-
},
59-
}
60-
}
61-
9+
// Define rules with translation keys
6210
export const rule: Record<RuleKeys, UseControllerProps['rules']> = {
6311
email: {
64-
required: '邮箱为必填项',
65-
pattern: { value: REGEX_EMAIL, message: '不合法的邮箱' },
12+
required: 'components.account.AuthFormShared.email_required',
13+
pattern: { value: REGEX_EMAIL, message: 'components.account.AuthFormShared.email_invalid' },
6614
},
6715
password: {
68-
required: '密码为必填项',
69-
minLength: { value: 8, message: '密码长度不能小于 8 位' },
70-
maxLength: { value: 32, message: '密码长度不能大于 32 位' },
16+
required: 'components.account.AuthFormShared.password_required',
17+
minLength: { value: 8, message: 'components.account.AuthFormShared.password_min_length' },
18+
maxLength: { value: 32, message: 'components.account.AuthFormShared.password_max_length' },
7119
},
7220
username: {
73-
required: '用户名为必填项',
74-
minLength: { value: 4, message: '用户名长度不能小于 4 位' },
75-
maxLength: { value: 24, message: '用户名长度不能大于 24 位' },
76-
pattern: { value: REGEX_USERNAME, message: '用户名前后不能包含空格' },
21+
required: 'components.account.AuthFormShared.username_required',
22+
minLength: { value: 4, message: 'components.account.AuthFormShared.username_min_length' },
23+
maxLength: { value: 24, message: 'components.account.AuthFormShared.username_max_length' },
24+
pattern: { value: REGEX_USERNAME, message: 'components.account.AuthFormShared.username_pattern' },
7725
},
7826
registertoken: {
79-
required: '邮箱验证码为必填项',
80-
minLength: { value: 6, message: '邮箱验证码长度为 6 位' },
81-
maxLength: { value: 6, message: '邮箱验证码长度为 6 位' },
27+
required: 'components.account.AuthFormShared.token_required',
28+
minLength: { value: 6, message: 'components.account.AuthFormShared.token_length' },
29+
maxLength: { value: 6, message: 'components.account.AuthFormShared.token_length' },
8230
},
8331
}
8432

85-
// --- **Opinioned** AuthForm Field Components ---
33+
// Helper function that translates rule messages
34+
function useTranslatedRules() {
35+
const { t } = useTranslation()
36+
37+
return function translateRule(ruleObj: UseControllerProps['rules']) {
38+
if (!ruleObj) return ruleObj
39+
40+
const result: UseControllerProps['rules'] = {}
41+
42+
for (const [key, value] of Object.entries(ruleObj)) {
43+
if (typeof value === 'string') {
44+
result[key] = t(value)
45+
} else if (typeof value === 'object' && value !== null) {
46+
result[key] = { ...value }
47+
if (typeof value.message === 'string') {
48+
result[key].message = t(value.message)
49+
}
50+
} else {
51+
result[key] = value
52+
}
53+
}
54+
55+
return result
56+
}
57+
}
8658

8759
export type AuthFormFieldProps<T extends FieldValues> = Pick<
8860
FormFieldProps<T, any>,
@@ -106,15 +78,16 @@ export const AuthFormEmailField = <T extends FieldValues>({
10678
inputGroupProps,
10779
}: AuthFormFieldProps<T>) => {
10880
const { t } = useTranslation()
109-
label = label || t('components.account.AuthFormShared.email')
81+
const translateRule = useTranslatedRules()
82+
11083
return (
11184
<FormField
112-
label={label}
85+
label={label || t('components.account.AuthFormShared.email')}
11386
field={field}
11487
control={control}
11588
error={error}
11689
ControllerProps={{
117-
rules: rule.email,
90+
rules: translateRule(rule.email),
11891
render: (renderProps) => (
11992
<InputGroup
12093
id={field}
@@ -129,9 +102,7 @@ export const AuthFormEmailField = <T extends FieldValues>({
129102
),
130103
}}
131104
FormGroupProps={{
132-
helperText:
133-
register &&
134-
t('components.account.AuthFormShared.email_verification_note'),
105+
helperText: register && t('components.account.AuthFormShared.email_verification_note'),
135106
}}
136107
/>
137108
)
@@ -147,16 +118,16 @@ export const AuthRegistrationTokenField = <T extends FieldValues>({
147118
inputGroupProps,
148119
}: AuthFormFieldProps<T>) => {
149120
const { t } = useTranslation()
150-
label =
151-
label || t('components.account.AuthFormShared.email_verification_code')
121+
const translateRule = useTranslatedRules()
122+
152123
return (
153124
<FormField
154-
label={label}
125+
label={label || t('components.account.AuthFormShared.token')}
155126
field={field}
156127
control={control}
157128
error={error}
158129
ControllerProps={{
159-
rules: rule.registertoken,
130+
rules: translateRule(rule.registertoken),
160131
render: (renderProps) => (
161132
<InputGroup
162133
id={field}
@@ -169,8 +140,7 @@ export const AuthRegistrationTokenField = <T extends FieldValues>({
169140
),
170141
}}
171142
FormGroupProps={{
172-
helperText:
173-
register && t('components.account.AuthFormShared.enter_email_code'),
143+
helperText: register && t('components.account.AuthFormShared.token_verification_note'),
174144
}}
175145
/>
176146
)
@@ -185,15 +155,16 @@ export const AuthFormPasswordField = <T extends FieldValues>({
185155
inputGroupProps,
186156
}: AuthFormFieldProps<T>) => {
187157
const { t } = useTranslation()
188-
label = label || t('components.account.AuthFormShared.password')
158+
const translateRule = useTranslatedRules()
159+
189160
return (
190161
<FormField
191-
label={label}
162+
label={label || t('components.account.AuthFormShared.password')}
192163
field={field}
193164
control={control}
194165
error={error}
195166
ControllerProps={{
196-
rules: rule.password,
167+
rules: translateRule(rule.password),
197168
render: (renderProps) => (
198169
<InputGroup
199170
id={field}
@@ -219,15 +190,16 @@ export const AuthFormUsernameField = <T extends FieldValues>({
219190
inputGroupProps,
220191
}: AuthFormFieldProps<T>) => {
221192
const { t } = useTranslation()
222-
label = label || t('components.account.AuthFormShared.username')
193+
const translateRule = useTranslatedRules()
194+
223195
return (
224196
<FormField
225-
label={label}
197+
label={label || t('components.account.AuthFormShared.username')}
226198
field={field}
227199
control={control}
228200
error={error}
229201
ControllerProps={{
230-
rules: rule.username,
202+
rules: translateRule(rule.username),
231203
render: (renderProps) => (
232204
<InputGroup
233205
id={field}

src/i18n/translations.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858
"cn": "用户名长度不能大于 24 位",
5959
"en": "Username cannot be longer than 24 characters"
6060
},
61+
"username_pattern": {
62+
"cn": "用户名前后不能包含空格",
63+
"en": "Username cannot start or end with spaces"
64+
},
6165
"token_required": {
6266
"cn": "邮箱验证码为必填项",
6367
"en": "Email verification code is required"

0 commit comments

Comments
 (0)