feat(hospital): 医院选择改为底部弹层并适配键盘

面板通过 root-portal 挂到页面根部,脱离父级 scroll-view 与 transform 层叠上下文,
两个预约入口共用同一选择面板;展开高度按窗口与键盘高度计算,搜索与手动填写
关闭 adjust-position,删除原先按行高推算列表高度和自动上滚页面的逻辑。
This commit is contained in:
lik
2026-11-16 19:30:28 +08:00
parent 0b4c752885
commit cee932c4c5
3 changed files with 98 additions and 382 deletions
+19 -46
View File
@@ -1,7 +1,5 @@
// 医院选择:列表可搜索(名称 / 主体名 / 全拼 / 拼音首字母);列表里没有的走「其他医院」手填
const SEARCH_PLACEHOLDER = '搜索医院名称或拼音'
// 单行选项高度:字号放大后重新核算(名称 32rpx + 地址 28rpx + 上下留白)
const OPTION_HEIGHT = 132
// 关键词去掉空白、转小写:中文原样匹配,拼音统一小写比较
const normalizeKeyword = (value) => String(value || '').trim().toLowerCase().replace(/\s+/g, '')
@@ -33,19 +31,14 @@ Component({
placeholder: { type: String, value: '请选择就诊医院' },
disabled: { type: Boolean, value: false },
compact: { type: Boolean, value: false },
// 默认由组件自己上滚页面;放在弹窗内的 scroll-view 时,
// 页面滚动无法带动弹窗内容,需置 false 交给页面做容器内滚动
autoPageScroll: { type: Boolean, value: true },
// 展开面板上滚时,医院行锚定到距页面顶部的距离(rpx);
// 传入上方行的总高度,可让「就诊人姓名」等上一行滚到页面顶部
scrollAnchorOffset: { type: Number, value: 0 },
},
data: {
open: false,
keyword: '',
filtered: [],
optionsHeight: 0,
panelHeight: 0,
keyboardHeight: 0,
manualEntry: false,
manualName: '',
searchPlaceholder: SEARCH_PLACEHOLDER,
@@ -60,11 +53,7 @@ Component({
methods: {
refreshFiltered(options = this.data.options, keyword = this.data.keyword) {
const filtered = filterHospitals(options, keyword)
const visibleRows = Math.min(filtered.length, 6)
this.setData({
filtered,
optionsHeight: visibleRows * OPTION_HEIGHT,
})
this.setData({ filtered })
},
onOpen() {
@@ -73,43 +62,24 @@ Component({
this.onClose()
return
}
this.setData({ open: true, keyword: '', manualEntry: false })
this.windowHeight = wx.getWindowInfo ? wx.getWindowInfo().windowHeight : wx.getSystemInfoSync().windowHeight
this.setData({ open: true, keyword: '', manualEntry: false, keyboardHeight: 0,
panelHeight: Math.floor(this.windowHeight * 0.82) })
this.refreshFiltered(this.data.options, '')
// 页面可监听 open 事件,在自定义滚动容器内做定位
this.triggerEvent('open')
if (!this.data.autoPageScroll) return
// 面板展开后若被页面底部遮挡,自动上滚页面,保证下拉列表完整可见
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 })
this.setData({ open: false, keyword: '', keyboardHeight: 0, manualEntry: false })
wx.hideKeyboard()
this.triggerEvent('close')
},
onKeyboardHeightChange(e) {
if (!this.data.open) return
const keyboardHeight = Math.max(0, Number(e.detail.height) || 0)
const available = Math.max(0, this.windowHeight - keyboardHeight)
this.setData({ keyboardHeight, panelHeight: Math.floor(Math.min(this.windowHeight * 0.82, available)) })
},
// 遮罩占位:catchtouchmove 阻止穿透滚动页面
@@ -148,9 +118,12 @@ Component({
onCancelManual() {
this.setData({ manualEntry: false, manualName: '' })
this.onKeyboardHeightChange({ detail: { height: 0 } })
wx.hideKeyboard()
},
onConfirmManual() {
if (this.data.disabled) return
const name = String(this.data.manualName || '').trim()
if (!name) return