1
- import React from 'react'
2
- import { useRef , useState } from 'react'
1
+ import React , { useRef } from 'react'
2
+ import { useState } from 'react'
3
3
import zxcvbn from 'zxcvbn'
4
+ import useFocusableRef from '../../Hooks/useFocusableRef'
4
5
5
6
import { debounce } from '../../utils'
6
7
7
8
const NO_PASSWORD_ENTERED = 'Enter password'
8
9
9
- const PasswordInput = ( { getError, next, title, buttonText } ) => {
10
+ const PasswordInput = ( { getError : getInputError , next, title, buttonText, autofocus } ) => {
10
11
const [ error , setError ] = useState ( NO_PASSWORD_ENTERED )
11
- const inputRef = useRef ( null )
12
+ const inputRef = useFocusableRef ( autofocus )
13
+ const [ disabled , setDisabled ] = useState ( false )
12
14
13
15
const resetError = ( ) => setError ( NO_PASSWORD_ENTERED )
14
16
@@ -22,12 +24,20 @@ const PasswordInput = ({ getError, next, title, buttonText }) => {
22
24
setTimeout ( clear , 600 )
23
25
}
24
26
25
- const validateInput = debounce ( ( e ) => {
26
- const value = e . target . value
27
- if ( ! value ) return resetError ( )
28
- const err = getError ( value )
29
- setError ( err || '' )
30
- } , 300 )
27
+ const getError = ( ) =>
28
+ inputRef . current . value ? getInputError ( inputRef . current . value ) || '' : NO_PASSWORD_ENTERED
29
+
30
+ const validateInput = ( e ) => {
31
+ const err = getError ( )
32
+ if ( err ) {
33
+ setDisabled ( true )
34
+ return debounce ( ( ) => {
35
+ setDisabled ( false )
36
+ setError ( getError ( ) )
37
+ } , 300 ) ( )
38
+ }
39
+ return setError ( err )
40
+ }
31
41
32
42
return (
33
43
< div className = 'addAccountItemOptionSetupFrame' >
@@ -42,7 +52,7 @@ const PasswordInput = ({ getError, next, title, buttonText }) => {
42
52
ref = { inputRef }
43
53
onChange = { validateInput }
44
54
onKeyDown = { ( e ) => {
45
- if ( ! error && e . key === 'Enter' ) handleSubmit ( )
55
+ if ( ! error && e . key === 'Enter' && ! disabled ) handleSubmit ( )
46
56
} }
47
57
/>
48
58
</ div >
@@ -52,15 +62,15 @@ const PasswordInput = ({ getError, next, title, buttonText }) => {
52
62
{ error }
53
63
</ div >
54
64
) : (
55
- < div role = 'button' className = 'addAccountItemOptionSubmit' onClick = { ( ) => handleSubmit ( ) } >
65
+ < div role = 'button' className = 'addAccountItemOptionSubmit' onClick = { ( ) => ! disabled && handleSubmit ( ) } >
56
66
{ buttonText }
57
67
</ div >
58
68
) }
59
69
</ div >
60
70
)
61
71
}
62
72
63
- export const CreatePassword = ( { onCreate } ) => {
73
+ export const CreatePassword = ( { onCreate, autofocus } ) => {
64
74
const getError = ( password ) => {
65
75
if ( password . length < 12 ) return 'PASSWORD MUST BE 12 OR MORE CHARACTERS'
66
76
const {
@@ -72,13 +82,29 @@ export const CreatePassword = ({ onCreate }) => {
72
82
return ( warning || 'PLEASE ENTER A STRONGER PASSWORD' ) . toUpperCase ( )
73
83
}
74
84
75
- return < PasswordInput getError = { getError } next = { onCreate } title = 'Create Password' buttonText = 'Continue' />
85
+ return (
86
+ < PasswordInput
87
+ getError = { getError }
88
+ next = { onCreate }
89
+ title = 'Create Password'
90
+ buttonText = 'Continue'
91
+ autofocus = { autofocus }
92
+ />
93
+ )
76
94
}
77
95
78
- export const ConfirmPassword = ( { password, onConfirm } ) => {
96
+ export const ConfirmPassword = ( { password, onConfirm, autofocus } ) => {
79
97
const getError = ( confirmedPassword ) => {
80
98
if ( password !== confirmedPassword ) return 'PASSWORDS DO NOT MATCH'
81
99
}
82
100
83
- return < PasswordInput getError = { getError } next = { onConfirm } title = 'Confirm Password' buttonText = 'Create' />
101
+ return (
102
+ < PasswordInput
103
+ getError = { getError }
104
+ next = { onConfirm }
105
+ title = 'Confirm Password'
106
+ buttonText = 'Create'
107
+ autofocus = { autofocus }
108
+ />
109
+ )
84
110
}
0 commit comments