form-picker-popup.js 3.61 KB
/**
 * 选项弹框 — single 单选 / multiple 多选
 * @see packageDynamicForm/docs/field-options-api.md
 *
 * mode=single:点选替换 draft,confirm 返回标量
 * mode=multiple:toggle 多选,confirm 返回数组
 */
const { normalizePickerValues, buildPickerOptionRows } = require('./picker-value.js')

Component({
	properties: {
		show: {
			type: Boolean,
			value: false
		},
		title: {
			type: String,
			value: '请选择'
		},
		height: {
			type: String,
			value: '70%'
		},
		options: {
			type: Array,
			value: []
		},
		value: {
			type: null,
			value: []
		},
		loading: {
			type: Boolean,
			value: false
		},
		mode: {
			type: String,
			value: 'multiple'
		},
		searchable: {
			type: Boolean,
			value: false
		},
		searchKeyword: {
			type: String,
			value: ''
		}
	},

	data: {
		optionRows: [],
		draftValues: [],
		isFullSucreen: false,
		emptyText: '暂无可选项'
	},

	lifetimes: {
		attached() {
			this.setData({
				isFullSucreen: wx.$util?.isFullSucreen?.(true) || false
			})
		}
	},

	observers: {
		show: function (show) {
			if (show) {
				wx.hideKeyboard?.()
				this.syncOptionRows()
			}
		},
		'options, value, loading': function () {
			if (this.properties.show && !this.properties.loading) {
				this.syncOptionRows()
			}
		},
		'searchable, searchKeyword': function (searchable, searchKeyword) {
			if (!this.properties.show || this.properties.loading) return
			this.updateEmptyText(searchable, searchKeyword)
		}
	},

	methods: {
		noScroll() {},

		/** show/options/value 变化时同步 optionRows 与 draftValues 选中态 */
		syncOptionRows() {
			const draftValues = normalizePickerValues(this.properties.value)
			const optionRows = buildPickerOptionRows(this.properties.options, this.properties.value)
			this.updateEmptyText(this.properties.searchable, this.properties.searchKeyword)
			this.setData({
				optionRows,
				draftValues
			})
		},

		/** 搜索态空列表文案对齐 Merchant-Seles */
		updateEmptyText(searchable, searchKeyword) {
			const emptyText =
				searchable && searchKeyword ? '暂无数据' : '暂无可选项'
			if (emptyText !== this.data.emptyText) {
				this.setData({ emptyText })
			}
		},

		/** search-input 确认搜索 → 父组件带 name 重拉员工列表 */
		onSearch(e) {
			this.triggerEvent('search', { value: e.detail?.value || '' })
		},

		/** 点选行:single 替换 draft,multiple toggle 多选并刷新 active 态 */
		onToggle(e) {
			const index = e.currentTarget.dataset.index
			const row = this.data.optionRows[index]
			if (!row) return
			const valueStr = String(row.value)
			const isSingle = this.properties.mode === 'single'
			let draftValues
			if (isSingle) {
				draftValues = [valueStr]
			} else {
				draftValues = [...this.data.draftValues]
				const hitIndex = draftValues.indexOf(valueStr)
				if (hitIndex === -1) {
					draftValues.push(valueStr)
				} else {
					draftValues.splice(hitIndex, 1)
				}
			}
			const optionRows = this.data.optionRows.map(item => ({
				...item,
				active: draftValues.includes(String(item.value))
			}))
			this.setData({
				draftValues,
				optionRows
			})
		},

		/** 确认 draft:按 mode 返回标量或数组 triggerEvent('confirm') */
		onConfirm() {
			const values = this.data.draftValues.map(value => {
				const matched = (this.properties.options || []).find(
					item => String(item.value) === String(value)
				)
				return matched ? matched.value : value
			})
			if (this.properties.mode === 'single') {
				this.triggerEvent('confirm', { value: values[0] })
				return
			}
			this.triggerEvent('confirm', { value: values })
		},

		onCancel() {
			this.triggerEvent('cancel')
		}
	}
})