LicensePlate.js 1.35 KB
// components/LicensePlate/LicensePlate.js
const list = '京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新'
import {componentsMixin } from '../../mixins/index'
Component({
  behaviors:[componentsMixin],
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean,
    activeItem: String
  },
  observers: {
    'activeItem': function (newVal) {
      this.init()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    licensePlateList: []
  },
  lifetimes: {
    attached() {
      this.init()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    choose(e) {
      let { index } = e.currentTarget.dataset
      let { licensePlateList } = this.data
      licensePlateList.map(e => e.active = false)
      licensePlateList[index].active = true
      let platPrefix = licensePlateList[index].text
      this.setData({ licensePlateList, platPrefix })
    },
    confirm() {
      let { platPrefix } = this.data
      this.triggerEvent('confirm', platPrefix || '')
      this.setData({
        show: false
      })
    },
    init() {
      let { activeItem } = this.data
      let licensePlateList = []
      list.split('').map(e => {
        licensePlateList.push({
          text: e,
          active: activeItem == e
        })
      })
      this.setData({
        licensePlateList
      })
    }
  }
})