-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
196 lines (184 loc) · 7.35 KB
/
index.jsx
File metadata and controls
196 lines (184 loc) · 7.35 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React, {useState} from 'react'
import PropTypes from 'prop-types'
import {Controller} from 'react-hook-form'
import {
Field as ChakraField,
IconButton,
Input,
InputGroup,
NativeSelect,
Checkbox
} from '@chakra-ui/react'
import {VisibilityIcon, VisibilityOffIcon, AlertIcon} from '../icons'
import {useIntl} from 'react-intl'
const Field = ({
name,
label,
formLabel,
type = 'text',
options = [],
rules = {},
error,
placeholder,
inputProps,
control,
autoComplete,
defaultValue,
helpText,
children,
inputRef
}) => {
const intl = useIntl()
const [hidePassword, setHidePassword] = useState(true)
const PasswordIcon = hidePassword ? VisibilityIcon : VisibilityOffIcon
const passwordIconLabel = hidePassword
? intl.formatMessage({
id: 'field.password.assistive_msg.show_password',
defaultMessage: 'Show password'
})
: intl.formatMessage({
id: 'field.password.assistive_msg.hide_password',
defaultMessage: 'Hide password'
})
const inputType =
type === 'password' && hidePassword ? 'password' : type === 'password' ? 'text' : type
return (
<Controller
name={name}
control={control}
rules={rules}
defaultValue={defaultValue}
render={({field: {onChange, value, ref}}) => {
const _inputProps =
typeof inputProps === 'function' ? inputProps({value, onChange}) : inputProps
const {endElement, ...restOfInputProps} = _inputProps || {}
return (
<ChakraField.Root id={name} invalid={!!error}>
{!['checkbox', 'radio', 'hidden'].includes(type) &&
(formLabel || <ChakraField.Label>{label}</ChakraField.Label>)}
{['text', 'password', 'email', 'phone', 'tel', 'number'].includes(type) && (
<InputGroup
endElement={
type === 'password' ? (
<IconButton
variant="ghost"
aria-label={passwordIconLabel}
onClick={() => setHidePassword(!hidePassword)}
>
<PasswordIcon color="gray.500" boxSize={6} />
</IconButton>
) : (
endElement
)
}
>
<>
<Input
ref={(node) => {
ref(node)
if (inputRef) inputRef.current = node
}}
onChange={onChange}
value={value}
type={inputType}
placeholder={placeholder}
autoComplete={autoComplete}
{...restOfInputProps}
/>
{children}
</>
</InputGroup>
)}
{type === 'hidden' && (
<input
ref={ref}
onChange={onChange}
value={value}
type="hidden"
{..._inputProps}
/>
)}
{type === 'select' && (
<>
<NativeSelect.Root value={value} {..._inputProps}>
<NativeSelect.Field
ref={ref}
value={value || ''}
onChange={onChange}
placeholder={placeholder}
>
{options.map((opt) => (
<option
key={`${opt.label}-${opt.value}`}
value={opt.value}
>
{opt.label}
</option>
))}
</NativeSelect.Field>
<NativeSelect.Indicator />
</NativeSelect.Root>
{children}
</>
)}
{type === 'checkbox' && (
<>
<Checkbox.Root
checked={value}
onCheckedChange={(details) => onChange(details.checked)}
{..._inputProps}
>
<Checkbox.HiddenInput ref={ref} />
<Checkbox.Control />
<Checkbox.Label>{formLabel || label}</Checkbox.Label>
</Checkbox.Root>
{children}
</>
)}
{error && type !== 'hidden' && (
<ChakraField.ErrorText color="red.600">
<AlertIcon aria-hidden="true" mr={2} />
{error.message}
</ChakraField.ErrorText>
)}
{helpText}
</ChakraField.Root>
)
}}
/>
)
}
Field.propTypes = {
name: PropTypes.string,
label: PropTypes.string,
autoComplete: PropTypes.string,
formLabel: PropTypes.any,
type: PropTypes.oneOf([
'text',
'number',
'password',
'email',
'phone',
'tel',
'select',
'checkbox',
'hidden'
]),
options: PropTypes.arrayOf(PropTypes.shape({label: PropTypes.string, value: PropTypes.any})),
rules: PropTypes.object,
error: PropTypes.shape({message: PropTypes.string}),
placeholder: PropTypes.string,
inputProps: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
control: PropTypes.object,
defaultValue: PropTypes.any,
helpText: PropTypes.any,
children: PropTypes.any,
inputRef: PropTypes.object
}
export default Field