forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordConfirmation.js
More file actions
161 lines (151 loc) · 4.79 KB
/
PasswordConfirmation.js
File metadata and controls
161 lines (151 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React from 'react'
import { t, Trans } from '@lingui/macro'
import {
Box,
FormControl,
FormErrorMessage,
FormLabel,
IconButton,
Input,
InputGroup,
InputLeftElement,
InputRightElement,
Spinner,
Stack,
} from '@chakra-ui/react'
import {
CheckIcon,
CloseIcon,
LockIcon,
ViewIcon,
ViewOffIcon,
} from '@chakra-ui/icons'
import { useField } from 'formik'
import { string } from 'prop-types'
export function PasswordConfirmation({
passwordLabel,
confirmPasswordLabel,
...props
}) {
const [icon, setIcon] = React.useState('lock')
const [confirmIcon, setConfirmIcon] = React.useState('lock')
const [passwordShow, setPasswordShow] = React.useState(false)
const handlePasswordShow = () => setPasswordShow(!passwordShow)
const [confirmShow, setConfirmShow] = React.useState(false)
const handleConfirmShow = () => setConfirmShow(!confirmShow)
function validatePassword(value) {
setIcon('spinner')
if (value === '') {
setIcon('close')
} else if (String(value).length < 12) {
setIcon('close')
} else {
setIcon('check')
}
}
const [passwordField, passwordMeta] = useField({
name: 'password',
validate: validatePassword,
})
function validateConfirmPassword(value) {
setConfirmIcon('spinner')
if (value === '') {
setConfirmIcon('close')
} else if (value !== document.getElementById('password').value) {
setConfirmIcon('close')
} else {
setConfirmIcon('check')
}
}
const [confirmPasswordField, confirmPasswordMeta] = useField({
name: 'confirmPassword',
validate: validateConfirmPassword,
})
const passwordLabelText =
passwordLabel === undefined ? <Trans>Password:</Trans> : passwordLabel
const confirmPasswordLabelText =
confirmPasswordLabel === undefined ? (
<Trans>Confirm Password:</Trans>
) : (
confirmPasswordLabel
)
return (
<Stack {...props}>
<Box>
<FormControl isInvalid={passwordMeta.error && passwordMeta.touched}>
<FormLabel htmlFor="password" fontWeight="bold">
{passwordLabelText}
</FormLabel>
<InputGroup>
<InputLeftElement aria-hidden="true">
{icon === 'spinner' && <Spinner size="sm" color="gray.300" />}
{icon === 'lock' && (
<LockIcon color="gray.300" aria-label="initial icon" />
)}
{icon === 'check' && <CheckIcon color="green.500" />}
{icon === 'close' && (
<CloseIcon color="red.500" aria-label="invalid password" />
)}
</InputLeftElement>
<Input
{...passwordField}
id="password"
placeholder={t`Password`}
type={passwordShow ? 'text' : 'password'}
/>
<InputRightElement width="width.4">
<IconButton
id="showPassword"
aria-label={passwordShow ? 'hide password' : 'show password'}
h="buttons.lg"
onClick={handlePasswordShow}
icon={passwordShow ? <ViewOffIcon /> : <ViewIcon />}
/>
</InputRightElement>
</InputGroup>
<FormErrorMessage>{passwordMeta.error}</FormErrorMessage>
</FormControl>
</Box>
<Box>
<FormControl
isInvalid={confirmPasswordMeta.error && confirmPasswordMeta.touched}
>
<FormLabel htmlFor="confirmPassword" fontWeight="bold">
{confirmPasswordLabelText}
</FormLabel>
<InputGroup>
<InputLeftElement aria-hidden="true">
{confirmIcon === 'spinner' && (
<Spinner size="sm" color="gray.300" />
)}
{confirmIcon === 'lock' && <LockIcon color="gray.300" />}
{confirmIcon === 'check' && <CheckIcon color="green.500" />}
{confirmIcon === 'close' && <CloseIcon color="red.500" />}
</InputLeftElement>
<Input
{...confirmPasswordField}
id="confirmPassword"
placeholder={t`Confirm password`}
type={confirmShow ? 'text' : 'password'}
/>
<InputRightElement width="width.4">
<IconButton
id="showPasswordConfirm"
aria-label={confirmShow ? 'hide password' : 'show password'}
h="buttons.lg"
onClick={handleConfirmShow}
icon={confirmShow ? <ViewOffIcon /> : <ViewIcon />}
/>
</InputRightElement>
</InputGroup>
<FormErrorMessage>{confirmPasswordMeta.error}</FormErrorMessage>
</FormControl>
</Box>
</Stack>
)
}
PasswordConfirmation.propTypes = {
spacing: string,
passwordLabel: string,
confirmPasswordLabel: string,
}