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 pathInput.jsx
180 lines (166 loc) · 4.5 KB
/
Input.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
// added to allow this component to be class based for the input ref callback below
/* eslint-disable react/prefer-stateless-function */
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import classNamesBind from 'classnames/bind';
import ThemeContext from 'terra-theme-context';
import styles from './Input.module.scss';
const cx = classNamesBind.bind(styles);
const propTypes = {
/**
* 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,
/**
* Whether the input displays as Incomplete. Use when no value has been provided. _(usage note: `required` must also be set)_.
*/
isIncomplete: PropTypes.bool,
/**
* Whether the input displays as Invalid. Use when value does not meet validation pattern.
*/
isInvalid: PropTypes.bool,
/**
* Function to trigger when the input loses focus.
*/
onBlur: PropTypes.func,
/**
* Function to trigger when user changes the input value. Provide a function to create a controlled input.
*/
onChange: PropTypes.func,
/**
* Function to trigger when the input gains focus.
*/
onFocus: PropTypes.func,
/**
* Content to be displayed as the name.
*/
name: PropTypes.string,
/**
* The regular expression that the input's value is checked against.
* NOTE: The pattern attribute works with the following input types: text, date, search, url, tel, email, and password.
*/
pattern: PropTypes.string,
/**
* Callback ref to pass into the input dom element.
*/
refCallback: PropTypes.func,
/**
* Whether the input is required.
*/
required: 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,
]),
/**
* String that labels the current element. 'aria-label' must be present,
* for accessibility.
*/
ariaLabel: PropTypes.string,
};
const defaultProps = {
defaultValue: undefined,
disabled: false,
isIncomplete: false,
isInvalid: false,
onBlur: undefined,
onChange: undefined,
onFocus: undefined,
name: null,
pattern: undefined,
required: false,
refCallback: undefined,
type: undefined,
value: undefined,
};
class Input extends React.Component {
render() {
const {
defaultValue,
disabled,
isIncomplete,
isInvalid,
onBlur,
onChange,
onFocus,
name,
pattern,
refCallback,
required,
type,
ariaLabel,
value,
...customProps
} = this.props;
const attributes = { ...customProps };
const theme = this.context;
const formInputClassNames = classNames(
cx(
'form-input',
{ 'form-error': isInvalid },
{ 'form-incomplete': (isIncomplete && required && !isInvalid) },
theme.className,
),
attributes.className,
);
let ariaLabelText;
// Handle case of users setting aria-label as a custom prop
if (attributes && Object.prototype.hasOwnProperty.call(attributes, 'aria-label')) {
// If they've set aria-label and ariaLabel, use the ariaLabel value,
// otherwise, fallback to using the aria-label value passed in.
ariaLabelText = !ariaLabel ? attributes['aria-label'] : ariaLabel;
} else if (ariaLabel) {
// If users only set ariaLabel prop, use that value
ariaLabelText = ariaLabel;
}
attributes['aria-label'] = ariaLabelText;
if (required) {
attributes['aria-required'] = 'true';
}
if (value !== undefined) {
attributes.value = value;
} else if (defaultValue !== undefined) {
attributes.defaultValue = defaultValue;
}
if (attributes.placeholder) {
attributes.placeholder = null;
}
return (
<input
{...attributes}
ref={(inputRef) => {
if (refCallback) refCallback(inputRef);
}}
name={name}
type={type}
pattern={pattern}
onBlur={onBlur}
onChange={onChange}
onFocus={onFocus}
disabled={disabled}
required={required}
className={formInputClassNames}
/>
);
}
}
Input.propTypes = propTypes;
Input.defaultProps = defaultProps;
Input.contextType = ThemeContext;
Input.isInput = true;
export default Input;