SelectPopup.js 4.07 KB
// components/SelectPopup/SelectPopup.js
import { IMG_URL } from '~/utils/constantData'
Component({
  options: {
    addGlobalClass: true
  },
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    title: {
      type: String,
      value: ''
    },
    // 原始选项列表,支持任意字段名,通过 labelKey/valueKey 映射
    options: {
      type: Array,
      value: []
    },
    // 是否多选
    multiple: {
      type: Boolean,
      value: false
    },
    // 单选初始值
    value: {
      type: null,
      value: null
    },
    // 多选初始值
    values: {
      type: Array,
      value: []
    },
    // 字段映射
    labelKey: {
      type: String,
      value: 'name'
    },
    valueKey: {
      type: String,
      value: 'value'
    },
    // 多选最大数量(不限制则设为 0 或不传)
    max: {
      type: Number,
      value: 0
    },
    // 单选时点击即确认
    confirmOnSelect: {
      type: Boolean,
      value: false
    }
  },
  data: {
    IMG_URL,
    displayOptions: [], // 带 checked 的渲染选项
    selectedValues: [] // 选中的值列表(单选也统一用数组)
  },
  observers: {
    'options, multiple, value, values': function(options, multiple, value, values) {
      this.initDisplay(options || [], multiple, value, values)
    },
    show(n) {
      // 进出场时保持数据一致
      if (n) {
        this.initDisplay(this.properties.options || [], this.properties.multiple, this.properties.value, this.properties.values)
      }
    }
  },
  methods: {
    initDisplay(options = [], multiple = false, value = null, values = []) {
      console.log(value)
      const vKey = this.properties.valueKey
      // 初始选中值统一转为数组
      const initSelected = multiple ? (Array.isArray(values) ? values.slice() : []) : (value !== null && value !== undefined ? value : [])
      const display = (options || []).map(item => {
        const val = item[vKey]
        const checked = initSelected?.some(v => v === val)
        return { ...item, checked }
      })
      this.setData({
        displayOptions: display,
        selectedValues: initSelected
      })
    },
    toggle(e) {
      const { index } = e.currentTarget.dataset
      const { multiple, max, valueKey } = this.properties
      const item = this.data.displayOptions[index]
      if (!item || item.disabled) return

      if (multiple) {
        let nextChecked = !item.checked
        // 限制最大数量
        if (nextChecked && max > 0) {
          const selectedCount = this.data.selectedValues.length
          if (selectedCount >= max) {
            // 可根据项目规范提示用户,这里不做 UI 弹窗,保持组件纯净
            return
          }
        }
        this.setData({ [`displayOptions[${index}].checked`]: nextChecked })
        const val = item[valueKey]
        let selected = this.data.selectedValues.slice()
        const pos = selected.findIndex(v => v === val)
        if (nextChecked && pos === -1) selected.push(val)
        if (!nextChecked && pos > -1) selected.splice(pos, 1)
        this.setData({ selectedValues: selected })
        this.triggerEvent('change', { values: selected, items: this.getSelectedItems() })
      } else {
        // 单选:取消其他,选中当前
        const currentVal = item[valueKey]
        const display = this.data.displayOptions.map((it, idx) => ({ ...it, checked: idx === index ? !it.checked : false }))
        const selected = display[index].checked ? [currentVal] : []
        this.setData({ displayOptions: display, selectedValues: selected })
        this.triggerEvent('change', { values: selected, items: this.getSelectedItems() })
        if (this.properties.confirmOnSelect) {
          this.onConfirm()
        }
      }
    },
    onCancel() {
      this.triggerEvent('cancel')
    },
    onConfirm() {
      const values = this.data.selectedValues
      const items = this.getSelectedItems()
      this.triggerEvent('confirm', { values, items })
    },
    getSelectedItems() {
      return (this.data.displayOptions || []).filter(it => it.checked)
    }
  }
})