|
@@ -1,131 +1,240 @@
|
|
|
<template>
|
|
|
- <view class="number-box">
|
|
|
- <view class="sub" @click="onSub" :class="{ disabled: subDisabled }"></view>
|
|
|
- <input type="number" class="control-input" :value="value" @input="onInput" />
|
|
|
- <view class="add" @click="onAdd" :class="{ disabled: addDisabled }"></view>
|
|
|
+ <view class="cm-number-box">
|
|
|
+ <view @click="_calcValue('minus')" class="cm-number-box__minus cm-number-box-btns">
|
|
|
+ <text
|
|
|
+ class="cm-number-box--text"
|
|
|
+ :class="{ 'cm-number-box--disabled': inputValue <= min || disabled }"
|
|
|
+ :style="{ color }"
|
|
|
+ >
|
|
|
+ -
|
|
|
+ </text>
|
|
|
+ </view>
|
|
|
+ <input
|
|
|
+ :disabled="disabled"
|
|
|
+ @focus="_onFocus"
|
|
|
+ @blur="_onBlur"
|
|
|
+ class="cm-number-box__value"
|
|
|
+ type="number"
|
|
|
+ v-model="inputValue"
|
|
|
+ :style="{ background, color }"
|
|
|
+ />
|
|
|
+ <view @click="_calcValue('plus')" class="cm-number-box__plus cm-number-box-btns">
|
|
|
+ <text
|
|
|
+ class="cm-number-box--text"
|
|
|
+ :class="{ 'cm-number-box--disabled': inputValue >= max || disabled }"
|
|
|
+ :style="{ color }"
|
|
|
+ >
|
|
|
+ +
|
|
|
+ </text>
|
|
|
+ </view>
|
|
|
</view>
|
|
|
</template>
|
|
|
-
|
|
|
<script>
|
|
|
+/**
|
|
|
+ * NumberBox 数字输入框
|
|
|
+ * @description 带加减按钮的数字输入框
|
|
|
+ * @tutorial https://ext.dcloud.net.cn/plugin?id=31
|
|
|
+ * @property {Number} value 输入框当前值
|
|
|
+ * @property {Number} min 最小值
|
|
|
+ * @property {Number} max 最大值
|
|
|
+ * @property {Number} step 每次点击改变的间隔大小
|
|
|
+ * @property {String} background 背景色
|
|
|
+ * @property {String} color 字体颜色(前景色)
|
|
|
+ * @property {Boolean} disabled = [true|false] 是否为禁用状态
|
|
|
+ * @event {Function} change 输入框值改变时触发的事件,参数为输入框当前的 value
|
|
|
+ * @event {Function} focus 输入框聚焦时触发的事件,参数为 event 对象
|
|
|
+ * @event {Function} blur 输入框失焦时触发的事件,参数为 event 对象
|
|
|
+ */
|
|
|
+
|
|
|
export default {
|
|
|
- model: {
|
|
|
- prop: 'value',
|
|
|
- event: 'input'
|
|
|
- },
|
|
|
+ name: 'UniNumberBox',
|
|
|
+ emits: ['change', 'input', 'update:modelValue', 'blur', 'focus'],
|
|
|
props: {
|
|
|
+ value: {
|
|
|
+ type: [Number, String],
|
|
|
+ default: 1
|
|
|
+ },
|
|
|
+ modelValue: {
|
|
|
+ type: [Number, String],
|
|
|
+ default: 1
|
|
|
+ },
|
|
|
min: {
|
|
|
type: Number,
|
|
|
default: 1
|
|
|
},
|
|
|
max: {
|
|
|
type: Number,
|
|
|
- default: 99999
|
|
|
+ default: 100
|
|
|
},
|
|
|
- value: {
|
|
|
+ step: {
|
|
|
type: Number,
|
|
|
default: 1
|
|
|
+ },
|
|
|
+ background: {
|
|
|
+ type: String,
|
|
|
+ default: '#f5f5f5'
|
|
|
+ },
|
|
|
+ color: {
|
|
|
+ type: String,
|
|
|
+ default: '#333'
|
|
|
+ },
|
|
|
+ disabled: {
|
|
|
+ type: Boolean,
|
|
|
+ default: false
|
|
|
}
|
|
|
},
|
|
|
- computed: {
|
|
|
- addDisabled() {
|
|
|
- return this.value === this.max
|
|
|
- },
|
|
|
- subDisabled() {
|
|
|
- return this.value === this.min
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ inputValue: 0
|
|
|
}
|
|
|
},
|
|
|
watch: {
|
|
|
- value(nval) {
|
|
|
- console.log(nval)
|
|
|
+ value(val) {
|
|
|
+ this.inputValue = +val
|
|
|
+ },
|
|
|
+ modelValue(val) {
|
|
|
+ this.inputValue = +val
|
|
|
+ }
|
|
|
+ },
|
|
|
+ created() {
|
|
|
+ if (this.value === 1) {
|
|
|
+ this.inputValue = +this.modelValue
|
|
|
+ }
|
|
|
+ if (this.modelValue === 1) {
|
|
|
+ this.inputValue = +this.value
|
|
|
}
|
|
|
},
|
|
|
methods: {
|
|
|
- // 减法
|
|
|
- onSub() {
|
|
|
- if (this.subDisabled) return
|
|
|
- let inputValue = this.value
|
|
|
- if (inputValue <= this.min) {
|
|
|
- inputValue = this.min
|
|
|
- } else {
|
|
|
- inputValue--
|
|
|
+ _calcValue(type) {
|
|
|
+ if (this.disabled) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const scale = this._getDecimalScale()
|
|
|
+ let value = this.inputValue * scale
|
|
|
+ let step = this.step * scale
|
|
|
+ if (type === 'minus') {
|
|
|
+ value -= step
|
|
|
+ if (value < this.min * scale) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (value > this.max * scale) {
|
|
|
+ value = this.max * scale
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (type === 'plus') {
|
|
|
+ value += step
|
|
|
+ if (value > this.max * scale) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (value < this.min * scale) {
|
|
|
+ value = this.min * scale
|
|
|
+ }
|
|
|
}
|
|
|
- this.$emit('input', inputValue)
|
|
|
- this.$emit('change', inputValue)
|
|
|
+
|
|
|
+ this.inputValue = (value / scale).toFixed(String(scale).length - 1)
|
|
|
+ this.$emit('change', +this.inputValue)
|
|
|
+ // TODO vue2 兼容
|
|
|
+ this.$emit('input', +this.inputValue)
|
|
|
+ // TODO vue3 兼容
|
|
|
+ this.$emit('update:modelValue', +this.inputValue)
|
|
|
},
|
|
|
- // 加法
|
|
|
- onAdd() {
|
|
|
- if (this.addDisabled) return
|
|
|
- let inputValue = this.value
|
|
|
- if (inputValue >= this.max) {
|
|
|
- inputValue = this.max
|
|
|
- } else {
|
|
|
- inputValue++
|
|
|
+ _getDecimalScale() {
|
|
|
+ let scale = 1
|
|
|
+ // 浮点型
|
|
|
+ if (~~this.step !== this.step) {
|
|
|
+ scale = Math.pow(10, String(this.step).split('.')[1].length)
|
|
|
}
|
|
|
- this.$emit('input', inputValue)
|
|
|
- this.$emit('change', inputValue)
|
|
|
+ return scale
|
|
|
},
|
|
|
- // 失去焦点
|
|
|
- onInput(e) {
|
|
|
- let inputValue = parseInt(e.detail.value)
|
|
|
- if (isNaN(inputValue)) {
|
|
|
- inputValue = this.min
|
|
|
+ _onBlur(event) {
|
|
|
+ this.$emit('blur', event)
|
|
|
+ let value = event.detail.value
|
|
|
+ if (!value) {
|
|
|
+ // this.inputValue = 0;
|
|
|
+ return
|
|
|
}
|
|
|
- if (inputValue < this.min) {
|
|
|
- inputValue = this.min
|
|
|
- } else if (inputValue > this.max) {
|
|
|
- inputValue = this.max
|
|
|
+ value = +value
|
|
|
+ if (value > this.max) {
|
|
|
+ value = this.max
|
|
|
+ } else if (value < this.min) {
|
|
|
+ value = this.min
|
|
|
}
|
|
|
- this.$emit('input', inputValue)
|
|
|
- this.$emit('change', inputValue)
|
|
|
+ const scale = this._getDecimalScale()
|
|
|
+ this.inputValue = value.toFixed(String(scale).length - 1)
|
|
|
+ this.$emit('change', +this.inputValue)
|
|
|
+ this.$emit('input', +this.inputValue)
|
|
|
+ },
|
|
|
+ _onFocus(event) {
|
|
|
+ this.$emit('focus', event)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
</script>
|
|
|
-
|
|
|
<style lang="scss" scoped>
|
|
|
-.number-box {
|
|
|
- width: 148rpx;
|
|
|
- height: 50rpx;
|
|
|
+$box-height: 48rpx;
|
|
|
+$bg: #fff;
|
|
|
+$br: 4rpx;
|
|
|
+$color: #333;
|
|
|
+
|
|
|
+.cm-number-box {
|
|
|
+ /* #ifndef APP-NVUE */
|
|
|
+ display: flex;
|
|
|
+ /* #endif */
|
|
|
+ flex-direction: row;
|
|
|
+}
|
|
|
+
|
|
|
+.cm-number-box-btns {
|
|
|
+ /* #ifndef APP-NVUE */
|
|
|
+ display: flex;
|
|
|
+ /* #endif */
|
|
|
+ flex-direction: row;
|
|
|
+ align-items: center;
|
|
|
+ justify-content: center;
|
|
|
+ padding: 0 16rpx;
|
|
|
+ background-color: $bg;
|
|
|
+ /* #ifdef H5 */
|
|
|
+ cursor: pointer;
|
|
|
+ /* #endif */
|
|
|
border: 1rpx solid #e1e1e1;
|
|
|
- border-radius: 25rpx;
|
|
|
- @extend .cm-flex-between;
|
|
|
+}
|
|
|
|
|
|
- .add,
|
|
|
- .sub {
|
|
|
- @extend .cm-flex-center;
|
|
|
- flex: 1;
|
|
|
- font-size: 24rpx;
|
|
|
- color: #666666;
|
|
|
+.cm-number-box__value {
|
|
|
+ background-color: $bg;
|
|
|
+ width: 48rpx;
|
|
|
+ height: $box-height;
|
|
|
+ text-align: center;
|
|
|
+ font-size: 26rpx;
|
|
|
+ border: 2rpx solid #e1e1e1;
|
|
|
+ border-left-width: 0;
|
|
|
+ border-right-width: 0;
|
|
|
+ color: $color;
|
|
|
+ background-color: #e1e1e1;
|
|
|
+}
|
|
|
|
|
|
- &.disabled {
|
|
|
- color: #cccccc;
|
|
|
- }
|
|
|
- }
|
|
|
+.cm-number-box__minus {
|
|
|
+ border-top-left-radius: $box-height / 2;
|
|
|
+ border-bottom-left-radius: $box-height / 2;
|
|
|
+}
|
|
|
|
|
|
- .add {
|
|
|
- &::before {
|
|
|
- margin-bottom: 2rpx;
|
|
|
- content: '+';
|
|
|
- }
|
|
|
- }
|
|
|
+.cm-number-box__plus {
|
|
|
+ border-top-right-radius: $box-height / 2;
|
|
|
+ border-bottom-right-radius: $box-height / 2;
|
|
|
+}
|
|
|
|
|
|
- .sub {
|
|
|
- &::before {
|
|
|
- margin-bottom: 2rpx;
|
|
|
- content: '-';
|
|
|
- }
|
|
|
- }
|
|
|
+.cm-number-box--text {
|
|
|
+ // fix nvue
|
|
|
+ line-height: 44rpx;
|
|
|
|
|
|
- .control-input {
|
|
|
- width: 56rpx;
|
|
|
- height: 48rpx;
|
|
|
- border-right: 1rpx solid #e1e1e1;
|
|
|
- border-left: 1rpx solid #e1e1e1;
|
|
|
- background-color: #f7f7f7;
|
|
|
- box-sizing: border-box;
|
|
|
- text-align: center;
|
|
|
- font-size: 26rpx;
|
|
|
- color: #333;
|
|
|
- }
|
|
|
+ font-size: 32rpx;
|
|
|
+ font-weight: 300;
|
|
|
+ color: $color;
|
|
|
+}
|
|
|
+
|
|
|
+.cm-number-box .cm-number-box--disabled {
|
|
|
+ color: #c0c0c0 !important;
|
|
|
+ /* #ifdef H5 */
|
|
|
+ cursor: not-allowed;
|
|
|
+ /* #endif */
|
|
|
}
|
|
|
</style>
|