数字键盘组件

numPad.vue

<template>
  <div class="num-pad" :class="{ 'num-pad--visible': visible }">
    <div class="num-pad__header" v-if="withHeader">
      <span>{{ header }}</span>
      <button class="num-pad__close" @click="handleClose">X</button>
    </div>
    <div class="num-pad__body">
      <div v-for="(row, index) in rows" :key="index" class="num-pad__row">
        <div v-for="(num, index) in row" :key="index" class="num-pad__item" @click="handleClick(num)">
          {{ num }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'NumPad',
  props: {
    value: [Number, String],
    point: Boolean,
    withHeader: Boolean,
    header: String,
    visible: Boolean
  },
  data() {
    return {
      rows: [],
      canDelete: false
    }
  },
  created() {
    // 初始化数字键盘
    this.initRows();
  },
  methods: {
    initRows() {
      const { point } = this;
      if (point) {
        this.rows = [
          [1, 2, 3],
          [4, 5, 6],
          [7, 8, 9],
          ['.', 0, '<']
        ];
      } else {
        this.rows = [
          [1, 2, 3],
          [4, 5, 6],
          [7, 8, 9],
          ['', 0, '<']
        ];
      }
    },
    handleClick(num) {
      const { canDelete } = this;
      const { point } = this;
      const value  = this.value.toString();
      if (num === '') {
        return;
      }
      if (num === '.') {
        if (!value.includes('.') && point) {
          this.$emit('input', value + num);
          this.canDelete = true;
        }
      } else if (num === '<') {
        if (canDelete) {
          this.$emit('input', value.slice(0, -1));
          this.canDelete = value.length > 1;
        }
      } else if (value === '') {
        this.$emit('input', num);
        this.canDelete = true;
      } else {
        this.$emit('input', value + num);
        this.canDelete = true;
      }
    },
    handleClose() {
      this.$emit('update:visible', false);
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.canDelete = this.value !== '';
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.num-pad {
  display: flex;
  flex-wrap: wrap;
  width: 250px;
  background-color: #fff;
  box-shadow: 0 1px 5px rgba(0,0,0,.2);
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  transition: all .3s ease-out;
  z-index: 10;
  opacity: 0;
  &.num-pad--visible {
    transform: translateX(-50%) translateY(-100%);
    opacity: 1;
  }
  .num-pad__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    height: 40px;
    width: 100%;
    font-size: 18px;
    border-bottom: 1px solid #ccc;
    background-color: #f6f7f9;
  }
  .num-pad__body {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    height: 250px;
    width: 100%;
    padding: 10px;
  }
  .num-pad__row {
    display: flex;
    justify-content: space-between;
    width: 100%;
    margin-bottom: 10px;
  }
  .num-pad__item {
    width: 30%;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #ccc;
    font-size: 24px;
    cursor: pointer;
    &:active {
      background-color: #f6f7f9;
    }
  }
  .num-pad__close {
    font-size: 24px;
    color: #999;
    border: none;
    background-color: transparent;
    cursor: pointer;
    &:focus,
    &:hover {
      color: #444;
    }
  }
}
</style>

使用 <num-pad v-model="form.number" :visible.sync="numPadVisible" point withHeader header="请输入数量" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kowalsik

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值