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

230 lines
5.8 KiB
Vue
Raw Normal View History

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 : '',
{ 'is-disabled': disabled },
{ 'is-without-controls': !controls}
2016-07-27 06:15:02 +00:00
]"
>
<el-input
:value="currentValue"
@keydown.up.native="increase"
@keydown.down.native="decrease"
@blur="handleBlur"
@input="handleInput"
2016-07-27 06:15:02 +00:00
:disabled="disabled"
:size="size"
:class="{
'is-active': inputActive
}">
2016-12-13 06:46:47 +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>
</template>
2016-07-27 06:15:02 +00:00
</el-input>
<span
v-if="controls"
2016-07-27 06:15:02 +00:00
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
v-if="controls"
2016-07-27 06:15:02 +00:00
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
},
value: {
default: 0
},
2016-07-27 06:15:02 +00:00
disabled: Boolean,
size: String,
controls: {
type: Boolean,
default: true
}
2016-07-27 06:15:02 +00:00
},
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) {
let value = Number(newVal);
if (value <= this.max && value >= this.min) {
this.$emit('change', value, oldVal);
this.$emit('input', value);
2016-08-11 07:51:13 +00:00
}
}
},
2016-07-27 06:15:02 +00:00
computed: {
minDisabled() {
return this.value - this.step < this.min;
2016-07-27 06:15:02 +00:00
},
maxDisabled() {
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-24 06:48:56 +00:00
const value = this.value || 0;
if (value + this.step > this.max || this.disabled) return;
this.currentValue = this.accAdd(this.step, value);
2016-07-27 06:15:02 +00:00
if (this.maxDisabled) {
this.inputActive = false;
}
},
decrease() {
2016-11-24 06:48:56 +00:00
const value = this.value || 0;
if (value - this.step < this.min || this.disabled) return;
this.currentValue = this.accSub(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;
}
},
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>