|
@@ -28,7 +28,7 @@
|
|
</span>
|
|
</span>
|
|
<el-input
|
|
<el-input
|
|
ref="input"
|
|
ref="input"
|
|
- :value="currentValue"
|
|
|
|
|
|
+ :value="currentInputValue"
|
|
:disabled="inputNumberDisabled"
|
|
:disabled="inputNumberDisabled"
|
|
:size="inputNumberSize"
|
|
:size="inputNumberSize"
|
|
:max="max"
|
|
:max="max"
|
|
@@ -96,7 +96,13 @@
|
|
default: ''
|
|
default: ''
|
|
},
|
|
},
|
|
name: String,
|
|
name: String,
|
|
- label: String
|
|
|
|
|
|
+ label: String,
|
|
|
|
+ precision: {
|
|
|
|
+ type: Number,
|
|
|
|
+ validator(val) {
|
|
|
|
+ return val >= 0 && val === parseInt(val, 10);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
},
|
|
},
|
|
data() {
|
|
data() {
|
|
return {
|
|
return {
|
|
@@ -108,7 +114,14 @@
|
|
immediate: true,
|
|
immediate: true,
|
|
handler(value) {
|
|
handler(value) {
|
|
let newVal = value === undefined ? value : Number(value);
|
|
let newVal = value === undefined ? value : Number(value);
|
|
- if (newVal !== undefined && isNaN(newVal)) return;
|
|
|
|
|
|
+ if (newVal !== undefined) {
|
|
|
|
+ if (isNaN(newVal)) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (this.precision !== undefined) {
|
|
|
|
+ newVal = this.toPrecision(newVal, this.precision);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
if (newVal >= this.max) newVal = this.max;
|
|
if (newVal >= this.max) newVal = this.max;
|
|
if (newVal <= this.min) newVal = this.min;
|
|
if (newVal <= this.min) newVal = this.min;
|
|
this.currentValue = newVal;
|
|
this.currentValue = newVal;
|
|
@@ -123,9 +136,17 @@
|
|
maxDisabled() {
|
|
maxDisabled() {
|
|
return this._increase(this.value, this.step) > this.max;
|
|
return this._increase(this.value, this.step) > this.max;
|
|
},
|
|
},
|
|
- precision() {
|
|
|
|
- const { value, step, getPrecision } = this;
|
|
|
|
- return Math.max(getPrecision(value), getPrecision(step));
|
|
|
|
|
|
+ numPrecision() {
|
|
|
|
+ const { value, step, getPrecision, precision } = this;
|
|
|
|
+ const stepPrecision = getPrecision(step);
|
|
|
|
+ if (precision !== undefined) {
|
|
|
|
+ if (stepPrecision > precision) {
|
|
|
|
+ console.warn('[Element Warn][InputNumber]precision should not be less than the decimal places of step');
|
|
|
|
+ }
|
|
|
|
+ return precision;
|
|
|
|
+ } else {
|
|
|
|
+ return Math.max(getPrecision(value), stepPrecision);
|
|
|
|
+ }
|
|
},
|
|
},
|
|
controlsAtRight() {
|
|
controlsAtRight() {
|
|
return this.controlsPosition === 'right';
|
|
return this.controlsPosition === 'right';
|
|
@@ -138,11 +159,19 @@
|
|
},
|
|
},
|
|
inputNumberDisabled() {
|
|
inputNumberDisabled() {
|
|
return this.disabled || (this.elForm || {}).disabled;
|
|
return this.disabled || (this.elForm || {}).disabled;
|
|
|
|
+ },
|
|
|
|
+ currentInputValue() {
|
|
|
|
+ const currentValue = this.currentValue;
|
|
|
|
+ if (typeof currentValue === 'number' && this.precision !== undefined) {
|
|
|
|
+ return currentValue.toFixed(this.precision);
|
|
|
|
+ } else {
|
|
|
|
+ return currentValue;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
},
|
|
},
|
|
methods: {
|
|
methods: {
|
|
toPrecision(num, precision) {
|
|
toPrecision(num, precision) {
|
|
- if (precision === undefined) precision = this.precision;
|
|
|
|
|
|
+ if (precision === undefined) precision = this.numPrecision;
|
|
return parseFloat(parseFloat(Number(num).toFixed(precision)));
|
|
return parseFloat(parseFloat(Number(num).toFixed(precision)));
|
|
},
|
|
},
|
|
getPrecision(value) {
|
|
getPrecision(value) {
|
|
@@ -158,14 +187,14 @@
|
|
_increase(val, step) {
|
|
_increase(val, step) {
|
|
if (typeof val !== 'number' && val !== undefined) return this.currentValue;
|
|
if (typeof val !== 'number' && val !== undefined) return this.currentValue;
|
|
|
|
|
|
- const precisionFactor = Math.pow(10, this.precision);
|
|
|
|
|
|
+ const precisionFactor = Math.pow(10, this.numPrecision);
|
|
// Solve the accuracy problem of JS decimal calculation by converting the value to integer.
|
|
// Solve the accuracy problem of JS decimal calculation by converting the value to integer.
|
|
return this.toPrecision((precisionFactor * val + precisionFactor * step) / precisionFactor);
|
|
return this.toPrecision((precisionFactor * val + precisionFactor * step) / precisionFactor);
|
|
},
|
|
},
|
|
_decrease(val, step) {
|
|
_decrease(val, step) {
|
|
if (typeof val !== 'number' && val !== undefined) return this.currentValue;
|
|
if (typeof val !== 'number' && val !== undefined) return this.currentValue;
|
|
|
|
|
|
- const precisionFactor = Math.pow(10, this.precision);
|
|
|
|
|
|
+ const precisionFactor = Math.pow(10, this.numPrecision);
|
|
|
|
|
|
return this.toPrecision((precisionFactor * val - precisionFactor * step) / precisionFactor);
|
|
return this.toPrecision((precisionFactor * val - precisionFactor * step) / precisionFactor);
|
|
},
|
|
},
|
|
@@ -183,17 +212,20 @@
|
|
},
|
|
},
|
|
handleBlur(event) {
|
|
handleBlur(event) {
|
|
this.$emit('blur', event);
|
|
this.$emit('blur', event);
|
|
- this.$refs.input.setCurrentValue(this.currentValue);
|
|
|
|
|
|
+ this.$refs.input.setCurrentValue(this.currentInputValue);
|
|
},
|
|
},
|
|
handleFocus(event) {
|
|
handleFocus(event) {
|
|
this.$emit('focus', event);
|
|
this.$emit('focus', event);
|
|
},
|
|
},
|
|
setCurrentValue(newVal) {
|
|
setCurrentValue(newVal) {
|
|
const oldVal = this.currentValue;
|
|
const oldVal = this.currentValue;
|
|
|
|
+ if (typeof newVal === 'number' && this.precision !== undefined) {
|
|
|
|
+ newVal = this.toPrecision(newVal, this.precision);
|
|
|
|
+ }
|
|
if (newVal >= this.max) newVal = this.max;
|
|
if (newVal >= this.max) newVal = this.max;
|
|
if (newVal <= this.min) newVal = this.min;
|
|
if (newVal <= this.min) newVal = this.min;
|
|
if (oldVal === newVal) {
|
|
if (oldVal === newVal) {
|
|
- this.$refs.input.setCurrentValue(this.currentValue);
|
|
|
|
|
|
+ this.$refs.input.setCurrentValue(this.currentInputValue);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
this.$emit('input', newVal);
|
|
this.$emit('input', newVal);
|