mirror of https://github.com/ElemeFE/element
InputNumber: support undefined value (#9361)
parent
98bdee26b1
commit
b206747ed0
|
@ -8,39 +8,39 @@
|
||||||
]"
|
]"
|
||||||
>
|
>
|
||||||
<span
|
<span
|
||||||
v-if="controls"
|
|
||||||
class="el-input-number__decrease"
|
class="el-input-number__decrease"
|
||||||
:class="{'is-disabled': minDisabled}"
|
|
||||||
v-repeat-click="decrease"
|
|
||||||
@keydown.enter="decrease"
|
|
||||||
role="button"
|
role="button"
|
||||||
|
v-if="controls"
|
||||||
|
v-repeat-click="decrease"
|
||||||
|
:class="{'is-disabled': minDisabled}"
|
||||||
|
@keydown.enter="decrease"
|
||||||
>
|
>
|
||||||
<i :class="`el-icon-${controlsAtRight ? 'arrow-down' : 'minus'}`"></i>
|
<i :class="`el-icon-${controlsAtRight ? 'arrow-down' : 'minus'}`"></i>
|
||||||
</span>
|
</span>
|
||||||
<span
|
<span
|
||||||
v-if="controls"
|
|
||||||
class="el-input-number__increase"
|
class="el-input-number__increase"
|
||||||
:class="{'is-disabled': maxDisabled}"
|
|
||||||
v-repeat-click="increase"
|
|
||||||
@keydown.enter="increase"
|
|
||||||
role="button"
|
role="button"
|
||||||
|
v-if="controls"
|
||||||
|
v-repeat-click="increase"
|
||||||
|
:class="{'is-disabled': maxDisabled}"
|
||||||
|
@keydown.enter="increase"
|
||||||
>
|
>
|
||||||
<i :class="`el-icon-${controlsAtRight ? 'arrow-up' : 'plus'}`"></i>
|
<i :class="`el-icon-${controlsAtRight ? 'arrow-up' : 'plus'}`"></i>
|
||||||
</span>
|
</span>
|
||||||
<el-input
|
<el-input
|
||||||
|
ref="input"
|
||||||
:value="currentValue"
|
:value="currentValue"
|
||||||
@keydown.up.native.prevent="increase"
|
|
||||||
@keydown.down.native.prevent="decrease"
|
|
||||||
@blur="handleBlur"
|
|
||||||
@focus="handleFocus"
|
|
||||||
@change="handleInputChange"
|
|
||||||
:disabled="disabled"
|
:disabled="disabled"
|
||||||
:size="inputNumberSize"
|
:size="inputNumberSize"
|
||||||
:max="max"
|
:max="max"
|
||||||
:min="min"
|
:min="min"
|
||||||
:name="name"
|
:name="name"
|
||||||
ref="input"
|
|
||||||
:label="label"
|
:label="label"
|
||||||
|
@keydown.up.native.prevent="increase"
|
||||||
|
@keydown.down.native.prevent="decrease"
|
||||||
|
@blur="handleBlur"
|
||||||
|
@focus="handleFocus"
|
||||||
|
@change="handleInputChange"
|
||||||
>
|
>
|
||||||
<template slot="prepend" v-if="$slots.prepend">
|
<template slot="prepend" v-if="$slots.prepend">
|
||||||
<slot name="prepend"></slot>
|
<slot name="prepend"></slot>
|
||||||
|
@ -83,9 +83,7 @@
|
||||||
type: Number,
|
type: Number,
|
||||||
default: -Infinity
|
default: -Infinity
|
||||||
},
|
},
|
||||||
value: {
|
value: {},
|
||||||
default: 0
|
|
||||||
},
|
|
||||||
disabled: Boolean,
|
disabled: Boolean,
|
||||||
size: String,
|
size: String,
|
||||||
controls: {
|
controls: {
|
||||||
|
@ -108,8 +106,8 @@
|
||||||
value: {
|
value: {
|
||||||
immediate: true,
|
immediate: true,
|
||||||
handler(value) {
|
handler(value) {
|
||||||
let newVal = Number(value);
|
let newVal = value === undefined ? value : Number(value);
|
||||||
if (isNaN(newVal)) return;
|
if (newVal !== undefined && isNaN(newVal)) return;
|
||||||
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;
|
||||||
|
@ -144,6 +142,7 @@
|
||||||
return parseFloat(parseFloat(Number(num).toFixed(precision)));
|
return parseFloat(parseFloat(Number(num).toFixed(precision)));
|
||||||
},
|
},
|
||||||
getPrecision(value) {
|
getPrecision(value) {
|
||||||
|
if (value === undefined) return 0;
|
||||||
const valueString = value.toString();
|
const valueString = value.toString();
|
||||||
const dotPosition = valueString.indexOf('.');
|
const dotPosition = valueString.indexOf('.');
|
||||||
let precision = 0;
|
let precision = 0;
|
||||||
|
@ -153,14 +152,14 @@
|
||||||
return precision;
|
return precision;
|
||||||
},
|
},
|
||||||
_increase(val, step) {
|
_increase(val, step) {
|
||||||
if (typeof val !== 'number') 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.precision);
|
||||||
|
// 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') 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.precision);
|
||||||
|
|
||||||
|
@ -170,14 +169,12 @@
|
||||||
if (this.disabled || this.maxDisabled) return;
|
if (this.disabled || this.maxDisabled) return;
|
||||||
const value = this.value || 0;
|
const value = this.value || 0;
|
||||||
const newVal = this._increase(value, this.step);
|
const newVal = this._increase(value, this.step);
|
||||||
if (newVal > this.max) return;
|
|
||||||
this.setCurrentValue(newVal);
|
this.setCurrentValue(newVal);
|
||||||
},
|
},
|
||||||
decrease() {
|
decrease() {
|
||||||
if (this.disabled || this.minDisabled) return;
|
if (this.disabled || this.minDisabled) return;
|
||||||
const value = this.value || 0;
|
const value = this.value || 0;
|
||||||
const newVal = this._decrease(value, this.step);
|
const newVal = this._decrease(value, this.step);
|
||||||
if (newVal < this.min) return;
|
|
||||||
this.setCurrentValue(newVal);
|
this.setCurrentValue(newVal);
|
||||||
},
|
},
|
||||||
handleBlur(event) {
|
handleBlur(event) {
|
||||||
|
@ -200,8 +197,8 @@
|
||||||
this.currentValue = newVal;
|
this.currentValue = newVal;
|
||||||
},
|
},
|
||||||
handleInputChange(value) {
|
handleInputChange(value) {
|
||||||
const newVal = Number(value);
|
const newVal = value === '' ? undefined : Number(value);
|
||||||
if (!isNaN(newVal)) {
|
if (!isNaN(newVal) || value === '') {
|
||||||
this.setCurrentValue(newVal);
|
this.setCurrentValue(newVal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue