Files
wxapp_escort/components/hospital-select/index.js
T

133 lines
3.8 KiB
JavaScript

// 医院选择:列表可搜索(名称 / 主体名 / 全拼 / 拼音首字母),找不到时可手动输入
const SEARCH_PLACEHOLDER = '搜索医院名称或拼音'
const OPTION_HEIGHT = 112
// 关键词去掉空白、转小写:中文原样匹配,拼音统一小写比较
const normalizeKeyword = (value) => String(value || '').trim().toLowerCase().replace(/\s+/g, '')
function filterHospitals(options, keyword) {
const kw = normalizeKeyword(keyword)
if (!kw) return options || []
return (options || []).filter((item) => [
item.name,
item.entityName,
item.pinyin,
item.pinyinFL,
].some((field) => normalizeKeyword(field).includes(kw)))
}
Component({
properties: {
label: { type: String, value: '就诊医院' },
value: { type: String, value: '' },
manual: {
type: Boolean,
value: false,
observer() {
// 父级切换手动/列表模式时收起面板,避免两种形态叠在一起
if (this.data.open) this.setData({ open: false, keyword: '', optionsHeight: 0 })
},
},
options: {
type: Array,
value: [],
observer(newValue) {
this.refreshFiltered(newValue, this.data.keyword)
},
},
placeholder: { type: String, value: '请选择就诊医院' },
disabled: { type: Boolean, value: false },
compact: { type: Boolean, value: false },
},
data: {
open: false,
keyword: '',
filtered: [],
optionsHeight: 0,
searchPlaceholder: SEARCH_PLACEHOLDER,
},
lifetimes: {
attached() {
this.refreshFiltered(this.data.options, '')
},
},
methods: {
refreshFiltered(options = this.data.options, keyword = this.data.keyword) {
const filtered = filterHospitals(options, keyword)
const visibleRows = Math.min(filtered.length, 4)
this.setData({
filtered,
optionsHeight: visibleRows * OPTION_HEIGHT,
})
},
onOpen() {
if (this.data.disabled || this.data.manual) return
if (this.data.open) {
this.onClose()
return
}
this.setData({ open: true, keyword: '' })
this.refreshFiltered(this.data.options, '')
},
onClose() {
this.setData({ open: false, keyword: '', optionsHeight: 0 })
},
onKeywordInput(e) {
const keyword = e.detail.value
this.setData({ keyword })
this.refreshFiltered(this.data.options, keyword)
},
onSelect(e) {
if (this.data.disabled) return
const index = Number(e.currentTarget.dataset.index)
const hospital = this.data.filtered[index]
if (!hospital) return
this.onClose()
this.triggerEvent('change', {
name: hospital.name,
manual: false,
hospital,
})
},
// 搜索词没有匹配时,直接把当前输入当作医院名,转入手动输入
onUseKeyword() {
const name = String(this.data.keyword || '').trim()
if (!name) return
this.onClose()
this.triggerEvent('change', { name, manual: true, hospital: null })
},
onManualInput(e) {
this.triggerEvent('change', { name: e.detail.value, manual: true, hospital: null })
},
onToggleManual() {
if (this.data.disabled) return
if (!this.data.manual) {
this.onClose()
this.triggerEvent('change', { name: this.data.value || '', manual: true, hospital: null })
return
}
// 从手动切回列表:当前值能在列表里找到就还原选中,否则清空
const value = String(this.data.value || '').trim()
const matched = (this.data.options || []).find((item) => item.name === value)
this.onClose()
this.triggerEvent('change', matched
? { name: matched.name, manual: false, hospital: matched }
: { name: '', manual: false, hospital: null })
},
},
})