Files
wxapp_escort/components/hospital-select/index.js
T
lik f45f2f4be3 style: 陪诊端三页视觉升级(字号放大后整页协调)
- 档案编辑页:表单值降为 500 字重、辅助文字提升一档、保存按钮增加「保存中…」防重复提交
- 预约记录页:卡片去描边、时间行加粗为第一眼信息、底行去 chevron、骨架屏替代转圈、空态增加预约入口按钮
- 预约详情页:陪诊师卡改为首字牌+一键拨打(makePhoneCall)、白卡圆角统一 28rpx、展开区增加入场动画
- 医院选择组件:选项文案与对齐微调
2026-11-05 19:58:04 +08:00

153 lines
4.8 KiB
JavaScript

// 医院选择:列表可搜索(名称 / 主体名 / 全拼 / 拼音首字母);列表里没有的走「其他医院」手填
const SEARCH_PLACEHOLDER = '搜索医院名称或拼音'
// 单行选项高度:字号放大后重新核算(名称 32rpx + 地址 28rpx + 上下留白)
const OPTION_HEIGHT = 132
// 关键词去掉空白、转小写:中文原样匹配,拼音统一小写比较
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,
...(Array.isArray(item.nicknames) ? item.nicknames : []),
].some((field) => normalizeKeyword(field).includes(kw)))
}
Component({
properties: {
label: { type: String, value: '就诊医院' },
value: { type: String, value: '' },
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 },
// 展开面板上滚时,医院行锚定到距页面顶部的距离(rpx);
// 传入上方行的总高度,可让「就诊人姓名」等上一行滚到页面顶部
scrollAnchorOffset: { type: Number, value: 0 },
},
data: {
open: false,
keyword: '',
filtered: [],
optionsHeight: 0,
manualEntry: false,
manualName: '',
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) return
if (this.data.open) {
this.onClose()
return
}
this.setData({ open: true, keyword: '', manualEntry: false })
this.refreshFiltered(this.data.options, '')
// 面板展开后若被页面底部遮挡,自动上滚页面,保证下拉列表完整可见
wx.nextTick(() => this.scrollPanelIntoView())
},
scrollPanelIntoView() {
const query = this.createSelectorQuery()
query.select('.hospital-row').boundingClientRect()
query.select('.hospital-panel').boundingClientRect()
query.selectViewport().scrollOffset()
query.exec((res) => {
const rowRect = res && res[0]
const panelRect = res && res[1]
const scroll = res && res[2]
if (!rowRect || !scroll) return
const rpx = wx.getSystemInfoSync().windowWidth / 750
const margin = 20
// 目标1:医院行上滚到距顶部 scrollAnchorOffset 处(默认页面顶部)
let delta = rowRect.top - this.data.scrollAnchorOffset * rpx
// 目标2:面板完整可见,两者取滚动量更大的一方
if (panelRect) {
delta = Math.max(delta, panelRect.bottom + margin - wx.getSystemInfoSync().windowHeight)
}
if (Math.abs(delta) < 1) return
wx.pageScrollTo({
scrollTop: scroll.scrollTop + delta,
duration: 300,
})
})
},
onClose() {
this.setData({ open: false, keyword: '', optionsHeight: 0, manualEntry: false })
},
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,
})
},
// 「其他医院」:展开手填输入框;有搜索词时带入搜索词,否则带入当前已填名称
onStartManual() {
const keyword = String(this.data.keyword || '').trim()
const current = String(this.data.value || '').trim()
this.setData({ manualEntry: true, manualName: keyword || current })
},
onManualNameInput(e) {
this.setData({ manualName: e.detail.value })
},
onCancelManual() {
this.setData({ manualEntry: false, manualName: '' })
},
onConfirmManual() {
const name = String(this.data.manualName || '').trim()
if (!name) return
this.onClose()
this.triggerEvent('change', { name, manual: true, hospital: null })
},
},
})