forked from pterolex/react-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumericInput.jsx
179 lines (149 loc) · 5.44 KB
/
NumericInput.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
/**
* @jsx React.DOM
*/
'use strict';
var React = require('react');
var cx = React.addons.classSet;
var numeral = require('numeral');
require('./NumericInput.less');
var NumericInput = React.createClass({
propTypes: {
placeholder: React.PropTypes.string,
type: React.PropTypes.string,
label: React.PropTypes.string,
error: React.PropTypes.string,
warning: React.PropTypes.string,
disabled: React.PropTypes.bool,
isNotExpandable: React.PropTypes.bool,
onChange: React.PropTypes.func
},
getInitialState() {
return {
value: this.props.value !== undefined ? this.props.value : '5',
min: this.props.min ? this.props.min : 0,
max: this.props.max ? this.props.max : 10,
isValid: this.props.isValid ? this.props.isValid : true,
formattedValue: this.props.value !== undefined ? this.formatValue(this.props.value) : '',
focused: false,
};
},
componentWillMount() {
this.setState({
error: this._checkLimits(this.state.value) ? '' : 'OUT OF LIMITS',
});
},
componentWillReceiveProps(nextProps) {
this.setState({
error: this._checkLimits(nextProps.value) ? '' : 'OUT OF LIMITS',
value: nextProps.value,
min: nextProps.min,
max: nextProps.max,
formattedValue: this.formatValue(nextProps.value),
});
},
formatValue(value) {
return numeral(value).format('0.00a');
},
_validatePartialValue(value) {
if (value === '') return true;
if (/\d*\.$/.test(value)) return true;
return false;
},
_checkLimits(value) {
if (parseFloat(value) < this.state.min || parseFloat(value) > this.state.max) {
return false;
} else {
return true;
}
},
handleChange(event) {
var value = event.target.value.toString();
var isValidNumber = /(^\d*\.\d*$)|(^\d*$)/.test(value);
var isPartialValue = this._validatePartialValue(value);
var isOutOfLimit = !this._checkLimits(value);
var error = isPartialValue ? 'WRONG VALUE' : '';
if (isValidNumber && isOutOfLimit) error = 'OUT OF LIMITS';
if (isValidNumber || isPartialValue) {
this.setState({
value: value.toString(),
error: error,
formattedValue: this.formatValue(value),
});
}
},
handleBlur(event){
var value = this.state.value ? this.state.value : '0';
this.setState({
focused: false,
value: value,
});
if (this.props.onBlur) {
if (this.props.dataMap) {
this.props.onBlur({
newValue: value,
path: this.props.dataMap,
valueToSend: value
});
} else this.props.onBlur(event);
}
},
handleFocus(){
this.setState({
focused: true,
});
if (this.props.onFocus) {
this.props.onFocus(event);
}
},
handleKeyDown(event){
if (this.props.onKeyDown) {
this.props.onKeyDown(event);
}
},
render() {
var errorNode = this.state.error
? (<div className="animated fadeInUp">
<label className="WLC-warning-text WLC-error">{this.state.error}</label>
<div className="WLC-appendix WLC-error"></div>
</div>)
: '';
var warningNode = this.state.warning
? (<div className="animated fadeInUp">
<label className="WLC-warning-text WLC-warning">{this.state.warning}</label>
<div className="WLC-appendix WLC-warning"></div>
</div>)
: '';
var typeSign = '';
if (this.props.type == 'percent') typeSign = '%';
if (this.props.type == 'currency') typeSign = this.props.currency.sign;
var symbolNode = <span className="symbol">{typeSign}</span>;
var inputClasses = cx({
'WLC-focused': this.state.focused,
'WLC-warning': this.state.warning,
'WLC-error': this.state.error,
});
var inputNode = <input ref = "input"
style = {this.props.style}
className = {inputClasses}
onChange = {this.handleChange}
onFocus = {this.handleFocus}
onBlur = {this.handleBlur}
onKeyDown = {this.handleKeyDown}
data-map = {this.props.dataMap}
data-name = {this.props.name}
disabled = {this.props.disabled}
type = {''}
value = {this.state.focused
? this.state.value.toString()
: this.state.formattedValue.toString()} />;
return (
<div className="WLC-NumericInput">
{inputNode}
{warningNode}
{errorNode}
{symbolNode}
</div>
);
}
});
module.exports = NumericInput;