Улучшение NumInput

This commit is contained in:
Book Pauk
2022-10-13 21:19:18 +07:00
parent 817f018d4d
commit 4b2e63bb5b

View File

@@ -6,15 +6,26 @@
class="no-mp" class="no-mp"
:class="(error ? 'error' : '')" :class="(error ? 'error' : '')"
:disable="disable" :disable="disable"
:mask="mask"
> >
<slot></slot> <slot></slot>
<template #prepend> <template #prepend>
<q-icon
v-show="mmButtons"
v-ripple="modelValue != min"
style="font-size: 100%"
:class="(modelValue != min ? '' : 'disable')"
name="la la-angle-double-left"
class="button"
@click="toMin"
/>
<q-icon <q-icon
v-ripple="validate(modelValue - step)" v-ripple="validate(modelValue - step)"
:class="(validate(modelValue - step) ? '' : 'disable')" :class="(validate(modelValue - step) ? '' : 'disable')"
name="la la-minus-circle" :name="minusIcon"
class="button" class="button"
@click="minus" @click="onClick('minus')"
@mousedown.prevent.stop="onMouseDown($event, 'minus')" @mousedown.prevent.stop="onMouseDown($event, 'minus')"
@mouseup.prevent.stop="onMouseUp" @mouseup.prevent.stop="onMouseUp"
@mouseout.prevent.stop="onMouseUp" @mouseout.prevent.stop="onMouseUp"
@@ -27,9 +38,9 @@
<q-icon <q-icon
v-ripple="validate(modelValue + step)" v-ripple="validate(modelValue + step)"
:class="(validate(modelValue + step) ? '' : 'disable')" :class="(validate(modelValue + step) ? '' : 'disable')"
name="la la-plus-circle" :name="plusIcon"
class="button" class="button"
@click="plus" @click="onClick('plus')"
@mousedown.prevent.stop="onMouseDown($event, 'plus')" @mousedown.prevent.stop="onMouseDown($event, 'plus')"
@mouseup.prevent.stop="onMouseUp" @mouseup.prevent.stop="onMouseUp"
@mouseout.prevent.stop="onMouseUp" @mouseout.prevent.stop="onMouseUp"
@@ -37,6 +48,16 @@
@touchend.stop="onTouchEnd" @touchend.stop="onTouchEnd"
@touchcancel.prevent.stop="onTouchEnd" @touchcancel.prevent.stop="onTouchEnd"
/> />
<q-icon
v-show="mmButtons"
v-ripple="modelValue != max"
style="font-size: 100%"
:class="(modelValue != max ? '' : 'disable')"
name="la la-angle-double-right"
class="button"
@click="toMax"
/>
</template> </template>
</q-input> </q-input>
</template> </template>
@@ -49,17 +70,18 @@ import * as utils from '../../share/utils';
const componentOptions = { const componentOptions = {
watch: { watch: {
filteredValue: function(newValue) { filteredValue() {
if (this.validate(newValue)) { this.checkErrorAndEmit(true);
this.error = false;
this.$emit('update:modelValue', this.string2number(newValue));
} else {
this.error = true;
}
}, },
modelValue: function(newValue) { modelValue(newValue) {
this.filteredValue = newValue; this.filteredValue = newValue;
}, },
min() {
this.checkErrorAndEmit();
},
max() {
this.checkErrorAndEmit();
}
} }
}; };
class NumInput { class NumInput {
@@ -70,7 +92,11 @@ class NumInput {
max: { type: Number, default: Number.MAX_VALUE }, max: { type: Number, default: Number.MAX_VALUE },
step: { type: Number, default: 1 }, step: { type: Number, default: 1 },
digits: { type: Number, default: 0 }, digits: { type: Number, default: 0 },
disable: Boolean disable: Boolean,
minusIcon: {type: String, default: 'la la-minus-circle'},
plusIcon: {type: String, default: 'la la-plus-circle'},
mmButtons: Boolean,
mask: String,
}; };
filteredValue = 0; filteredValue = 0;
@@ -95,6 +121,16 @@ class NumInput {
return true; return true;
} }
checkErrorAndEmit(emit = false) {
if (this.validate(this.filteredValue)) {
this.error = false;
if (emit)
this.$emit('update:modelValue', this.string2number(this.filteredValue));
} else {
this.error = true;
}
}
plus() { plus() {
const newValue = this.modelValue + this.step; const newValue = this.modelValue + this.step;
if (this.validate(newValue)) if (this.validate(newValue))
@@ -107,23 +143,42 @@ class NumInput {
this.filteredValue = newValue; this.filteredValue = newValue;
} }
onClick(way) {
if (this.clickRepeat)
return;
if (way == 'plus') {
this.plus();
} else {
this.minus();
}
}
onMouseDown(event, way) { onMouseDown(event, way) {
this.startClickRepeat = true; this.startClickRepeat = true;
this.clickRepeat = false; this.clickRepeat = false;
if (event.button == 0) { if (event.button == 0) {
(async() => { (async() => {
await utils.sleep(300); if (this.inRepeatFunc)
if (this.startClickRepeat) { return;
this.clickRepeat = true;
while (this.clickRepeat) { this.inRepeatFunc = true;
if (way == 'plus') { try {
this.plus(); await utils.sleep(300);
} else { if (this.startClickRepeat) {
this.minus(); this.clickRepeat = true;
while (this.clickRepeat) {
if (way == 'plus') {
this.plus();
} else {
this.minus();
}
await utils.sleep(100);
} }
await utils.sleep(50);
} }
} finally {
this.inRepeatFunc = false;
} }
})(); })();
} }
@@ -133,7 +188,12 @@ class NumInput {
if (this.inTouch) if (this.inTouch)
return; return;
this.startClickRepeat = false; this.startClickRepeat = false;
this.clickRepeat = false; if (this.clickRepeat) {
(async() => {
await utils.sleep(50);
this.clickRepeat = false;
})();
}
} }
onTouchStart(event, way) { onTouchStart(event, way) {
@@ -151,6 +211,14 @@ class NumInput {
this.inTouch = false; this.inTouch = false;
this.onMouseUp(); this.onMouseUp();
} }
toMin() {
this.filteredValue = this.min;
}
toMax() {
this.filteredValue = this.max;
}
} }
export default vueComponent(NumInput); export default vueComponent(NumInput);
@@ -165,7 +233,9 @@ export default vueComponent(NumInput);
.button { .button {
font-size: 130%; font-size: 130%;
border-radius: 20px; border-radius: 15px;
width: 30px;
height: 30px;
color: #bbb; color: #bbb;
cursor: pointer; cursor: pointer;
} }