-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathInput.js
More file actions
162 lines (152 loc) · 4.44 KB
/
Input.js
File metadata and controls
162 lines (152 loc) · 4.44 KB
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
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import keycoder from 'keycoder';
import Icon from './Icon';
import FormElement from './FormElement';
import Text from './Text';
import { uuid } from './util';
export default class Input extends Component {
constructor() {
super();
this.onChange = this.onChange.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
}
onChange(e) {
const value = e.target.value;
if (this.props.onChange) {
this.props.onChange(e, value);
}
}
onKeyDown(e) {
const { symbolPattern, onKeyDown } = this.props;
if (symbolPattern) {
const { keyCode, shiftKey } = e;
const value = keycoder.toCharacter(keyCode, shiftKey);
if (value && !value.match(new RegExp(symbolPattern))) {
e.preventDefault();
return;
}
}
if (onKeyDown) {
onKeyDown(e);
}
}
renderAddon(content) {
return (
<Text
tag='span'
className='slds-form-element__addon'
category='body'
type='regular'
>
{ content }
</Text>
);
}
renderIcon(icon, align) {
return (
React.isValidElement(icon) ? icon :
<Icon
icon={ icon }
className={ classnames('slds-input__icon', `slds-input__icon--${align}`, 'slds-icon-text-default') }
/>
);
}
renderInput(props) {
const {
id, readOnly, className, inputRef, type, bare, value, defaultValue, htmlReadOnly,
...pprops
} = props;
const inputClassNames = classnames(className, bare ? 'slds-input--bare' : 'slds-input');
return (
readOnly ?
<Text
type='regular'
category='body'
className='slds-form-element__static'
id={ id }
>
{ value }
</Text> :
<input
ref={ inputRef }
className={ inputClassNames }
id={ id }
type={ type }
value={ value }
defaultValue={ defaultValue }
readOnly={ htmlReadOnly }
{ ...pprops }
onChange={ this.onChange }
onKeyDown={ this.onKeyDown }
/>
);
}
render() {
const {
id = `input-${uuid()}`, label, required, error, readOnly, totalCols, cols, ...props
} = this.props;
if (label || required || error || totalCols || cols) {
const formElemProps = { id, label, required, error, readOnly, totalCols, cols };
return (
<FormElement { ...formElemProps }>
<Input { ...{ id, readOnly, ...props } } />
</FormElement>
);
}
const { iconLeft, iconRight, addonLeft, addonRight, ...pprops } = props;
delete pprops.symbolPattern;
const inputProps = { ...pprops, id, readOnly };
if (iconLeft || iconRight || addonLeft || addonRight) {
const wrapperClassName = classnames(
'slds-form-element__control',
{ 'slds-input-has-icon': iconLeft || iconRight },
{ 'slds-input-has-icon--left-right': iconLeft && iconRight },
{ 'slds-input-has-icon--left': iconLeft },
{ 'slds-input-has-icon--right': iconRight },
{ 'slds-input-has-fixed-addon': addonLeft || addonRight },
);
return (
<div className={ wrapperClassName }>
{ addonLeft ? this.renderAddon(addonLeft) : undefined }
{ iconLeft ? this.renderIcon(iconLeft, 'left') : undefined }
{ this.renderInput(inputProps) }
{ iconRight ? this.renderIcon(iconRight, 'right') : undefined }
{ addonRight ? this.renderAddon(addonRight) : undefined }
</div>
);
}
return this.renderInput(inputProps);
}
}
Input.propTypes = {
id: PropTypes.string,
className: PropTypes.string,
label: PropTypes.string,
required: PropTypes.bool,
error: FormElement.propTypes.error,
totalCols: PropTypes.number,
cols: PropTypes.number,
value: PropTypes.string,
defaultValue: PropTypes.string,
placeholder: PropTypes.string,
bare: PropTypes.bool,
inputRef: PropTypes.func,
symbolPattern: PropTypes.string,
readOnly: PropTypes.bool,
htmlReadOnly: PropTypes.bool,
iconLeft: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
]),
iconRight: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element,
]),
addonLeft: PropTypes.string,
addonRight: PropTypes.string,
onChange: PropTypes.func,
onKeyDown: PropTypes.func,
};
Input.isFormElement = true;