Skip to content

Commit 667d3c7

Browse files
author
Marco Cesarato
committed
fix: issue with decimal auto adding zero
Refs #5
1 parent da0c3b6 commit 667d3c7

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

index.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class InputSpinner extends Component {
7070
onChange(num) {
7171
if (this.props.disabled) return;
7272
const current_value = this.state.value;
73-
if (String(num).endsWith(".") && !this.getValue().endsWith(".0")) {
73+
const separator = !this.isStringEmpty(this.props.decimalSeparator) ? this.props.decimalSeparator : ".";
74+
if (String(num).endsWith(separator) && !this.getValue().endsWith(separator + "0")) {
7475
this.decimalInput = true;
7576
}
7677
num = this.parseNum(String(num).replace(/^0+/, "")) || 0;
@@ -134,6 +135,7 @@ class InputSpinner extends Component {
134135
* @returns {*}
135136
*/
136137
parseNum(num) {
138+
num = String(num).replace(!this.isStringEmpty(this.props.decimalSeparator) ? this.props.decimalSeparator : ".", ".");
137139
if (this.typeDecimal()) {
138140
num = parseFloat(num);
139141
} else {
@@ -154,11 +156,16 @@ class InputSpinner extends Component {
154156
let value = this.state.value;
155157
if (this.typeDecimal() && this.decimalInput) {
156158
this.decimalInput = false;
157-
return this.parseNum(value).toFixed(1);
159+
value = this.parseNum(value).toFixed(1)
160+
.replace(/0+$/, "");
158161
} else if (this.typeDecimal()) {
159-
value = this.parseNum(value).toFixed(this.props.precision);
162+
value = String(this.parseNum(
163+
this.parseNum(value).toFixed(this.props.precision)
164+
));
165+
} else {
166+
value = String(this.parseNum(value));
160167
}
161-
return String(this.parseNum(value));
168+
return value.replace(".", !this.isStringEmpty(this.props.decimalSeparator) ? this.props.decimalSeparator : ".");
162169
}
163170

164171
/**
@@ -226,11 +233,11 @@ class InputSpinner extends Component {
226233
* @returns {*}
227234
* @param e
228235
*/
229-
onSubmit(e){
236+
onSubmit(e){
230237
if (this.props.onSubmit) {
231238
this.props.onSubmit(this.parseNum(e.nativeEvent.text));
232239
}
233-
}
240+
}
234241

235242

236243
/**
@@ -266,6 +273,15 @@ class InputSpinner extends Component {
266273
return Object.entries(obj).length === 0 && obj.constructor === Object;
267274
}
268275

276+
/**
277+
* Is string empty
278+
* @param str
279+
* @returns {boolean|boolean}
280+
*/
281+
isStringEmpty(str) {
282+
return !str || String(str) === "";
283+
}
284+
269285
/**
270286
* Is text input editable
271287
* @returns {boolean|Boolean}
@@ -335,8 +351,8 @@ class InputSpinner extends Component {
335351
return this.maxReached()
336352
? this._getColorMax()
337353
: this.minReached()
338-
? this._getColorMin()
339-
: this.props.color;
354+
? this._getColorMin()
355+
: this.props.color;
340356
}
341357

342358
/**
@@ -376,8 +392,8 @@ class InputSpinner extends Component {
376392
return this.maxReached()
377393
? this._getColorMax()
378394
: this.minReached()
379-
? this._getColorMin()
380-
: color;
395+
? this._getColorMin()
396+
: color;
381397
}
382398

383399
/**
@@ -721,6 +737,7 @@ InputSpinner.propTypes = {
721737
style: PropTypes.object,
722738
append: PropTypes.element,
723739
prepend: PropTypes.element,
740+
decimalSeparator: PropTypes.string,
724741
};
725742

726743
InputSpinner.defaultProps = {
@@ -758,6 +775,7 @@ InputSpinner.defaultProps = {
758775
buttonPressStyle: {},
759776
inputStyle: {},
760777
style: {},
778+
decimalSeparator: ".",
761779
};
762780

763781
export default InputSpinner;

0 commit comments

Comments
 (0)