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
193 lines (173 loc) · 4.46 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Copyright IBM Corp. 2016, 2018
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import PropTypes from 'prop-types';
import React from 'react';
import classNames from 'classnames';
import { settings } from 'carbon-components';
import WarningFilled16 from '@carbon/icons-react/lib/warning--filled/16';
import PasswordInput from './PasswordInput';
import ControlledPasswordInput from './ControlledPasswordInput';
import { textInputProps } from './util';
const { prefix } = settings;
const TextInput = React.forwardRef(function TextInput(
{
labelText,
className = `${prefix}--text__input`,
id,
placeholder,
type,
onChange,
onClick,
hideLabel,
invalid,
invalidText,
helperText,
light,
...other
},
ref
) {
const errorId = id + '-error-msg';
const textInputClasses = classNames(`${prefix}--text-input`, className, {
[`${prefix}--text-input--light`]: light,
[`${prefix}--text-input--invalid`]: invalid,
});
const sharedTextInputProps = {
id,
onChange: evt => {
if (!other.disabled) {
onChange(evt);
}
},
onClick: evt => {
if (!other.disabled) {
onClick(evt);
}
},
placeholder,
type,
ref,
className: textInputClasses,
...other,
};
const labelClasses = classNames(`${prefix}--label`, {
[`${prefix}--visually-hidden`]: hideLabel,
[`${prefix}--label--disabled`]: other.disabled,
});
const helperTextClasses = classNames(`${prefix}--form__helper-text`, {
[`${prefix}--form__helper-text--disabled`]: other.disabled,
});
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 = (
<input {...textInputProps({ invalid, sharedTextInputProps, errorId })} />
);
const helper = helperText ? (
<div className={helperTextClasses}>{helperText}</div>
) : null;
return (
<div className={`${prefix}--form-item ${prefix}--text-input-wrapper`}>
{label}
{helper}
<div
className={`${prefix}--text-input__field-wrapper`}
data-invalid={invalid || null}>
{invalid && (
<WarningFilled16 className={`${prefix}--text-input__invalid-icon`} />
)}
{input}
</div>
{error}
</div>
);
});
TextInput.PasswordInput = PasswordInput;
TextInput.ControlledPasswordInput = ControlledPasswordInput;
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;