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
127 lines (118 loc) · 3.59 KB
/
PasswordConfirmation.js
File metadata and controls
127 lines (118 loc) · 3.59 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
import React from 'react'
import { object, string } from 'prop-types'
import { t } from '@lingui/macro'
import { IconButton, Spinner, Stack } from '@chakra-ui/react'
import { CheckIcon, CloseIcon, LockIcon, ViewIcon, ViewOffIcon } from '@chakra-ui/icons'
import { FormField } from './FormField'
export function PasswordConfirmation({
passwordLabel = t`Password:`,
confirmPasswordLabel = t`Confirm Password:`,
inputProps,
...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')
}
}
function validateConfirmPassword(value) {
setConfirmIcon('spinner')
if (value === '') {
setConfirmIcon('close')
} else if (value !== document.getElementById('password').value) {
setConfirmIcon('close')
} else {
setConfirmIcon('check')
}
}
return (
<Stack {...props}>
<FormField
name="password"
label={passwordLabel}
leftElement={
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" />
) : (
<></>
)
}
rightElement={
<IconButton
id="showPassword"
aria-label={passwordShow ? t`Hide password` : t`Show password`}
onClick={handlePasswordShow}
h="buttons.lg"
mr={8}
icon={passwordShow ? <ViewOffIcon /> : <ViewIcon />}
/>
}
type={passwordShow ? 'text' : 'password'}
placeholder={t`Password`}
useFieldInput={{
name: 'password',
validate: validatePassword,
}}
inputProps={inputProps}
{...props}
/>
<FormField
name="confirmPassword"
label={confirmPasswordLabel}
leftElement={
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" />
) : (
<></>
)
}
rightElement={
<IconButton
id="showPasswordConfirm"
aria-label={confirmShow ? t`Hide password` : t`Show password`}
onClick={handleConfirmShow}
h="buttons.lg"
mr={8}
icon={confirmShow ? <ViewOffIcon /> : <ViewIcon />}
/>
}
type={confirmShow ? 'text' : 'password'}
placeholder={t`Password`}
useFieldInput={{
name: 'confirmPassword',
validate: validateConfirmPassword,
}}
inputProps={inputProps}
{...props}
/>
</Stack>
)
}
PasswordConfirmation.propTypes = {
passwordLabel: string,
confirmPasswordLabel: string,
inputProps: object,
}