all files / src/controllCount/ index.vue

100% Statements 5/5
100% Branches 8/8
100% Functions 1/1
100% Lines 5/5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                                                                                                   
<template>
  <div class="am-controll-count" :data-maxcount="maxCount">
    <button :class="['btn prev', {disabled: count<=1}]" @click="computedNum(-1)">-</button>
    <input type="text" v-model="count" readonly="readonly">
    <button :class="['btn next', {disabled: count>=maxCount}]" @click="computedNum(1)">+</button>
  </div>
</template>
<script>
  export default {
    name: "controllNum",
    props: {
      count: {
        type: Number,
        default: 1
      },
      maxCount: {
        type: Number,
        default: 10
      }
    },
    methods: {
      computedNum(val) {
        if (val < 0 && this.count === 1) {
          return;
        }
        if (val > 0 && this.count >= this.maxCount) {
          return;
        }
        this.$emit('update:count',this.count + val);
      }
    }
  };
</script>
<style scoped lang="scss">
  @import "var";
  .am-controll-count {
    display: flex;
    align-items: center;
    width: 100%;
    input,
    .btn {
      text-align: center;
      line-height: 30px;
      color: #666;
      background: #ffffff;
      outline: none;
      box-sizing: border-box;
    }
    .btn {
      border: 1px solid #ebebeb;
      flex: 0 0 34px;
      width: 32px;
      font-size: 20px;
      cursor: pointer;
      &:nth-child(1) {
        border-radius: $border-radius 0 0 $border-radius;
      }
      &:nth-child(3) {
        border-radius: 0 $border-radius $border-radius 0;
      }
    }
    input {
      flex: 1;
      min-width: 34px;
      color: #333;
      font-size: 16px;
      border: none;
      border-top: 1px solid #ebebeb;
      border-bottom: 1px solid #ebebeb;
      border-radius: 0;
      -webkit-appearance: none;
    }
    .disabled {
      color:#aaa;
      background: #ebebeb;
    }
  }
</style>