feat(escort): 医院选择改为可搜索自定义组件

This commit is contained in:
lik
2026-11-05 13:54:31 +08:00
parent 6536abb240
commit e3c4c14720
13 changed files with 601 additions and 41 deletions
+132
View File
@@ -0,0 +1,132 @@
// 医院选择:列表可搜索(名称 / 主体名 / 全拼 / 拼音首字母),找不到时可手动输入
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 })
},
},
})
+4
View File
@@ -0,0 +1,4 @@
{
"component": true,
"usingComponents": {}
}
+68
View File
@@ -0,0 +1,68 @@
<view class="hospital-select {{compact ? 'is-compact' : ''}} {{disabled ? 'is-disabled' : ''}}">
<view class="hospital-row">
<view class="hospital-label-wrap">
<text class="hospital-label">{{label}}</text>
<text class="hospital-mode" catchtap="onToggleManual">{{manual ? '从列表选择' : '手动输入'}}</text>
</view>
<input
wx:if="{{manual}}"
class="hospital-input"
value="{{value}}"
placeholder="{{placeholder}}"
placeholder-class="hospital-placeholder"
disabled="{{disabled}}"
bindinput="onManualInput"
/>
<view wx:else class="hospital-value" bindtap="onOpen">
<text class="hospital-value-text {{value ? '' : 'is-placeholder'}}">{{value || placeholder}}</text>
<view class="hospital-chevron {{open ? 'is-open' : ''}}"></view>
</view>
</view>
<view class="hospital-panel" wx:if="{{open && !manual}}">
<view class="hospital-search">
<input
class="hospital-search-input"
value="{{keyword}}"
placeholder="{{searchPlaceholder}}"
placeholder-class="hospital-placeholder"
focus="{{open}}"
confirm-type="search"
bindinput="onKeywordInput"
/>
<text class="hospital-search-close" bindtap="onClose">收起</text>
</view>
<scroll-view
wx:if="{{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>
<text class="hospital-use-input" wx:if="{{keyword}}" bindtap="onUseKeyword">使用「{{keyword}}」</text>
<text class="hospital-use-input" wx:else bindtap="onToggleManual">手动输入</text>
</view>
<view class="hospital-panel-footer" wx:if="{{keyword && filtered.length > 0}}">
<text class="hospital-use-input" bindtap="onUseKeyword">没有找到?使用「{{keyword}}」</text>
</view>
</view>
</view>
+218
View File
@@ -0,0 +1,218 @@
/* 医院选择:行结构与页面表单保持一致,展开面板作为行内下拉 */
.hospital-row {
display: flex;
align-items: center;
justify-content: space-between;
padding: 24rpx 0;
border-bottom: 2rpx solid var(--line, #ECE3D6);
}
.is-compact .hospital-row {
min-height: 72rpx;
padding: 12rpx 0;
}
.is-disabled {
opacity: 0.6;
}
.hospital-label-wrap {
display: flex;
flex-direction: column;
align-items: flex-start;
flex-shrink: 0;
}
.hospital-label {
color: var(--muted, #6F655C);
font-size: var(--fs-28, 28rpx);
letter-spacing: 1rpx;
}
.is-compact .hospital-label {
font-size: var(--fs-26, 26rpx);
}
.hospital-mode {
margin-top: 6rpx;
color: var(--brand-ink, #9B4508);
font-size: var(--fs-24, 24rpx);
line-height: 1.4;
}
.hospital-mode:active {
opacity: 0.6;
}
.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-28, 28rpx);
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-input {
flex: 1;
min-width: 0;
margin-left: 24rpx;
color: var(--ink, #2B2521);
font-size: var(--fs-28, 28rpx);
text-align: right;
}
.hospital-placeholder {
color: var(--muted, #6F655C);
font-weight: 400;
}
.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);
}
/* 展开面板:暖白纸面 + 浅橙描边,与全站二级信息块一致 */
.hospital-panel {
margin: 12rpx 0 8rpx;
padding: 16rpx;
border: 2rpx solid var(--brand-line, #FFE0C2);
border-radius: 20rpx;
background: var(--paper, #FAF8F5);
}
.hospital-search {
display: flex;
align-items: center;
padding-bottom: 12rpx;
border-bottom: 2rpx solid var(--line, #ECE3D6);
}
.hospital-search-input {
flex: 1;
min-width: 0;
color: var(--ink, #2B2521);
font-size: var(--fs-26, 26rpx);
}
.hospital-search-close {
flex-shrink: 0;
margin-left: 16rpx;
color: var(--muted, #6F655C);
font-size: var(--fs-24, 24rpx);
}
.hospital-search-close:active {
opacity: 0.6;
}
.hospital-options {
margin-top: 4rpx;
}
.hospital-option {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16rpx 4rpx;
border-bottom: 2rpx solid var(--line, #ECE3D6);
}
.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-28, 28rpx);
font-weight: 700;
line-height: 1.4;
word-break: break-all;
}
.is-compact .hospital-option-name {
font-size: var(--fs-26, 26rpx);
}
.hospital-option-sub {
display: block;
margin-top: 6rpx;
color: var(--muted, #6F655C);
font-size: var(--fs-24, 24rpx);
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-24, 24rpx);
}
.hospital-empty {
padding: 32rpx 0;
text-align: center;
}
.hospital-empty-text {
display: block;
color: var(--muted, #6F655C);
font-size: var(--fs-26, 26rpx);
}
.hospital-use-input {
display: inline-block;
margin-top: 12rpx;
color: var(--brand-ink, #9B4508);
font-size: var(--fs-26, 26rpx);
}
.hospital-use-input:active {
opacity: 0.6;
}
.hospital-panel-footer {
padding-top: 12rpx;
text-align: center;
}