element/packages/input-number/src/input-number.vue

203 lines
5.0 KiB
Vue
Raw Normal View History

2016-07-27 06:15:02 +00:00
<template>
<div class="el-input-number"
:class="[
size ? 'is-' + size : '',
{ 'is-disabled': disabled }
]"
>
<el-input
v-model.number="currentValue"
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-13 03:12:24 +00:00
import ElInput from 'element-ui/packages/input/index.js';
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
},
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() {
// 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 {
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) {
if (newVal <= this.max && newVal >= this.min) {
2016-09-01 07:56:04 +00:00
this.$emit('change', newVal);
this.$emit('input', newVal);
} else {
this.$nextTick(() => {
this.currentValue = oldVal;
});
2016-08-11 07:51:13 +00:00
}
}
},
2016-07-27 06:15:02 +00:00
computed: {
minDisabled() {
2016-09-01 07:56:04 +00:00
return this.currentValue - this.step < this.min;
2016-07-27 06:15:02 +00:00
},
maxDisabled() {
2016-09-01 07:56:04 +00:00
return this.currentValue + 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-09-01 07:56:04 +00:00
if (this.currentValue + this.step > this.max || this.disabled) return;
this.currentValue = this.accAdd(this.step, this.currentValue);
2016-07-27 06:15:02 +00:00
if (this.maxDisabled) {
this.inputActive = false;
}
},
decrease() {
2016-09-01 07:56:04 +00:00
if (this.currentValue - this.step < this.min || this.disabled) return;
this.currentValue = this.accSub(this.currentValue, 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;
}
}
}
};
</script>