forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordField.js
More file actions
54 lines (47 loc) · 1.45 KB
/
PasswordField.js
File metadata and controls
54 lines (47 loc) · 1.45 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
import React from 'react'
import { func, object, oneOfType, shape, string } from 'prop-types'
import { t } from '@lingui/macro'
import { IconButton } from '@chakra-ui/react'
import { LockIcon, ViewIcon, ViewOffIcon } from '@chakra-ui/icons'
import { FormField } from './FormField'
function PasswordField({ forwardedRef, name, label, inputProps, ...props }) {
const [show, setShow] = React.useState(false)
const handleClick = () => setShow(!show)
return (
<FormField
name={name}
label={label}
leftElement={<LockIcon color="gray.300" />}
rightElement={
<IconButton
id={'show' + name.charAt(0).toUpperCase() + name.slice(1)}
aria-label={show ? 'hide password' : 'show password'}
onClick={handleClick}
h="buttons.lg"
mr={8}
icon={show ? <ViewOffIcon /> : <ViewIcon />}
/>
}
type={show ? 'text' : 'password'}
placeholder={t`Password`}
ref={forwardedRef}
inputProps={inputProps}
{...props}
/>
)
}
PasswordField.propTypes = {
name: string,
label: string,
inputProps: object,
forwardedRef: oneOfType([func, shape({ current: object })]),
}
PasswordField.defaultProps = {
name: 'password',
label: t`Password:`,
}
const withForwardedRef = React.forwardRef((props, ref) => {
return <PasswordField {...props} forwardedRef={ref} />
})
withForwardedRef.displayName = 'PasswordField'
export { withForwardedRef as PasswordField }