123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <template>
- <view class="set-count flex aic" :class="{'mini': size === 'mini'}">
- <view class="box box-left" :class="{'mini': size === 'mini'}"
- :style="{color:count <= countMin || disabled?'#BDC1C4':'#000', background:count <= countMin || disabled?'#F2F3F5':''}"
- @click.stop="cut(count)">-
- </view>
- <view class="one" :style="{width: size === 'max' ? '156rpx' : '100rpx'}">
- <u--input :customStyle="{width: '100%', height: '100%'}" v-model="count" :disabled="disabled" value="count"
- border="none" @focus="focus" @blur="blur" inputAlign="center" @input="inputVal"></u--input>
- </view>
- <view class="box box-right" :class="{'mini': size === 'mini'}"
- :style="{color:count >= countStock || count >= countmax || disabled?'#BDC1C4':'#000', background:count >= countStock || count >= countmax || disabled?'#F2F3F5':''}"
- @click.stop="add(count)">+
- </view>
- </view>
- </template>
- <script>
- export default {
- props: {
- disabled: Boolean,
- size: {
- type: String,
- default: 'max'
- },
- countStock: { // 库存值
- type: Number,
- default: 99999,
- },
- countmax: { //最大值
- type: Number,
- default: 99999,
- },
- countMin: { // 最小值
- type: Number,
- default: 1,
- },
- value: { // 绑定值
- type: [Number, String],
- default: 1
- },
- step: { // 每次加减数
- type: [Number, String],
- default: 1
- }
- },
- data() {
- return {
- isCtrl: false,
- count: 0,
- isInit: true // 是否首次进入
- }
- },
- mounted() {
- this.count = this.countMin > this.value ? +this.countMin : +this.value
- },
- watch: {
- value(v) {
- this.count = this.countMin > this.value ? +this.countMin : +this.value
- }
- },
- methods: {
- setCount(count) {
- this.count = count
- this.$showToast({
- title: `最大设置${count}`,
- mask: false
- })
- },
- focus() {
- this.isCtrl = false
- },
- blur() {
- this.$nextTick(() => {
- if (this.count > this.countStock || this.count > this.countmax) {
- this.setCount(this.countStock <= this.countmax ? this.countStock : this.countmax)
- this.$emit('input', this.count)
- this.$emit('change', this.count)
- } else {
- if (this.count < this.countMin) {
- this.count = this.countMin
- }
- this.$emit('input', this.count)
- this.$emit('change', this.count)
- }
- })
- },
- inputVal() {
- this.$nextTick(() => {
- const inputType = /^0|[^\d]+/g
- this.count = String(this.count).replace(inputType, '')
- })
- },
- // 增加数量
- add() {
- if (this.disabled) {
- return
- }
- this.count = +this.count
- if (this.count < this.countmax && this.count < this.countStock) {
- this.count += Number(this.step)
- this.$emit('input', this.count)
- this.$emit('change', this.count)
- }
- },
- // 减少数量
- cut() {
- if (this.disabled) {
- return
- }
- this.isCtrl = true
- this.count = +this.count
- if (this.count > this.countMin) {
- this.count -= Number(this.step)
- this.$emit('input', this.count)
- this.$emit('change', this.count)
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .set-count {
- height: 72rpx;
- color: $color-text-m;
- border-radius: 4rpx;
- border: 2rpx solid #DCDFE6;
- &.mini {
- height: 58rpx;
- }
- .box {
- display: flex;
- width: 70rpx;
- height: 70rpx;
- justify-content: center;
- align-items: center;
- &.mini {
- width: 56rpx;
- height: 56rpx;
- }
- }
- .box-left {
- border-right: 2rpx solid #DCDFE6;
- }
- .box-right {
- border-left: 2rpx solid #DCDFE6;
- }
- .one {
- width: 156rpx;
- height: 100%;
- }
- }
- </style>
|