2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
|
|
|
<div class="el-input-number"
|
|
|
|
:class="[
|
2017-10-12 09:50:06 +00:00
|
|
|
inputNumberSize ? 'el-input-number--' + inputNumberSize : '',
|
2016-11-30 10:16:49 +00:00
|
|
|
{ 'is-disabled': disabled },
|
2017-09-22 09:46:34 +00:00
|
|
|
{ 'is-without-controls': !controls },
|
|
|
|
{ 'is-controls-right': controlsAtRight }
|
2016-07-27 06:15:02 +00:00
|
|
|
]"
|
|
|
|
>
|
|
|
|
<span
|
2017-02-22 02:28:03 +00:00
|
|
|
class="el-input-number__decrease"
|
2018-01-21 03:06:59 +00:00
|
|
|
role="button"
|
|
|
|
v-if="controls"
|
2016-08-11 07:51:13 +00:00
|
|
|
v-repeat-click="decrease"
|
2018-01-21 03:06:59 +00:00
|
|
|
:class="{'is-disabled': minDisabled}"
|
2017-09-29 07:58:07 +00:00
|
|
|
@keydown.enter="decrease"
|
2016-07-27 06:15:02 +00:00
|
|
|
>
|
2017-09-22 09:46:34 +00:00
|
|
|
<i :class="`el-icon-${controlsAtRight ? 'arrow-down' : 'minus'}`"></i>
|
2016-07-27 06:15:02 +00:00
|
|
|
</span>
|
|
|
|
<span
|
2017-02-22 02:28:03 +00:00
|
|
|
class="el-input-number__increase"
|
2018-01-21 03:06:59 +00:00
|
|
|
role="button"
|
|
|
|
v-if="controls"
|
2016-08-11 07:51:13 +00:00
|
|
|
v-repeat-click="increase"
|
2018-01-21 03:06:59 +00:00
|
|
|
:class="{'is-disabled': maxDisabled}"
|
2017-09-29 07:58:07 +00:00
|
|
|
@keydown.enter="increase"
|
2016-07-27 06:15:02 +00:00
|
|
|
>
|
2017-09-22 09:46:34 +00:00
|
|
|
<i :class="`el-icon-${controlsAtRight ? 'arrow-up' : 'plus'}`"></i>
|
2016-07-27 06:15:02 +00:00
|
|
|
</span>
|
2016-12-27 01:24:59 +00:00
|
|
|
<el-input
|
2018-01-21 03:06:59 +00:00
|
|
|
ref="input"
|
2017-01-10 16:39:35 +00:00
|
|
|
:value="currentValue"
|
2016-12-27 01:24:59 +00:00
|
|
|
:disabled="disabled"
|
2017-10-12 09:50:06 +00:00
|
|
|
:size="inputNumberSize"
|
2017-01-03 04:23:01 +00:00
|
|
|
:max="max"
|
|
|
|
:min="min"
|
2017-07-30 08:26:05 +00:00
|
|
|
:name="name"
|
2017-09-29 07:58:07 +00:00
|
|
|
:label="label"
|
2018-01-21 03:06:59 +00:00
|
|
|
@keydown.up.native.prevent="increase"
|
|
|
|
@keydown.down.native.prevent="decrease"
|
|
|
|
@blur="handleBlur"
|
|
|
|
@focus="handleFocus"
|
|
|
|
@change="handleInputChange"
|
2016-12-27 01:24:59 +00:00
|
|
|
>
|
2017-07-30 08:26:05 +00:00
|
|
|
<template slot="prepend" v-if="$slots.prepend">
|
|
|
|
<slot name="prepend"></slot>
|
|
|
|
</template>
|
|
|
|
<template slot="append" v-if="$slots.append">
|
|
|
|
<slot name="append"></slot>
|
2018-01-09 06:08:46 +00:00
|
|
|
</template>
|
2016-12-27 01:24:59 +00:00
|
|
|
</el-input>
|
2016-07-27 06:15:02 +00:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
2016-10-25 13:35:41 +00:00
|
|
|
import ElInput from 'element-ui/packages/input';
|
2017-07-18 05:47:35 +00:00
|
|
|
import Focus from 'element-ui/src/mixins/focus';
|
2017-10-11 10:00:58 +00:00
|
|
|
import RepeatClick from 'element-ui/src/directives/repeat-click';
|
2016-07-27 06:15:02 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'ElInputNumber',
|
2017-07-18 05:47:35 +00:00
|
|
|
mixins: [Focus('input')],
|
2017-10-16 06:07:24 +00:00
|
|
|
inject: {
|
|
|
|
elFormItem: {
|
|
|
|
default: ''
|
|
|
|
}
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
directives: {
|
2017-10-11 10:00:58 +00:00
|
|
|
repeatClick: RepeatClick
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
ElInput
|
|
|
|
},
|
2016-12-27 01:24:59 +00:00
|
|
|
props: {
|
|
|
|
step: {
|
|
|
|
type: Number,
|
|
|
|
default: 1
|
|
|
|
},
|
|
|
|
max: {
|
|
|
|
type: Number,
|
|
|
|
default: Infinity
|
|
|
|
},
|
|
|
|
min: {
|
|
|
|
type: Number,
|
2017-01-03 04:23:01 +00:00
|
|
|
default: -Infinity
|
2016-12-27 01:24:59 +00:00
|
|
|
},
|
2018-01-21 03:06:59 +00:00
|
|
|
value: {},
|
2016-12-27 01:24:59 +00:00
|
|
|
disabled: Boolean,
|
|
|
|
size: String,
|
|
|
|
controls: {
|
|
|
|
type: Boolean,
|
|
|
|
default: true
|
2017-05-10 10:27:50 +00:00
|
|
|
},
|
2017-09-22 09:46:34 +00:00
|
|
|
controlsPosition: {
|
|
|
|
type: String,
|
|
|
|
default: ''
|
|
|
|
},
|
2017-09-29 07:58:07 +00:00
|
|
|
name: String,
|
|
|
|
label: String
|
2016-12-27 01:24:59 +00:00
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
data() {
|
|
|
|
return {
|
2017-01-11 17:52:57 +00:00
|
|
|
currentValue: 0
|
2016-07-27 06:15:02 +00:00
|
|
|
};
|
|
|
|
},
|
2016-08-11 07:51:13 +00:00
|
|
|
watch: {
|
2017-01-11 17:52:57 +00:00
|
|
|
value: {
|
|
|
|
immediate: true,
|
|
|
|
handler(value) {
|
2018-01-21 03:06:59 +00:00
|
|
|
let newVal = value === undefined ? value : Number(value);
|
|
|
|
if (newVal !== undefined && isNaN(newVal)) return;
|
2017-01-11 17:52:57 +00:00
|
|
|
if (newVal >= this.max) newVal = this.max;
|
|
|
|
if (newVal <= this.min) newVal = this.min;
|
2017-01-10 16:39:35 +00:00
|
|
|
this.currentValue = newVal;
|
2017-01-11 17:52:57 +00:00
|
|
|
this.$emit('input', newVal);
|
2016-08-11 07:51:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
computed: {
|
|
|
|
minDisabled() {
|
2017-01-03 04:23:01 +00:00
|
|
|
return this._decrease(this.value, this.step) < this.min;
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
maxDisabled() {
|
2017-01-03 04:23:01 +00:00
|
|
|
return this._increase(this.value, this.step) > this.max;
|
|
|
|
},
|
|
|
|
precision() {
|
|
|
|
const { value, step, getPrecision } = this;
|
|
|
|
return Math.max(getPrecision(value), getPrecision(step));
|
2017-09-22 09:46:34 +00:00
|
|
|
},
|
|
|
|
controlsAtRight() {
|
|
|
|
return this.controlsPosition === 'right';
|
2017-10-12 09:50:06 +00:00
|
|
|
},
|
|
|
|
_elFormItemSize() {
|
|
|
|
return (this.elFormItem || {}).elFormItemSize;
|
|
|
|
},
|
|
|
|
inputNumberSize() {
|
|
|
|
return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2017-01-03 04:23:01 +00:00
|
|
|
toPrecision(num, precision) {
|
|
|
|
if (precision === undefined) precision = this.precision;
|
|
|
|
return parseFloat(parseFloat(Number(num).toFixed(precision)));
|
2016-09-01 07:56:04 +00:00
|
|
|
},
|
2017-01-03 04:23:01 +00:00
|
|
|
getPrecision(value) {
|
2018-01-21 03:06:59 +00:00
|
|
|
if (value === undefined) return 0;
|
2017-01-03 04:23:01 +00:00
|
|
|
const valueString = value.toString();
|
|
|
|
const dotPosition = valueString.indexOf('.');
|
|
|
|
let precision = 0;
|
|
|
|
if (dotPosition !== -1) {
|
|
|
|
precision = valueString.length - dotPosition - 1;
|
2016-09-01 07:56:04 +00:00
|
|
|
}
|
2017-01-03 04:23:01 +00:00
|
|
|
return precision;
|
|
|
|
},
|
|
|
|
_increase(val, step) {
|
2018-01-21 03:06:59 +00:00
|
|
|
if (typeof val !== 'number' && val !== undefined) return this.currentValue;
|
2017-01-03 04:23:01 +00:00
|
|
|
|
|
|
|
const precisionFactor = Math.pow(10, this.precision);
|
2018-01-21 03:06:59 +00:00
|
|
|
// Solve the accuracy problem of JS decimal calculation by converting the value to integer.
|
2017-01-03 04:23:01 +00:00
|
|
|
return this.toPrecision((precisionFactor * val + precisionFactor * step) / precisionFactor);
|
|
|
|
},
|
|
|
|
_decrease(val, step) {
|
2018-01-21 03:06:59 +00:00
|
|
|
if (typeof val !== 'number' && val !== undefined) return this.currentValue;
|
2017-01-03 04:23:01 +00:00
|
|
|
|
|
|
|
const precisionFactor = Math.pow(10, this.precision);
|
|
|
|
|
|
|
|
return this.toPrecision((precisionFactor * val - precisionFactor * step) / precisionFactor);
|
2016-09-01 07:56:04 +00:00
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
increase() {
|
2017-01-03 04:23:01 +00:00
|
|
|
if (this.disabled || this.maxDisabled) return;
|
2016-11-24 06:48:56 +00:00
|
|
|
const value = this.value || 0;
|
2017-01-03 04:23:01 +00:00
|
|
|
const newVal = this._increase(value, this.step);
|
2017-01-10 16:39:35 +00:00
|
|
|
this.setCurrentValue(newVal);
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
decrease() {
|
2017-01-03 04:23:01 +00:00
|
|
|
if (this.disabled || this.minDisabled) return;
|
2016-11-24 06:48:56 +00:00
|
|
|
const value = this.value || 0;
|
2017-01-03 04:23:01 +00:00
|
|
|
const newVal = this._decrease(value, this.step);
|
2017-01-10 16:39:35 +00:00
|
|
|
this.setCurrentValue(newVal);
|
2016-11-01 03:16:56 +00:00
|
|
|
},
|
2017-07-17 10:24:24 +00:00
|
|
|
handleBlur(event) {
|
|
|
|
this.$emit('blur', event);
|
2016-12-27 11:14:22 +00:00
|
|
|
this.$refs.input.setCurrentValue(this.currentValue);
|
2017-01-10 16:39:35 +00:00
|
|
|
},
|
2017-07-17 10:24:24 +00:00
|
|
|
handleFocus(event) {
|
|
|
|
this.$emit('focus', event);
|
|
|
|
},
|
2017-01-10 16:39:35 +00:00
|
|
|
setCurrentValue(newVal) {
|
|
|
|
const oldVal = this.currentValue;
|
2017-01-11 17:52:57 +00:00
|
|
|
if (newVal >= this.max) newVal = this.max;
|
|
|
|
if (newVal <= this.min) newVal = this.min;
|
2017-05-10 10:27:50 +00:00
|
|
|
if (oldVal === newVal) {
|
|
|
|
this.$refs.input.setCurrentValue(this.currentValue);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-11 17:52:57 +00:00
|
|
|
this.$emit('change', newVal, oldVal);
|
|
|
|
this.$emit('input', newVal);
|
|
|
|
this.currentValue = newVal;
|
2017-01-10 16:39:35 +00:00
|
|
|
},
|
2018-01-09 06:08:46 +00:00
|
|
|
handleInputChange(value) {
|
2018-01-21 03:06:59 +00:00
|
|
|
const newVal = value === '' ? undefined : Number(value);
|
|
|
|
if (!isNaN(newVal) || value === '') {
|
2017-01-10 16:39:35 +00:00
|
|
|
this.setCurrentValue(newVal);
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
2017-05-10 10:27:50 +00:00
|
|
|
},
|
2017-09-29 07:58:07 +00:00
|
|
|
mounted() {
|
|
|
|
let innerInput = this.$refs.input.$refs.input;
|
|
|
|
innerInput.setAttribute('role', 'spinbutton');
|
|
|
|
innerInput.setAttribute('aria-valuemax', this.max);
|
|
|
|
innerInput.setAttribute('aria-valuemin', this.min);
|
|
|
|
innerInput.setAttribute('aria-valuenow', this.currentValue);
|
|
|
|
innerInput.setAttribute('aria-disabled', this.disabled);
|
|
|
|
},
|
|
|
|
updated() {
|
|
|
|
let innerInput = this.$refs.input.$refs.input;
|
|
|
|
innerInput.setAttribute('aria-valuenow', this.currentValue);
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|