Stepper.js 1.29 KB
// components/Stepper/Stepper.js
import {componentsMixin } from '../../mixins/index'
Component({
  behaviors:[componentsMixin],
  /**
   * 组件的属性列表
   */
  properties: {
    count: Number,
    min: {
      type: Number,
      value: 1
    },
    max: {
      type: Number,
      value: 99,
      observer(n) {
        if (n && this.data.count > n) {
          this.setData({ count: n })
          this.triggerEvent('change', Number(n))
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    input(e) {
      let { value } = e.detail
      let {
        max,
      } = this.data
      if (value > max) value = max
      this.setData({ count: value })
    },
    tap() { },
    blur(e) {
      let { value } = e.detail
      let {
        min,
      } = this.data
      if (value < min) value = min
      this.setData({ count: value })
      this.triggerEvent('change', Number(value))
    },
    change(e) {
      let {
        add
      } = e.currentTarget.dataset
      let num = add ? 1 : -1
      let {
        count,
        min,
        max
      } = this.data
      if (count == min && !add) return
      if (count == max && add) return
      count += num
      this.triggerEvent('change', Number(count))
    }
  }
})