/
index.jsx
151 lines (135 loc) 路 3.78 KB
/
index.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
import React from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
class Input extends React.Component {
componentDidMount() {
if (process.env.NODE_ENV === 'development') {
this.checkHasLabel();
}
}
getHTMLTagForType() {
const { type } = this.props;
if (type === 'select' || type === 'textarea') { return type; }
return 'input';
}
getClassNameForType() {
switch (this.props.type) {
case 'file':
return 'form-control-file';
case 'checkbox':
case 'radio':
return 'form-check-input';
default:
return 'form-control';
}
}
setRef(forwardedRef) {
// In production just return the optional forwardedRef
if (process.env.NODE_ENV !== 'development') { return forwardedRef; }
return (element) => {
if (forwardedRef) { forwardedRef.current = element; } // eslint-disable-line no-param-reassign
this.inputEl = element;
};
}
checkHasLabel() {
if (this.inputEl.labels.length > 0) { return; }
if (this.inputEl.getAttribute('aria-label') !== null) { return; }
if (this.inputEl.getAttribute('aria-labelledby') !== null) { return; }
if (console) {
// eslint-disable-next-line no-console
console.warn('Input[a11y]: There is no associated label for this Input');
}
}
renderOptions(options) {
return options.map((option) => {
const {
value,
label,
group,
...attributes
} = option;
if (group) {
return (
<optgroup key={`optgroup-${label}`} label={label} {...attributes}>
{this.renderOptions(group)}
</optgroup>
);
}
return (
<option key={value} value={value} {...attributes}>
{label}
</option>
);
}, this);
}
render() {
const {
type,
className,
options,
forwardedRef, // eslint-disable-line react/prop-types
...attributes // eslint-disable-line react/prop-types
} = this.props;
const htmlTag = this.getHTMLTagForType();
const htmlProps = {
className: classNames(this.getClassNameForType(), className),
type: htmlTag === 'input' ? type : undefined,
...attributes,
ref: this.setRef(forwardedRef),
};
const htmlChildren = type === 'select' ? this.renderOptions(options) : null;
return React.createElement(htmlTag, htmlProps, htmlChildren);
}
}
Input.propTypes = {
/** specifies the type of component.
* One of select, textarea, or any valid type for an html input tag. */
type: PropTypes.oneOf([
'textarea',
'select',
'checkbox',
'color',
'date',
'datetime',
'datetime-local',
'email',
'file',
'hidden',
'image',
'month',
'number',
'password',
'radio',
'range',
'reset',
'search',
'submit',
'tel',
'text',
'time',
'url',
'week',
]).isRequired,
/** specifies the className in addition to a bootstrap class name. */
className: PropTypes.string,
/** should be used to specify the options of an Input of type select */
options: PropTypes.arrayOf(PropTypes.shape({
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
disabled: PropTypes.bool,
group: PropTypes.arrayOf(PropTypes.shape({
label: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
disabled: PropTypes.bool,
})),
})),
};
Input.defaultProps = {
className: undefined,
options: [],
};
// eslint-disable-next-line react/no-multi-comp
const InputWithRefForwarding = React.forwardRef((props, ref) => (
<Input forwardedRef={ref} {...props} />
));
export default InputWithRefForwarding;