1
1
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'
8
3
import { useTranslation } from 'react-i18next'
9
-
10
4
import { FormField , FormFieldProps } from 'components/FormField'
11
5
import { REGEX_EMAIL , REGEX_USERNAME } from 'utils/regexes'
12
6
13
7
export type RuleKeys = 'email' | 'password' | 'username' | 'registertoken'
14
8
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
62
10
export const rule : Record < RuleKeys , UseControllerProps [ 'rules' ] > = {
63
11
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 ' } ,
66
14
} ,
67
15
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 ' } ,
71
19
} ,
72
20
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 ' } ,
77
25
} ,
78
26
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 ' } ,
82
30
} ,
83
31
}
84
32
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
+ }
86
58
87
59
export type AuthFormFieldProps < T extends FieldValues > = Pick <
88
60
FormFieldProps < T , any > ,
@@ -106,15 +78,16 @@ export const AuthFormEmailField = <T extends FieldValues>({
106
78
inputGroupProps,
107
79
} : AuthFormFieldProps < T > ) => {
108
80
const { t } = useTranslation ( )
109
- label = label || t ( 'components.account.AuthFormShared.email' )
81
+ const translateRule = useTranslatedRules ( )
82
+
110
83
return (
111
84
< FormField
112
- label = { label }
85
+ label = { label || t ( 'components.account.AuthFormShared.email' ) }
113
86
field = { field }
114
87
control = { control }
115
88
error = { error }
116
89
ControllerProps = { {
117
- rules : rule . email ,
90
+ rules : translateRule ( rule . email ) ,
118
91
render : ( renderProps ) => (
119
92
< InputGroup
120
93
id = { field }
@@ -129,9 +102,7 @@ export const AuthFormEmailField = <T extends FieldValues>({
129
102
) ,
130
103
} }
131
104
FormGroupProps = { {
132
- helperText :
133
- register &&
134
- t ( 'components.account.AuthFormShared.email_verification_note' ) ,
105
+ helperText : register && t ( 'components.account.AuthFormShared.email_verification_note' ) ,
135
106
} }
136
107
/>
137
108
)
@@ -147,16 +118,16 @@ export const AuthRegistrationTokenField = <T extends FieldValues>({
147
118
inputGroupProps,
148
119
} : AuthFormFieldProps < T > ) => {
149
120
const { t } = useTranslation ( )
150
- label =
151
- label || t ( 'components.account.AuthFormShared.email_verification_code' )
121
+ const translateRule = useTranslatedRules ( )
122
+
152
123
return (
153
124
< FormField
154
- label = { label }
125
+ label = { label || t ( 'components.account.AuthFormShared.token' ) }
155
126
field = { field }
156
127
control = { control }
157
128
error = { error }
158
129
ControllerProps = { {
159
- rules : rule . registertoken ,
130
+ rules : translateRule ( rule . registertoken ) ,
160
131
render : ( renderProps ) => (
161
132
< InputGroup
162
133
id = { field }
@@ -169,8 +140,7 @@ export const AuthRegistrationTokenField = <T extends FieldValues>({
169
140
) ,
170
141
} }
171
142
FormGroupProps = { {
172
- helperText :
173
- register && t ( 'components.account.AuthFormShared.enter_email_code' ) ,
143
+ helperText : register && t ( 'components.account.AuthFormShared.token_verification_note' ) ,
174
144
} }
175
145
/>
176
146
)
@@ -185,15 +155,16 @@ export const AuthFormPasswordField = <T extends FieldValues>({
185
155
inputGroupProps,
186
156
} : AuthFormFieldProps < T > ) => {
187
157
const { t } = useTranslation ( )
188
- label = label || t ( 'components.account.AuthFormShared.password' )
158
+ const translateRule = useTranslatedRules ( )
159
+
189
160
return (
190
161
< FormField
191
- label = { label }
162
+ label = { label || t ( 'components.account.AuthFormShared.password' ) }
192
163
field = { field }
193
164
control = { control }
194
165
error = { error }
195
166
ControllerProps = { {
196
- rules : rule . password ,
167
+ rules : translateRule ( rule . password ) ,
197
168
render : ( renderProps ) => (
198
169
< InputGroup
199
170
id = { field }
@@ -219,15 +190,16 @@ export const AuthFormUsernameField = <T extends FieldValues>({
219
190
inputGroupProps,
220
191
} : AuthFormFieldProps < T > ) => {
221
192
const { t } = useTranslation ( )
222
- label = label || t ( 'components.account.AuthFormShared.username' )
193
+ const translateRule = useTranslatedRules ( )
194
+
223
195
return (
224
196
< FormField
225
- label = { label }
197
+ label = { label || t ( 'components.account.AuthFormShared.username' ) }
226
198
field = { field }
227
199
control = { control }
228
200
error = { error }
229
201
ControllerProps = { {
230
- rules : rule . username ,
202
+ rules : translateRule ( rule . username ) ,
231
203
render : ( renderProps ) => (
232
204
< InputGroup
233
205
id = { field }
0 commit comments