优化:统一小程序业务引用选择交互
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
Component({
|
||||
properties: {
|
||||
visible: { type: Boolean, value: false },
|
||||
title: { type: String, value: '请选择' },
|
||||
keyword: { type: String, value: '' },
|
||||
placeholder: { type: String, value: '搜索' },
|
||||
items: { type: Array, value: [] },
|
||||
selectedId: { type: null, value: '' },
|
||||
loading: { type: Boolean, value: false },
|
||||
errorText: { type: String, value: '' },
|
||||
emptyText: { type: String, value: '暂无可选内容' },
|
||||
actionText: { type: String, value: '' },
|
||||
showSearch: { type: Boolean, value: true }
|
||||
},
|
||||
|
||||
methods: {
|
||||
close() {
|
||||
this.triggerEvent('close')
|
||||
},
|
||||
|
||||
stopEvent() {
|
||||
},
|
||||
|
||||
onSearch(event) {
|
||||
this.triggerEvent('search', { value: event.detail.value })
|
||||
},
|
||||
|
||||
selectItem(event) {
|
||||
const item = this.data.items[Number(event.currentTarget.dataset.index)]
|
||||
if (item) this.triggerEvent('select', { item })
|
||||
},
|
||||
|
||||
retry() {
|
||||
this.triggerEvent('retry')
|
||||
},
|
||||
|
||||
action() {
|
||||
this.triggerEvent('action')
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"component": true
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
const assert = require('assert')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
let definition
|
||||
global.Component = value => { definition = value }
|
||||
require('./index')
|
||||
|
||||
const events = []
|
||||
const component = {
|
||||
data: {
|
||||
items: [{ id: 7, title: '小周' }]
|
||||
},
|
||||
triggerEvent(name, detail) {
|
||||
events.push({ name, detail })
|
||||
}
|
||||
}
|
||||
Object.assign(component, definition.methods)
|
||||
|
||||
component.onSearch({ detail: { value: '周' } })
|
||||
component.selectItem({ currentTarget: { dataset: { index: 0 } } })
|
||||
component.retry()
|
||||
component.action()
|
||||
component.close()
|
||||
|
||||
assert.deepStrictEqual(events.map(item => item.name), ['search', 'select', 'retry', 'action', 'close'])
|
||||
assert.strictEqual(events[0].detail.value, '周')
|
||||
assert.strictEqual(events[1].detail.item.id, 7)
|
||||
|
||||
const wxml = fs.readFileSync(path.join(__dirname, 'index.wxml'), 'utf8')
|
||||
const wxss = fs.readFileSync(path.join(__dirname, 'index.wxss'), 'utf8')
|
||||
assert.match(wxml, /class="selector-mask"[^>]*catchtouchmove="stopEvent"/)
|
||||
assert.match(wxml, /<scroll-view class="selector-scroll" scroll-y/)
|
||||
assert.doesNotMatch(wxml, /selector-scroll[^>]*catchtouchmove/)
|
||||
assert.match(wxss, /\.selector-sheet\s*\{[^}]*height:\s*88vh;[^}]*flex-direction:\s*column;[^}]*overflow:\s*hidden;/s)
|
||||
assert.match(wxss, /\.selector-scroll\s*\{[^}]*min-height:\s*0;[^}]*flex:\s*1;/s)
|
||||
@@ -0,0 +1,44 @@
|
||||
<view wx:if="{{visible}}" class="selector-mask" bindtap="close" catchtouchmove="stopEvent">
|
||||
<view class="selector-sheet" catchtap="stopEvent">
|
||||
<view class="selector-handle"></view>
|
||||
<view class="selector-head">
|
||||
<text class="selector-title">{{title}}</text>
|
||||
<button class="selector-close" bindtap="close" aria-label="关闭"></button>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{showSearch}}" class="selector-search-wrap">
|
||||
<text class="selector-search-icon"></text>
|
||||
<input class="selector-search" value="{{keyword}}" bindinput="onSearch" placeholder="{{placeholder}}" confirm-type="search" />
|
||||
</view>
|
||||
|
||||
<scroll-view class="selector-scroll" scroll-y enhanced show-scrollbar="{{false}}">
|
||||
<view wx:if="{{loading}}" class="selector-state">
|
||||
<view class="selector-loading-dot"></view>
|
||||
<text>正在加载...</text>
|
||||
</view>
|
||||
<view wx:elif="{{errorText}}" class="selector-state selector-error">
|
||||
<text>{{errorText}}</text>
|
||||
<button class="selector-retry" bindtap="retry">重新加载</button>
|
||||
</view>
|
||||
<view wx:elif="{{items.length === 0}}" class="selector-state">{{emptyText}}</view>
|
||||
<view wx:else class="selector-list">
|
||||
<view wx:for="{{items}}" wx:key="id" class="selector-item {{selectedId == item.id ? 'selected' : ''}}" data-index="{{index}}" bindtap="selectItem">
|
||||
<image wx:if="{{item.avatar}}" class="selector-avatar" src="{{item.avatar}}" mode="aspectFill"></image>
|
||||
<view class="selector-copy {{item.avatar ? '' : 'without-avatar'}}">
|
||||
<view class="selector-title-row">
|
||||
<text class="selector-item-title">{{item.title}}</text>
|
||||
<text wx:if="{{item.badge}}" class="selector-badge">{{item.badge}}</text>
|
||||
</view>
|
||||
<text wx:if="{{item.subtitle}}" class="selector-subtitle">{{item.subtitle}}</text>
|
||||
<text wx:if="{{item.meta}}" class="selector-meta">{{item.meta}}</text>
|
||||
</view>
|
||||
<view class="selector-check">{{selectedId == item.id ? '✓' : '›'}}</view>
|
||||
</view>
|
||||
</view>
|
||||
</scroll-view>
|
||||
|
||||
<view wx:if="{{actionText}}" class="selector-footer">
|
||||
<button class="selector-action" bindtap="action">{{actionText}}</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,289 @@
|
||||
.selector-mask,
|
||||
.selector-sheet,
|
||||
.selector-head,
|
||||
.selector-search-wrap,
|
||||
.selector-scroll,
|
||||
.selector-list,
|
||||
.selector-item,
|
||||
.selector-copy,
|
||||
.selector-footer,
|
||||
.selector-close,
|
||||
.selector-retry,
|
||||
.selector-action,
|
||||
.selector-search {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.selector-close,
|
||||
.selector-retry,
|
||||
.selector-action {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.selector-close::after,
|
||||
.selector-retry::after,
|
||||
.selector-action::after {
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.selector-mask {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 120;
|
||||
display: flex;
|
||||
align-items: flex-end;
|
||||
background: rgba(25, 22, 18, 0.5);
|
||||
animation: selectorMaskIn 180ms ease both;
|
||||
}
|
||||
|
||||
.selector-sheet {
|
||||
display: flex;
|
||||
width: 100%;
|
||||
height: 88vh;
|
||||
max-height: 88vh;
|
||||
flex-direction: column;
|
||||
overflow: hidden;
|
||||
border-radius: 34rpx 34rpx 0 0;
|
||||
background: #fffdf8;
|
||||
box-shadow: 0 -20rpx 48rpx rgba(44, 35, 27, 0.18);
|
||||
animation: selectorSheetUp 220ms ease both;
|
||||
}
|
||||
|
||||
.selector-handle {
|
||||
width: 72rpx;
|
||||
height: 7rpx;
|
||||
margin: 14rpx auto 2rpx;
|
||||
border-radius: 999rpx;
|
||||
background: #ddd6cd;
|
||||
}
|
||||
|
||||
.selector-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
min-height: 92rpx;
|
||||
padding: 0 28rpx;
|
||||
}
|
||||
|
||||
.selector-title {
|
||||
color: #29241f;
|
||||
font-size: 34rpx;
|
||||
font-weight: 900;
|
||||
letter-spacing: 1rpx;
|
||||
}
|
||||
|
||||
.selector-close {
|
||||
position: relative;
|
||||
width: 52rpx;
|
||||
min-width: 52rpx;
|
||||
height: 52rpx;
|
||||
min-height: 52rpx;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
border: 1rpx solid #e6dfd6;
|
||||
border-radius: 50%;
|
||||
background: #f8f4ee;
|
||||
}
|
||||
|
||||
.selector-close::before,
|
||||
.selector-close::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 24rpx;
|
||||
left: 14rpx;
|
||||
width: 22rpx;
|
||||
height: 3rpx;
|
||||
border-radius: 999rpx;
|
||||
background: #665e56;
|
||||
}
|
||||
|
||||
.selector-close::before { transform: rotate(45deg); }
|
||||
.selector-close::after { transform: rotate(-45deg); }
|
||||
|
||||
.selector-search-wrap {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
min-height: 80rpx;
|
||||
margin: 0 28rpx 18rpx;
|
||||
padding: 0 22rpx;
|
||||
border: 1rpx solid #e7e0d7;
|
||||
border-radius: 22rpx;
|
||||
background: #f7f3ed;
|
||||
}
|
||||
|
||||
.selector-search-icon {
|
||||
position: relative;
|
||||
width: 26rpx;
|
||||
height: 26rpx;
|
||||
margin-right: 18rpx;
|
||||
border: 3rpx solid #8e877f;
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.selector-search-icon::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
right: -9rpx;
|
||||
bottom: -6rpx;
|
||||
width: 12rpx;
|
||||
height: 3rpx;
|
||||
border-radius: 999rpx;
|
||||
background: #8e877f;
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
|
||||
.selector-search {
|
||||
height: 78rpx;
|
||||
flex: 1;
|
||||
color: #29241f;
|
||||
font-size: 27rpx;
|
||||
}
|
||||
|
||||
.selector-scroll {
|
||||
height: 0;
|
||||
min-height: 0;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.selector-list {
|
||||
padding: 0 28rpx 24rpx;
|
||||
}
|
||||
|
||||
.selector-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
min-height: 112rpx;
|
||||
margin-bottom: 14rpx;
|
||||
padding: 20rpx;
|
||||
border: 1rpx solid #ece5dc;
|
||||
border-radius: 24rpx;
|
||||
background: #fff;
|
||||
transition: transform 140ms ease, border-color 140ms ease, background 140ms ease;
|
||||
}
|
||||
|
||||
.selector-item:active { transform: scale(0.99); }
|
||||
.selector-item.selected { border-color: #8eafa3; background: #f0f6f3; }
|
||||
|
||||
.selector-avatar {
|
||||
width: 76rpx;
|
||||
height: 76rpx;
|
||||
flex: 0 0 auto;
|
||||
border-radius: 50%;
|
||||
background: #eee9e2;
|
||||
}
|
||||
|
||||
.selector-copy {
|
||||
min-width: 0;
|
||||
flex: 1;
|
||||
margin-left: 18rpx;
|
||||
}
|
||||
|
||||
.selector-copy.without-avatar { margin-left: 2rpx; }
|
||||
|
||||
.selector-title-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12rpx;
|
||||
}
|
||||
|
||||
.selector-item-title {
|
||||
overflow: hidden;
|
||||
color: #2b2621;
|
||||
font-size: 28rpx;
|
||||
font-weight: 800;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.selector-badge {
|
||||
flex: 0 0 auto;
|
||||
padding: 5rpx 12rpx;
|
||||
border-radius: 999rpx;
|
||||
background: #e7f0ec;
|
||||
color: #55786d;
|
||||
font-size: 19rpx;
|
||||
font-weight: 800;
|
||||
}
|
||||
|
||||
.selector-subtitle,
|
||||
.selector-meta {
|
||||
display: block;
|
||||
margin-top: 7rpx;
|
||||
overflow: hidden;
|
||||
color: #7f7870;
|
||||
font-size: 23rpx;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.selector-meta { color: #a19990; font-size: 21rpx; }
|
||||
|
||||
.selector-check {
|
||||
width: 52rpx;
|
||||
color: #8c857d;
|
||||
font-size: 32rpx;
|
||||
font-weight: 800;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.selector-item.selected .selector-check { color: #2f6f62; }
|
||||
|
||||
.selector-state {
|
||||
display: flex;
|
||||
min-height: 360rpx;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 40rpx 48rpx;
|
||||
color: #9a9289;
|
||||
font-size: 25rpx;
|
||||
line-height: 1.6;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.selector-error { color: #a8554e; }
|
||||
|
||||
.selector-retry {
|
||||
min-width: 180rpx;
|
||||
height: 64rpx;
|
||||
margin-top: 22rpx;
|
||||
padding: 0 28rpx;
|
||||
border: 1rpx solid #d8d1c8;
|
||||
border-radius: 999rpx;
|
||||
background: #fff;
|
||||
color: #514b44;
|
||||
font-size: 23rpx;
|
||||
font-weight: 800;
|
||||
line-height: 64rpx;
|
||||
}
|
||||
|
||||
.selector-loading-dot {
|
||||
width: 20rpx;
|
||||
height: 20rpx;
|
||||
margin-bottom: 20rpx;
|
||||
border-radius: 50%;
|
||||
background: #6f8f83;
|
||||
box-shadow: -30rpx 0 #c9d8d2, 30rpx 0 #9eb9af;
|
||||
}
|
||||
|
||||
.selector-footer {
|
||||
padding: 16rpx 28rpx calc(18rpx + env(safe-area-inset-bottom));
|
||||
border-top: 1rpx solid #eee7de;
|
||||
background: #fffdf8;
|
||||
}
|
||||
|
||||
.selector-action {
|
||||
width: 100%;
|
||||
height: 78rpx;
|
||||
border: 1rpx solid #cbdad4;
|
||||
border-radius: 22rpx;
|
||||
background: #edf4f1;
|
||||
color: #2f6f62;
|
||||
font-size: 26rpx;
|
||||
font-weight: 900;
|
||||
line-height: 78rpx;
|
||||
}
|
||||
|
||||
@keyframes selectorMaskIn { from { opacity: 0; } to { opacity: 1; } }
|
||||
@keyframes selectorSheetUp { from { transform: translateY(48rpx); } to { transform: translateY(0); } }
|
||||
Reference in New Issue
Block a user