Skip to content

Commit 298d764

Browse files
committed
frontend: use memo to keep the regex if needed
1 parent 6a54ab5 commit 298d764

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

frontends/web/src/components/password.tsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { useEffect, useRef, useState, ChangeEvent, ClipboardEvent } from 'react';
18+
import { useEffect, useRef, useState, ChangeEvent, ClipboardEvent, useMemo } from 'react';
1919
import { useTranslation } from 'react-i18next';
2020
import { useCapsLock } from '@/hooks/keyboard';
2121
import { Input, Checkbox, Field } from './forms';
@@ -72,17 +72,13 @@ export const PasswordSingleInput = ({
7272
const [seePlaintext, setSeePlaintext] = useState(false);
7373

7474
const passwordRef = useRef<HTMLInputElement>(null);
75-
const regexRef = useRef<RegExp>();
7675

77-
// Setup regex + autofocus
76+
// Autofocus
7877
useEffect(() => {
79-
if (pattern) {
80-
regexRef.current = new RegExp(pattern);
81-
}
8278
if (autoFocus && passwordRef.current) {
8379
passwordRef.current.focus();
8480
}
85-
}, [pattern, autoFocus]);
81+
}, [autoFocus]);
8682

8783
const tryPaste = (event: ClipboardEvent<HTMLInputElement>) => {
8884
if (event.currentTarget.type === 'password') {
@@ -96,7 +92,7 @@ export const PasswordSingleInput = ({
9692
};
9793

9894
const validate = (value: string) => {
99-
if (regexRef.current && passwordRef.current && !passwordRef.current.validity.valid) {
95+
if (passwordRef.current && !passwordRef.current.validity.valid) {
10096
onValidPassword(null);
10197
return;
10298
}
@@ -181,17 +177,15 @@ export const PasswordRepeatInput = ({
181177

182178
const passwordRef = useRef<HTMLInputElement>(null);
183179
const passwordRepeatRef = useRef<HTMLInputElement>(null);
184-
const regexRef = useRef<RegExp>();
185180

186-
// Setup regex + autofocus
181+
const regex = useMemo(() => (pattern ? new RegExp(pattern) : null), [pattern]);
182+
183+
// Autofocus
187184
useEffect(() => {
188-
if (pattern) {
189-
regexRef.current = new RegExp(pattern);
190-
}
191185
if (autoFocus && passwordRef.current) {
192186
passwordRef.current.focus();
193187
}
194-
}, [pattern, autoFocus]);
188+
}, [autoFocus]);
195189

196190
const tryPaste = (event: ClipboardEvent<HTMLInputElement>) => {
197191
if (event.currentTarget.type === 'password') {
@@ -206,7 +200,6 @@ export const PasswordRepeatInput = ({
206200

207201
const validate = (pwd: string, pwdRepeat: string) => {
208202
if (
209-
regexRef.current &&
210203
passwordRef.current &&
211204
passwordRepeatRef.current &&
212205
(!passwordRef.current.validity.valid || !passwordRepeatRef.current.validity.valid)
@@ -264,7 +257,9 @@ export const PasswordRepeatInput = ({
264257
{warning}
265258
</Input>
266259

267-
<MatchesPattern regex={regexRef.current} text={title} value={password} />
260+
{regex && (
261+
<MatchesPattern regex={regex} text={title} value={password} />
262+
)}
268263

269264
<Input
270265
disabled={disabled}
@@ -282,7 +277,9 @@ export const PasswordRepeatInput = ({
282277
{warning}
283278
</Input>
284279

285-
<MatchesPattern regex={regexRef.current} text={title} value={passwordRepeat} />
280+
{regex && (
281+
<MatchesPattern regex={regex} text={title} value={passwordRepeat} />
282+
)}
286283

287284
<Field>
288285
<Checkbox

0 commit comments

Comments
 (0)