selectPopup.js 5.4 KB
// packageWorkspace/pages/performanceRating/components/selectPopup/selectPopup.js
const IMG_URL = 'https://img02.glsx.com.cn/weapp/resource/miniprogram/regional-assistant/'
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
    },
    height: {
      type: String
    },
    allowSearch: {
      type: Boolean
    }
  },
  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.setData({
          searchValue: ''
        })
        this.initDisplay(this.properties.options || [], this.properties.multiple, this.properties.value, this.properties.values)
      }
    }
  },
  methods: {
    initDisplay(options = [], multiple = false, value = null, values = []) {
      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,
        copyDisplayOptions: wx.$util.deepClone(display),
        selectedValues: initSelected
      })
    },
    toggle(e) {
      const { index, item } = e.currentTarget.dataset
      const { multiple, max, valueKey } = this.properties
      let { displayOptions, copyDisplayOptions } = this.data
      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 弹窗,保持组件纯净
            this.triggerEvent('maxtips')
            return
          }
        }
        const selectIndex = displayOptions?.findIndex(v => v[valueKey] == item[valueKey])
        this.setData({ [`displayOptions[${selectIndex}].checked`]: nextChecked })
        const copySelectIndex = copyDisplayOptions?.findIndex(v => v[valueKey] == item[valueKey])
        this.setData({ [`copyDisplayOptions[${copySelectIndex}].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()
        }
      }
    },
    search(e) {
      let { labelKey, copyDisplayOptions, displayOptions, selectedValues, valueKey } = this.data
      let { value } = e.detail
      if (!value) {
        copyDisplayOptions.map(i => {
          i.checked = false
          if (selectedValues.includes(i[valueKey])) i.checked = true
          return i
        })
        this.setData({
          displayOptions: copyDisplayOptions
        })
      } else {
        let arr = []
        copyDisplayOptions.map(i => {
          i.checked = false
          if (selectedValues.includes(i[valueKey])) i.checked = true
          if (i[labelKey].indexOf(value) != -1) {
            arr.push(i)
          }
        })
        this.setData({
          displayOptions: arr
        })
      }
    },
    onCancel() {
      this.triggerEvent('cancel')
    },
    onConfirm() {
      const values = this.data.selectedValues
      const items = this.getSelectedItems()
      this.triggerEvent('confirm', { values, items })
    },
    getSelectedItems() {
      return (this.data.copyDisplayOptions || []).filter(it => it.checked)
    }
  }
})