This repository has been archived by the owner on Oct 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 398
/
TextInput.js
177 lines (152 loc) · 3.53 KB
/
TextInput.js
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
import PropTypes from 'prop-types';
import React from 'react';
import classNames from 'classnames';
import { settings } from 'carbon-components';
const { prefix } = settings;
const TextInput = ({
labelText,
className = `${prefix}--text__input`,
id,
placeholder,
type,
onChange,
onClick,
hideLabel,
invalid,
invalidText,
helperText,
light,
...other
}) => {
const textInputProps = {
id,
onChange: evt => {
if (!other.disabled) {
onChange(evt);
}
},
onClick: evt => {
if (!other.disabled) {
onClick(evt);
}
},
placeholder,
type,
};
const errorId = id + '-error-msg';
const textInputClasses = classNames(`${prefix}--text-input`, className, {
[`${prefix}--text-input--light`]: light,
});
const labelClasses = classNames(`${prefix}--label`, {
[`${prefix}--visually-hidden`]: hideLabel,
});
const label = labelText ? (
<label htmlFor={id} className={labelClasses}>
{labelText}
</label>
) : null;
const error = invalid ? (
<div className={`${prefix}--form-requirement`} id={errorId}>
{invalidText}
</div>
) : null;
const input = invalid ? (
<input
{...other}
{...textInputProps}
data-invalid
aria-invalid
aria-describedby={errorId}
className={textInputClasses}
/>
) : (
<input {...other} {...textInputProps} className={textInputClasses} />
);
const helper = helperText ? (
<div className={`${prefix}--form__helper-text`}>{helperText}</div>
) : null;
return (
<div className={`${prefix}--form-item`}>
{label}
{helper}
{input}
{error}
</div>
);
};
TextInput.propTypes = {
/**
* Specify an optional className to be applied to the <input> node
*/
className: PropTypes.string,
/**
* Optionally provide the default value of the <input>
*/
defaultValue: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Specify whether the <input> should be disabled
*/
disabled: PropTypes.bool,
/**
* Specify a custom `id` for the <input>
*/
id: PropTypes.string.isRequired,
/**
* Provide the text that will be read by a screen reader when visiting this
* control
*/
labelText: PropTypes.node.isRequired,
/**
* Optionally provide an `onChange` handler that is called whenever <input>
* is updated
*/
onChange: PropTypes.func,
/**
* Optionally provide an `onClick` handler that is called whenever the
* <input> is clicked
*/
onClick: PropTypes.func,
/**
* Specify the placeholder attribute for the <input>
*/
placeholder: PropTypes.string,
/**
* Specify the type of the <input>
*/
type: PropTypes.string,
/**
* Specify the value of the <input>
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Specify whether you want the underlying label to be visually hidden
*/
hideLabel: PropTypes.bool,
/**
* Specify whether the control is currently invalid
*/
invalid: PropTypes.bool,
/**
* Provide the text that is displayed when the control is in an invalid state
*/
invalidText: PropTypes.string,
/**
* Provide text that is used alongside the control label for additional help
*/
helperText: PropTypes.node,
/**
* `true` to use the light version.
*/
light: PropTypes.bool,
};
TextInput.defaultProps = {
disabled: false,
type: 'text',
onChange: () => {},
onClick: () => {},
invalid: false,
invalidText: '',
helperText: '',
light: false,
};
export default TextInput;