This repository was archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathInputField.jsx
203 lines (193 loc) · 4.83 KB
/
InputField.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
import React from 'react';
import PropTypes from 'prop-types';
import Field from 'terra-form-field';
import IconError from 'terra-icon/lib/icon/IconError';
import Input from './Input';
const propTypes = {
/**
* Id of the input. Populates both the input and the htmlFor prop of the wrapper Field.
*/
inputId: PropTypes.string.isRequired,
/**
* The label of the form control children.
*/
label: PropTypes.string.isRequired,
/**
* The defaultValue of the input field. Use this to create an uncontrolled input.
*/
defaultValue: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
/**
* Whether the input is disabled.
*/
disabled: PropTypes.bool,
/**
* Error message for when the input is invalid.
*/
error: PropTypes.node,
/**
* Error Icon to display when the input is invalid.
*/
errorIcon: PropTypes.element,
/**
* Help element to display with the input.
*/
help: PropTypes.node,
/**
* Whether or not to hide the required indicator on the label.
*/
hideRequired: PropTypes.bool,
/**
* Attributes to attach to the input object
*/
// eslint-disable-next-line react/forbid-prop-types
inputAttrs: PropTypes.object,
/**
* Whether the field and input displays as Incomplete. Use when no value has been provided. _(usage note: `required` must also be set)_.
*/
isIncomplete: PropTypes.bool,
/**
* Whether or not the field is an inline field.
*/
isInline: PropTypes.bool,
/**
* Whether the field and input displays as Invalid. Use when value does not meet validation pattern.
*/
isInvalid: PropTypes.bool,
/**
* Whether or not the label is visible. Use this props to hide a label while still creating it on the DOM for accessibility.
*/
isLabelHidden: PropTypes.bool,
/**
* Attributes to attach to the label.
*/
// eslint-disable-next-line react/forbid-prop-types
labelAttrs: PropTypes.object,
/**
* Set the max-width of a field using `length` or `%`. Best practice recommendation to never exceed
* a rendered value of 1020px. _(Note: Providing custom inline styles will take precedence.)_
*/
maxWidth: PropTypes.string,
/**
* Function to trigger when user changes the input value. Provide a function to create a controlled input.
*/
onChange: PropTypes.func,
/**
* Ref callback to pass into the ref attribute of the html input element.
*/
refCallback: PropTypes.func,
/**
* Whether or not the field is required.
*/
required: PropTypes.bool,
/**
* Whether or not to append the 'optional' label to a non-required field label.
*/
showOptional: PropTypes.bool,
/**
* Specifies the type of input element to display.
*/
type: PropTypes.string,
/**
* The value of the input field. Use this to create a controlled input.
*/
value: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
};
const defaultProps = {
defaultValue: undefined,
disabled: false,
error: null,
errorIcon: <IconError />,
help: null,
hideRequired: false,
inputAttrs: {},
isIncomplete: false,
isInline: false,
isInvalid: false,
isLabelHidden: false,
labelAttrs: {},
onChange: undefined,
maxWidth: undefined,
refCallback: undefined,
required: false,
showOptional: false,
type: undefined,
value: undefined,
};
const InputField = (props) => {
const {
defaultValue,
disabled,
error,
errorIcon,
help,
hideRequired,
inputAttrs,
inputId,
isIncomplete,
isInline,
isInvalid,
isLabelHidden,
label,
labelAttrs,
maxWidth,
onChange,
refCallback,
required,
showOptional,
type,
value,
...customProps
} = props;
let ariaDescriptionIds;
if (help && error && isInvalid) {
ariaDescriptionIds = `${inputId}-error ${inputId}-help`;
} else {
if (help) {
ariaDescriptionIds = `${inputId}-help`;
}
if (error && isInvalid) {
ariaDescriptionIds = `${inputId}-error`;
}
}
const inputType = type || inputAttrs.type;
return (
<Field
label={label}
labelAttrs={labelAttrs}
error={error}
errorIcon={errorIcon}
help={help}
hideRequired={hideRequired}
required={required}
showOptional={showOptional}
isInvalid={isInvalid}
isInline={isInline}
isLabelHidden={isLabelHidden}
htmlFor={inputId}
maxWidth={maxWidth}
{...customProps}
>
<Input
{...inputAttrs}
disabled={inputAttrs.disabled || disabled}
id={inputId}
isIncomplete={isIncomplete}
type={inputType}
onChange={onChange}
value={value}
defaultValue={defaultValue}
refCallback={refCallback}
aria-describedby={ariaDescriptionIds}
/>
</Field>
);
};
InputField.propTypes = propTypes;
InputField.defaultProps = defaultProps;
export default InputField;