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

150 lines
3.3 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
2016-08-11 07:51:13 +00:00
v-model="currentValue"
@onchange="handleChange"
2016-07-27 06:15:02 +00:00
:disabled="disabled"
:size="size"
:number="true"
: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>
import ElInput from 'packages/input/index.js';
import { once, on } from 'wind-dom/src/event';
export default {
name: 'ElInputNumber',
props: {
value: {
2016-08-11 07:51:13 +00:00
type: Number
2016-07-27 06:15:02 +00:00
},
step: {
type: Number,
default: 1
},
max: {
type: Number,
default: Infinity
},
min: {
type: Number,
default: 0
},
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() {
return {
2016-08-11 07:51:13 +00:00
currentValue: null,
2016-07-27 06:15:02 +00:00
inputActive: false
};
},
2016-08-11 07:51:13 +00:00
watch: {
value: {
immediate: true,
handler(val) {
this.currentValue = val;
}
},
currentValue(val) {
if (!isNaN(parseInt(val, 10))) {
this.$emit('input', parseInt(val, 10));
}
}
},
2016-07-27 06:15:02 +00:00
computed: {
minDisabled() {
return this.value - this.step < this.min;
},
maxDisabled() {
return this.value + this.step > this.max;
}
},
methods: {
increase() {
if (this.value + this.step > this.max || this.disabled) return;
2016-08-11 07:51:13 +00:00
this.currentValue += this.step;
2016-07-27 06:15:02 +00:00
if (this.maxDisabled) {
this.inputActive = false;
}
},
decrease() {
if (this.value - this.step < this.min || this.disabled) return;
2016-08-11 07:51:13 +00:00
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;
}
},
2016-08-11 07:51:13 +00:00
handleChange(value) {
2016-07-27 06:15:02 +00:00
this.$emit('onchange', value);
}
}
};
</script>