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 = '搜索医院名称或拼音' const SEARCH_PLACEHOLDER = '搜索医院名称或拼音'
// 单行选项高度:字号放大后重新核算(名称 32rpx + 地址 28rpx + 上下留白)
const OPTION_HEIGHT = 132
// 关键词去掉空白、转小写:中文原样匹配,拼音统一小写比较 // 关键词去掉空白、转小写:中文原样匹配,拼音统一小写比较
const normalizeKeyword = (value) => String(value || '').trim().toLowerCase().replace(/\s+/g, '') const normalizeKeyword = (value) => String(value || '').trim().toLowerCase().replace(/\s+/g, '')
@@ -33,19 +31,14 @@ Component({
placeholder: { type: String, value: '请选择就诊医院' }, placeholder: { type: String, value: '请选择就诊医院' },
disabled: { type: Boolean, value: false }, disabled: { type: Boolean, value: false },
compact: { type: Boolean, value: false }, compact: { type: Boolean, value: false },
// 默认由组件自己上滚页面;放在弹窗内的 scroll-view 时,
// 页面滚动无法带动弹窗内容,需置 false 交给页面做容器内滚动
autoPageScroll: { type: Boolean, value: true },
// 展开面板上滚时,医院行锚定到距页面顶部的距离(rpx);
// 传入上方行的总高度,可让「就诊人姓名」等上一行滚到页面顶部
scrollAnchorOffset: { type: Number, value: 0 },
}, },
data: { data: {
open: false, open: false,
keyword: '', keyword: '',
filtered: [], filtered: [],
optionsHeight: 0, panelHeight: 0,
keyboardHeight: 0,
manualEntry: false, manualEntry: false,
manualName: '', manualName: '',
searchPlaceholder: SEARCH_PLACEHOLDER, searchPlaceholder: SEARCH_PLACEHOLDER,
@@ -60,11 +53,7 @@ Component({
methods: { methods: {
refreshFiltered(options = this.data.options, keyword = this.data.keyword) { refreshFiltered(options = this.data.options, keyword = this.data.keyword) {
const filtered = filterHospitals(options, keyword) const filtered = filterHospitals(options, keyword)
const visibleRows = Math.min(filtered.length, 6) this.setData({ filtered })
this.setData({
filtered,
optionsHeight: visibleRows * OPTION_HEIGHT,
})
}, },
onOpen() { onOpen() {
@@ -73,43 +62,24 @@ Component({
this.onClose() this.onClose()
return 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, '') this.refreshFiltered(this.data.options, '')
// 页面可监听 open 事件,在自定义滚动容器内做定位
this.triggerEvent('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() { 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 阻止穿透滚动页面 // 遮罩占位:catchtouchmove 阻止穿透滚动页面
@@ -148,9 +118,12 @@ Component({
onCancelManual() { onCancelManual() {
this.setData({ manualEntry: false, manualName: '' }) this.setData({ manualEntry: false, manualName: '' })
this.onKeyboardHeightChange({ detail: { height: 0 } })
wx.hideKeyboard()
}, },
onConfirmManual() { onConfirmManual() {
if (this.data.disabled) return
const name = String(this.data.manualName || '').trim() const name = String(this.data.manualName || '').trim()
if (!name) return if (!name) return
+41 -68
View File
@@ -1,73 +1,46 @@
<view class="hospital-select {{compact ? 'is-compact' : ''}} {{disabled ? 'is-disabled' : ''}} {{open ? 'is-open' : ''}}"> <view class="hospital-select {{compact ? 'is-compact' : ''}} {{disabled ? 'is-disabled' : ''}}">
<!-- 展开时的全屏遮罩:点击任意空白处收起,强化「可开可收」的下拉感知 --> <view class="hospital-row" bindtap="onOpen" aria-role="button" aria-label="{{label}}:{{value || placeholder}}" aria-expanded="{{open}}">
<view class="hospital-mask" wx:if="{{open}}" bindtap="onClose" catchtouchmove="noop"></view>
<view class="hospital-row">
<text class="hospital-label">{{label}}</text> <text class="hospital-label">{{label}}</text>
<view class="hospital-value" bindtap="onOpen"> <view class="hospital-value">
<text class="hospital-value-text {{value ? '' : 'is-placeholder'}}">{{value || placeholder}}</text> <text class="hospital-value-text {{value ? '' : 'is-placeholder'}}">{{value || placeholder}}</text>
<view class="hospital-chevron {{open ? 'is-open' : ''}}"></view> <view class="hospital-chevron"></view>
</view>
</view>
<view class="hospital-panel" wx:if="{{open}}">
<view class="hospital-search">
<input
class="hospital-search-input"
value="{{keyword}}"
placeholder="{{searchPlaceholder}}"
placeholder-class="hospital-placeholder"
confirm-type="search"
bindinput="onKeywordInput"
/>
<view class="hospital-search-close" bindtap="onClose" aria-role="button" aria-label="收起医院列表">
<view class="hospital-collapse-arrow"></view>
</view>
</view>
<!-- 填写其他医院:列表里没有的医院走这里 -->
<view wx:if="{{manualEntry}}" class="hospital-manual-panel">
<input
class="hospital-manual-input"
value="{{manualName}}"
placeholder="请输入医院名称"
placeholder-class="hospital-placeholder"
focus="{{manualEntry}}"
confirm-type="done"
bindinput="onManualNameInput"
bindconfirm="onConfirmManual"
/>
<text class="hospital-manual-confirm" catchtap="onConfirmManual">确定</text>
<text class="hospital-manual-cancel" catchtap="onCancelManual">返回列表</text>
</view>
<scroll-view
wx:elif="{{filtered.length > 0}}"
class="hospital-options"
scroll-y
style="height: {{optionsHeight}}rpx;"
>
<view
wx:for="{{filtered}}"
wx:key="_id"
class="hospital-option"
data-index="{{index}}"
bindtap="onSelect"
>
<view class="hospital-option-main">
<text class="hospital-option-name">{{item.name}}</text>
<text class="hospital-option-sub" wx:if="{{item.address}}">{{item.address}}</text>
</view>
<text class="hospital-option-selected" wx:if="{{item.name === value}}">已选</text>
</view>
</scroll-view>
<view wx:else class="hospital-empty">
<text class="hospital-empty-text">{{keyword ? '没有找到匹配的医院' : '暂无可选医院'}}</text>
</view>
<!-- 其他医院:常驻入口,不用先搜出空结果才找得到 -->
<view wx:if="{{!manualEntry}}" class="hospital-other" bindtap="onStartManual">
<text class="hospital-other-text">其他医院(列表里没有?手动填写名称)</text>
</view> </view>
</view> </view>
</view> </view>
<!-- 脱离父级 scroll-view 和 transform 层叠上下文,两个预约入口共享同一选择面板。 -->
<root-portal wx:if="{{open}}">
<view class="hospital-overlay">
<view class="hospital-mask" bindtap="onClose" catchtouchmove="noop"></view>
<view class="hospital-panel {{keyboardHeight ? 'keyboard-open' : ''}}" style="height: {{panelHeight}}px; bottom: {{keyboardHeight}}px;" catchtap="noop">
<view class="hospital-heading" catchtouchmove="noop">
<text class="hospital-title">{{manualEntry ? '手动填写医院' : '选择医院'}}</text>
<view class="hospital-close" bindtap="onClose" aria-role="button" aria-label="关闭医院选择"><view class="close-mark"></view></view>
</view>
<block wx:if="{{manualEntry}}">
<view class="hospital-manual-panel">
<input class="hospital-manual-input" value="{{manualName}}" placeholder="请输入医院名称" placeholder-class="hospital-placeholder" focus="{{manualEntry}}" adjust-position="{{false}}" confirm-type="done" bindinput="onManualNameInput" bindconfirm="onConfirmManual" bindkeyboardheightchange="onKeyboardHeightChange" />
<view class="manual-actions">
<button class="manual-back" size="default" bindtap="onCancelManual">返回列表</button>
<button class="manual-confirm" size="default" disabled="{{!manualName}}" bindtap="onConfirmManual">确定</button>
</view>
</view>
</block>
<block wx:else>
<view class="hospital-search" catchtouchmove="noop">
<input class="hospital-search-input" value="{{keyword}}" placeholder="{{searchPlaceholder}}" placeholder-class="hospital-placeholder" adjust-position="{{false}}" confirm-type="search" bindinput="onKeywordInput" bindkeyboardheightchange="onKeyboardHeightChange" />
</view>
<scroll-view class="hospital-options" scroll-y enhanced show-scrollbar="{{true}}">
<view class="hospital-option {{item.name === value ? 'is-selected' : ''}}" wx:for="{{filtered}}" wx:key="_id" data-index="{{index}}" bindtap="onSelect" aria-role="button" aria-label="{{item.name}} {{item.address || ''}}">
<view class="hospital-option-main">
<text class="hospital-option-name">{{item.name}}</text>
<text class="hospital-option-sub" wx:if="{{item.address}}">{{item.address}}</text>
</view>
<view class="hospital-option-selected" wx:if="{{item.name === value}}" aria-label="已选"></view>
</view>
<view class="hospital-empty" wx:if="{{!filtered.length}}">{{keyword ? '没有找到匹配的医院' : '暂无可选医院'}}</view>
</scroll-view>
<view class="hospital-other" bindtap="onStartManual" catchtouchmove="noop" aria-role="button">手动填写医院</view>
</block>
</view>
</view>
</root-portal>
+38 -268
View File
@@ -1,268 +1,38 @@
/* 医院选择:行结构与页面表单保持一致,展开面板作为行内下拉 */ /* 普通表单行;浮层由 root-portal 挂到页面根部,不撑开原表单。 */
/* 展开时整组件抬到遮罩之上:遮罩盖住页面其余内容(含底部栏 100),面板仍可点 */ .hospital-row { display: flex; align-items: center; justify-content: space-between; box-sizing: border-box; min-height: 100rpx; padding: 24rpx 0; border-bottom: 2rpx solid var(--line, #ECE3D6); }
.hospital-select.is-open { .is-compact .hospital-row { min-height: 88rpx; padding: 20rpx 0; }
position: relative; .is-disabled { opacity: 0.6; }
z-index: 151; .hospital-label { flex-shrink: 0; color: var(--muted, #6F655C); font-size: var(--fs-30, 30rpx); letter-spacing: 1rpx; line-height: 1.6; }
} .is-compact .hospital-label { font-size: var(--fs-28, 28rpx); }
.hospital-value { display: flex; flex: 1; min-width: 0; align-items: center; justify-content: flex-end; gap: 12rpx; margin-left: 24rpx; }
/* 展开遮罩:浅墨半透明,点击任意空白处收起;层级低于页面支付弹窗(200/201) */ .hospital-value-text { min-width: 0; color: var(--ink, #2B2521); font-size: var(--fs-30, 30rpx); font-weight: 500; text-align: right; line-height: 1.6; word-break: break-all; }
.hospital-mask { .hospital-value-text.is-placeholder, .hospital-placeholder { color: var(--muted, #6F655C); font-weight: 400; }
position: fixed; .hospital-chevron { flex-shrink: 0; width: 16rpx; height: 16rpx; border-right: 2rpx solid var(--muted, #6F655C); border-bottom: 2rpx solid var(--muted, #6F655C); transform: rotate(45deg); }
top: 0; .hospital-overlay { position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 1000; color: var(--ink, #2B2521); }
right: 0; .hospital-mask { position: absolute; top: 0; right: 0; bottom: 0; left: 0; background: rgba(43, 37, 33, 0.3); }
bottom: 0; .hospital-panel { position: absolute; left: 0; right: 0; display: flex; flex-direction: column; box-sizing: border-box; padding-bottom: env(safe-area-inset-bottom); border-radius: 32rpx 32rpx 0 0; background: var(--white, #FFFFFF); overflow: hidden; }
left: 0; .hospital-panel.keyboard-open { padding-bottom: 0; }
z-index: 1; .hospital-heading { display: flex; align-items: center; justify-content: space-between; flex-shrink: 0; min-height: 104rpx; padding: 0 20rpx 0 32rpx; }
background: rgba(43, 37, 33, 0.3); .hospital-title { font-size: var(--fs-32, 32rpx); font-weight: 600; line-height: 1.5; }
} .hospital-close { display: flex; align-items: center; justify-content: center; width: 88rpx; height: 88rpx; }
.close-mark { position: relative; width: 28rpx; height: 28rpx; }
/* 展开时行与面板抬到遮罩之上(同属 .is-open 层叠上下文,仅内部比较) */ .close-mark::before, .close-mark::after { content: ''; position: absolute; top: 13rpx; left: 0; width: 28rpx; height: 2rpx; background: var(--muted, #6F655C); transform: rotate(45deg); }
.hospital-select.is-open .hospital-row, .close-mark::after { transform: rotate(-45deg); }
.hospital-select.is-open .hospital-panel { .hospital-search { flex-shrink: 0; padding: 0 32rpx 16rpx; }
position: relative; .hospital-search-input, .hospital-manual-input { box-sizing: border-box; width: 100%; height: 88rpx; padding: 16rpx 20rpx; color: var(--ink, #2B2521); font-size: var(--fs-30, 30rpx); border: 2rpx solid var(--line, #ECE3D6); border-radius: 16rpx; background: var(--paper, #FAF8F5); }
z-index: 2; .hospital-options { flex: 1; height: 0; min-height: 0; box-sizing: border-box; }
} .hospital-option { display: flex; align-items: center; gap: 16rpx; margin: 0 32rpx; padding: 24rpx 0; border-bottom: 2rpx solid var(--line, #ECE3D6); }
.hospital-option:last-child { border-bottom: none; }
.hospital-row { .hospital-option:active, .hospital-option.is-selected { background: var(--brand-soft, #FFF4E8); }
display: flex; .hospital-option-main { flex: 1; min-width: 0; }
align-items: center; .hospital-option-name { display: block; color: var(--ink, #2B2521); font-size: var(--fs-30, 30rpx); font-weight: 500; line-height: 1.5; word-break: break-all; }
justify-content: space-between; .hospital-option-sub { display: block; margin-top: 8rpx; color: var(--muted, #6F655C); font-size: var(--fs-26, 26rpx); line-height: 1.6; word-break: break-all; }
padding: 24rpx 0; .hospital-option-selected { flex-shrink: 0; width: 20rpx; height: 12rpx; margin: -6rpx 8rpx 0; border-left: 3rpx solid var(--brand-ink, #9B4508); border-bottom: 3rpx solid var(--brand-ink, #9B4508); transform: rotate(-45deg); }
border-bottom: 2rpx solid var(--line); .hospital-empty { padding: 40rpx 32rpx; text-align: center; color: var(--muted, #6F655C); font-size: var(--fs-28, 28rpx); line-height: 1.6; }
} .hospital-other { display: flex; align-items: center; justify-content: center; flex-shrink: 0; min-height: 96rpx; border-top: 2rpx solid var(--line, #ECE3D6); color: var(--brand-ink, #9B4508); font-size: var(--fs-30, 30rpx); }
.hospital-manual-panel { flex: 1; min-height: 0; padding: 0 32rpx 24rpx; overflow-y: auto; }
.is-compact .hospital-row { .manual-actions { display: flex; gap: 16rpx; margin-top: 24rpx; }
min-height: 72rpx; .manual-actions button[size="default"] { display: flex; align-items: center; justify-content: center; flex: 1; width: auto; min-height: 88rpx; margin: 0; padding: 16rpx; border-radius: 16rpx; font-size: var(--fs-30, 30rpx); line-height: 1.5; }
padding: 12rpx 0; .manual-back { background: var(--paper, #FAF8F5); color: var(--ink, #2B2521); }
} .manual-confirm { background: var(--brand-strong, #B8520A); color: var(--white, #FFFFFF); }
.manual-actions button::after { border: none; }
.is-disabled {
opacity: 0.6;
}
/* 行标签与取值统一 30rpx,与页面表单(.order-label / .order-value)同档。
标签走裸字 muted:表单里浅橙底只留给可选项,标签不冒充控件
(与 itemorder / itemdetail 的 .order-label、health/profileedit 的标签同一口径) */
.hospital-label {
flex-shrink: 0;
color: var(--muted, #6F655C);
font-size: var(--fs-30, 30rpx);
letter-spacing: 1rpx;
}
/* compact 档用于「预约记录 · 编辑」表单,跟该页 .form-label(28) / .form-input(30) 对齐 */
.is-compact .hospital-label {
font-size: var(--fs-28, 28rpx);
}
.hospital-value {
display: flex;
flex: 1;
min-width: 0;
align-items: center;
justify-content: flex-end;
margin-left: 24rpx;
}
.hospital-value-text {
min-width: 0;
overflow: hidden;
color: var(--ink, #2B2521);
font-size: var(--fs-30, 30rpx);
font-weight: 700;
text-align: right;
text-overflow: ellipsis;
white-space: nowrap;
}
.is-compact .hospital-value-text {
font-weight: 400;
}
.hospital-value-text.is-placeholder {
color: var(--muted, #6F655C);
font-weight: 400;
}
.hospital-placeholder {
color: var(--muted, #6F655C);
font-weight: 400;
}
/* 「其他医院」:面板底部常驻入口,展开手填输入框 */
.hospital-other {
margin-top: 16rpx;
padding: 20rpx 0;
border-top: 2rpx solid var(--sep, #E5DACC);
text-align: center;
}
.hospital-other-text {
color: var(--brand-ink, #9B4508);
font-size: var(--fs-30, 30rpx);
}
.hospital-other:active {
opacity: 0.6;
}
.hospital-manual-panel {
display: flex;
align-items: center;
padding: 20rpx 0 8rpx;
}
.hospital-manual-input {
flex: 1;
min-width: 0;
box-sizing: border-box;
padding: 16rpx 20rpx;
color: var(--ink, #2B2521);
font-size: var(--fs-30, 30rpx);
/* 字段填充走暖白纸面 + 发丝边(与 .order-textarea 同款);原 #E6F4EA 是旧绿主题残留 */
background: var(--paper);
border: 2rpx solid var(--line);
border-radius: 12rpx;
}
.hospital-manual-confirm {
flex-shrink: 0;
margin-left: 16rpx;
color: var(--brand-ink, #9B4508);
font-size: var(--fs-30, 30rpx);
}
.hospital-manual-cancel {
flex-shrink: 0;
margin-left: 16rpx;
color: var(--muted, #6F655C);
font-size: var(--fs-28, 28rpx);
}
.hospital-chevron {
flex-shrink: 0;
width: 16rpx;
height: 16rpx;
margin-left: 16rpx;
border-right: 2rpx solid var(--muted, #6F655C);
border-bottom: 2rpx solid var(--muted, #6F655C);
transform: rotate(45deg);
transition: transform 0.2s ease;
}
.hospital-chevron.is-open {
transform: rotate(-135deg);
}
/* 展开面板:白底 + 2rpx 浅橙描边托形,与白卡靠橙边区分(不用阴影) */
.hospital-panel {
margin: 12rpx 0 24rpx;
padding: 0 20rpx 20rpx;
border: 2rpx solid var(--brand-line, #FFE0C2);
border-radius: 20rpx;
background: var(--white, #FFFFFF);
}
.hospital-search {
display: flex;
align-items: center;
padding: 8rpx 0 12rpx;
border-bottom: 2rpx solid var(--sep, #E5DACC);
}
.hospital-search-input {
flex: 1;
min-width: 0;
box-sizing: border-box;
padding: 16rpx 20rpx;
color: var(--ink, #2B2521);
font-size: var(--fs-30, 30rpx);
/* 字段填充走暖白纸面 + 发丝边(与 .order-textarea 同款);原 #E6F4EA 是旧绿主题残留 */
background: var(--paper);
border: 2rpx solid var(--line);
border-radius: 12rpx;
}
/* 收起按钮:上箭头图标(CSS 边框三角,不依赖字体图标),留足点击热区 */
.hospital-search-close {
display: flex;
flex-shrink: 0;
align-items: center;
justify-content: center;
margin-left: 16rpx;
padding: 12rpx;
}
.hospital-collapse-arrow {
width: 20rpx;
height: 20rpx;
border-top: 4rpx solid var(--brand-ink, #9B4508);
border-right: 4rpx solid var(--brand-ink, #9B4508);
transform: rotate(-45deg);
}
.hospital-search-close:active {
opacity: 0.6;
}
.hospital-options {
margin-top: 0;
}
.hospital-option {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16rpx 0;
border-bottom: 2rpx solid var(--sep, #E5DACC);
}
.hospital-option:last-child {
border-bottom: none;
}
.hospital-option:active {
background: var(--brand-soft, #FFF4E8);
}
.hospital-option-main {
flex: 1;
min-width: 0;
}
.hospital-option-name {
display: block;
color: var(--ink, #2B2521);
font-size: var(--fs-30, 30rpx);
font-weight: 700;
line-height: 1.4;
word-break: break-all;
}
.hospital-option-sub {
display: block;
margin-top: 6rpx;
color: var(--muted, #6F655C);
font-size: var(--fs-26, 26rpx);
line-height: 1.4;
word-break: break-all;
}
.hospital-option-selected {
flex-shrink: 0;
margin-left: 16rpx;
color: var(--brand-ink, #9B4508);
font-size: var(--fs-26, 26rpx);
}
.hospital-empty {
padding: 32rpx 0;
text-align: center;
}
.hospital-empty-text {
display: block;
color: var(--muted, #6F655C);
font-size: var(--fs-30, 30rpx);
}