2016-07-27 06:15:02 +00:00
|
|
|
<template>
|
|
|
|
<div class="el-input-number"
|
|
|
|
:class="[
|
2016-10-25 03:33:39 +00:00
|
|
|
size ? 'el-input-number--' + size : '',
|
2016-07-27 06:15:02 +00:00
|
|
|
{ 'is-disabled': disabled }
|
|
|
|
]"
|
|
|
|
>
|
|
|
|
<el-input
|
2016-11-01 03:16:56 +00:00
|
|
|
:value="currentValue"
|
2016-10-26 03:22:13 +00:00
|
|
|
@keydown.up.native="increase"
|
|
|
|
@keydown.down.native="decrease"
|
2016-11-01 03:16:56 +00:00
|
|
|
@blur="handleBlur"
|
|
|
|
@input="handleInput"
|
2016-07-27 06:15:02 +00:00
|
|
|
:disabled="disabled"
|
|
|
|
:size="size"
|
|
|
|
:class="{
|
|
|
|
'is-active': inputActive
|
|
|
|
}">
|
|
|
|
</el-input>
|
|
|
|
<span
|
|
|
|
class="el-input-number__decrease el-icon-minus"
|
|
|
|
:class="{'is-disabled': minDisabled}"
|
2016-08-11 07:51:13 +00:00
|
|
|
v-repeat-click="decrease"
|
2016-07-27 06:15:02 +00:00
|
|
|
@mouseenter="activeInput(minDisabled)"
|
2016-08-11 07:51:13 +00:00
|
|
|
@mouseleave="inactiveInput(minDisabled)"
|
2016-07-27 06:15:02 +00:00
|
|
|
>
|
|
|
|
</span>
|
|
|
|
<span
|
|
|
|
class="el-input-number__increase el-icon-plus"
|
|
|
|
:class="{'is-disabled': maxDisabled}"
|
2016-08-11 07:51:13 +00:00
|
|
|
v-repeat-click="increase"
|
2016-07-27 06:15:02 +00:00
|
|
|
@mouseenter="activeInput(maxDisabled)"
|
2016-08-11 07:51:13 +00:00
|
|
|
@mouseleave="inactiveInput(maxDisabled)"
|
2016-07-27 06:15:02 +00:00
|
|
|
>
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
2016-10-25 13:35:41 +00:00
|
|
|
import ElInput from 'element-ui/packages/input';
|
2016-07-27 06:15:02 +00:00
|
|
|
import { once, on } from 'wind-dom/src/event';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'ElInputNumber',
|
|
|
|
props: {
|
|
|
|
step: {
|
|
|
|
type: Number,
|
|
|
|
default: 1
|
|
|
|
},
|
|
|
|
max: {
|
|
|
|
type: Number,
|
|
|
|
default: Infinity
|
|
|
|
},
|
|
|
|
min: {
|
|
|
|
type: Number,
|
|
|
|
default: 0
|
|
|
|
},
|
2016-10-19 09:32:43 +00:00
|
|
|
value: {
|
|
|
|
default: 0
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
disabled: Boolean,
|
|
|
|
size: String
|
|
|
|
},
|
|
|
|
directives: {
|
|
|
|
repeatClick: {
|
2016-08-11 07:51:13 +00:00
|
|
|
bind(el, binding, vnode) {
|
2016-07-27 06:15:02 +00:00
|
|
|
let interval = null;
|
|
|
|
let startTime;
|
|
|
|
|
|
|
|
const handler = () => {
|
2016-08-11 07:51:13 +00:00
|
|
|
vnode.context[binding.expression]();
|
2016-07-27 06:15:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const clear = function() {
|
|
|
|
if (new Date() - startTime < 100) {
|
|
|
|
handler();
|
|
|
|
}
|
|
|
|
clearInterval(interval);
|
|
|
|
interval = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
on(el, 'mousedown', function() {
|
|
|
|
startTime = new Date();
|
|
|
|
once(document, 'mouseup', clear);
|
|
|
|
interval = setInterval(function() {
|
|
|
|
handler();
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
components: {
|
|
|
|
ElInput
|
|
|
|
},
|
|
|
|
data() {
|
2016-10-19 09:32:43 +00:00
|
|
|
// correct the init value
|
|
|
|
let value = this.value;
|
|
|
|
if (value < this.min) {
|
|
|
|
this.$emit('input', this.min);
|
|
|
|
value = this.min;
|
|
|
|
}
|
|
|
|
if (value > this.max) {
|
|
|
|
this.$emit('input', this.max);
|
|
|
|
value = this.max;
|
|
|
|
}
|
2016-07-27 06:15:02 +00:00
|
|
|
return {
|
2016-10-19 09:32:43 +00:00
|
|
|
currentValue: value,
|
2016-07-27 06:15:02 +00:00
|
|
|
inputActive: false
|
|
|
|
};
|
|
|
|
},
|
2016-08-11 07:51:13 +00:00
|
|
|
watch: {
|
2016-09-09 06:27:49 +00:00
|
|
|
value(val) {
|
|
|
|
this.currentValue = val;
|
|
|
|
},
|
|
|
|
|
2016-09-01 07:56:04 +00:00
|
|
|
currentValue(newVal, oldVal) {
|
2016-11-01 03:16:56 +00:00
|
|
|
let value = Number(newVal);
|
|
|
|
if (value <= this.max && value >= this.min) {
|
|
|
|
this.$emit('change', value);
|
|
|
|
this.$emit('input', value);
|
2016-08-11 07:51:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
computed: {
|
|
|
|
minDisabled() {
|
2016-11-01 03:16:56 +00:00
|
|
|
return this.value - this.step < this.min;
|
2016-07-27 06:15:02 +00:00
|
|
|
},
|
|
|
|
maxDisabled() {
|
2016-11-01 03:16:56 +00:00
|
|
|
return this.value + this.step > this.max;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2016-09-01 07:56:04 +00:00
|
|
|
accSub(arg1, arg2) {
|
|
|
|
var r1, r2, m, n;
|
|
|
|
try {
|
|
|
|
r1 = arg1.toString().split('.')[1].length;
|
|
|
|
} catch (e) {
|
|
|
|
r1 = 0;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
r2 = arg2.toString().split('.')[1].length;
|
|
|
|
} catch (e) {
|
|
|
|
r2 = 0;
|
|
|
|
}
|
|
|
|
m = Math.pow(10, Math.max(r1, r2));
|
|
|
|
n = (r1 >= r2) ? r1 : r2;
|
|
|
|
return parseFloat(((arg1 * m - arg2 * m) / m).toFixed(n));
|
|
|
|
},
|
|
|
|
accAdd(arg1, arg2) {
|
|
|
|
var r1, r2, m, c;
|
|
|
|
try {
|
|
|
|
r1 = arg1.toString().split('.')[1].length;
|
|
|
|
} catch (e) {
|
|
|
|
r1 = 0;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
r2 = arg2.toString().split('.')[1].length;
|
|
|
|
} catch (e) {
|
|
|
|
r2 = 0;
|
|
|
|
}
|
|
|
|
c = Math.abs(r1 - r2);
|
|
|
|
m = Math.pow(10, Math.max(r1, r2));
|
|
|
|
if (c > 0) {
|
|
|
|
var cm = Math.pow(10, c);
|
|
|
|
if (r1 > r2) {
|
|
|
|
arg1 = Number(arg1.toString().replace('.', ''));
|
|
|
|
arg2 = Number(arg2.toString().replace('.', '')) * cm;
|
|
|
|
} else {
|
|
|
|
arg1 = Number(arg1.toString().replace('.', '')) * cm;
|
|
|
|
arg2 = Number(arg2.toString().replace('.', ''));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
arg1 = Number(arg1.toString().replace('.', ''));
|
|
|
|
arg2 = Number(arg2.toString().replace('.', ''));
|
|
|
|
}
|
|
|
|
return (arg1 + arg2) / m;
|
|
|
|
},
|
2016-07-27 06:15:02 +00:00
|
|
|
increase() {
|
2016-11-01 03:16:56 +00:00
|
|
|
if (this.value + this.step > this.max || this.disabled) return;
|
|
|
|
this.currentValue = this.accAdd(this.step, this.value);
|
2016-07-27 06:15:02 +00:00
|
|
|
if (this.maxDisabled) {
|
|
|
|
this.inputActive = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
decrease() {
|
2016-11-01 03:16:56 +00:00
|
|
|
if (this.value - this.step < this.min || this.disabled) return;
|
|
|
|
this.currentValue = this.accSub(this.value, this.step);
|
2016-07-27 06:15:02 +00:00
|
|
|
if (this.minDisabled) {
|
|
|
|
this.inputActive = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
activeInput(disabled) {
|
|
|
|
if (!this.disabled && !disabled) {
|
|
|
|
this.inputActive = true;
|
|
|
|
}
|
|
|
|
},
|
2016-08-11 07:51:13 +00:00
|
|
|
inactiveInput(disabled) {
|
2016-07-27 06:15:02 +00:00
|
|
|
if (!this.disabled && !disabled) {
|
|
|
|
this.inputActive = false;
|
|
|
|
}
|
2016-11-01 03:16:56 +00:00
|
|
|
},
|
|
|
|
handleBlur(event) {
|
|
|
|
let value = Number(this.currentValue);
|
|
|
|
if (isNaN(value) || value > this.max || value < this.min) {
|
|
|
|
this.currentValue = this.value;
|
|
|
|
} else {
|
|
|
|
this.currentValue = value;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
handleInput(value) {
|
|
|
|
this.currentValue = value;
|
2016-07-27 06:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|