-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormField.jsx
206 lines (198 loc) · 5.09 KB
/
FormField.jsx
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
197
198
199
200
201
202
203
204
205
206
import React, { forwardRef } from 'react';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import shortid from 'shortid';
import MaskedField from 'react-masked-field';
import Hint from '../hint/Hint';
import Label from '../label/Label';
const types = [
'date',
'datetime-local',
'email',
'file',
'hidden',
'image',
'month',
'number',
'password',
'range',
'search',
'tel',
'text',
'textarea',
'time',
'url',
'week',
];
const FormField = forwardRef(
(
{
borderless,
className,
defaultValue,
disabled,
errorText,
hasError,
helpText,
id,
label,
labelProps,
mask,
name,
onChange,
placeholder,
readOnly,
required,
type,
value,
...others
},
ref,
) => {
const uid = id || shortid.generate();
const inputName = name || uid;
const inputHintId = `${inputName}_hint`;
const inputErrorId = `${inputName}_error`;
const fieldType = !types.includes(type) ? 'text' : type;
const Input = mask ? MaskedField : 'input';
return (
<div
data-test-id="formfield"
className={classnames('atomikui-formfield', className, {
'has-error': hasError,
'atomikui-formfield--borderless': borderless,
})}
>
{label && (
<div
data-test-id="formfield-label"
className="atomikui-formfield__label"
>
<Label htmlFor={uid} {...labelProps}>
{label}
</Label>
</div>
)}
{type === 'textarea' ? (
<textarea
{...(!mask && { ref })}
id={uid}
name={inputName}
className={classnames(
'atomikui-formfield__textarea',
className,
{},
)}
placeholder={placeholder}
{...(value && { value })}
{...(defaultValue && !value && { defaultValue })}
defaultValue={defaultValue}
aria-describedby={`${inputHintId} ${inputErrorId}`}
required={required}
readOnly={readOnly}
disabled={disabled}
onChange={onChange}
{...others}
/>
) : (
<Input
{...(!mask && { ref })}
id={uid}
name={inputName}
className={classnames('atomikui-formfield__input', className, {})}
type={fieldType}
mask={mask}
placeholder={placeholder}
{...(value && { value })}
{...(defaultValue && !value && { defaultValue })}
aria-describedby={`${inputHintId} ${inputErrorId}`}
readOnly={readOnly}
disabled={disabled}
onChange={onChange}
required
{...others}
/>
)}
{(helpText || errorText) && (
<div className="atomikui-formfield__hints">
{helpText && <Hint id={inputHintId}>{helpText}</Hint>}
{hasError && (
<Hint id={inputErrorId} type="error">
{errorText}
</Hint>
)}
</div>
)}
</div>
);
},
);
FormField.propTypes = {
/** Sets forn field with no border */
borderless: PropTypes.bool,
/** Specifies custom component classes. */
className: PropTypes.string,
/** Sets the defaultValue for uncontrolled form fields */
defaultValue: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(Date),
]),
/** Disables a form field. */
disabled: PropTypes.bool,
/** Text to be displayed when there is an error. */
errorText: PropTypes.string,
/** Specifies the error state. */
hasError: PropTypes.bool,
/** Assistive text to be displayed with form field. */
helpText: PropTypes.string,
/** A unique id. */
id: PropTypes.string,
/** Specifies label text. */
label: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
/** Props to be spread on label element */
// eslint-disable-next-line react/forbid-prop-types
labelProps: PropTypes.object,
/** Optional form field mask */
mask: PropTypes.string,
/** onChange callback. */
onChange: PropTypes.func,
/** Specifies input placeholder text. */
placeholder: PropTypes.string,
/** Specifies input name attribute. */
name: PropTypes.string,
/** Makes a form field read only. */
readOnly: PropTypes.bool,
/** Specifies if a field is required. */
required: PropTypes.bool,
/** Specifies the type of input. */
type: PropTypes.oneOf(types),
/** Specifies the inputs value. */
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(Date),
]),
};
FormField.defaultProps = {
borderless: false,
className: '',
defaultValue: '',
disabled: false,
errorText: '',
hasError: false,
helpText: '',
id: null,
label: '',
labelProps: null,
mask: '',
name: '',
onChange: () => {},
placeholder: '',
readOnly: false,
required: false,
type: 'text',
value: '',
};
FormField.displayName = 'FormField';
export default FormField;