gaoluyang
2026-11-03 2d8b1a28fa5c8b852f0d0078bac4894bfb4abed5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<script setup lang="ts">
  import { computed, ref, watch } from "vue";
  import type { SheetOption } from "../shared";
 
  const props = withDefaults(
    defineProps<{
      show: boolean;
      title: string;
      multiple?: boolean;
      items: SheetOption[];
      selectedKeys?: Array<number | string>;
      searchable?: boolean;
      searchPlaceholder?: string;
      loading?: boolean;
      /** 还有下一页(远程分页时由调用方传入);用于上拉加载更多 */
      hasMore?: boolean;
      emptyText?: string;
      readonly?: boolean;
    }>(),
    {
      multiple: false,
      selectedKeys: () => [],
      searchable: true,
      searchPlaceholder: "搜索名称或编码",
      loading: false,
      hasMore: false,
      emptyText: "暂无可选数据",
      readonly: false,
    },
  );
 
  const emit = defineEmits<{
    "update:show": [value: boolean];
    search: [keyword: string];
    "load-more": [];
    confirm: [keys: Array<number | string>];
  }>();
 
  const keyword = ref("");
  const picked = ref<Array<number | string>>([]);
 
  const pickedSet = computed(() => new Set(picked.value.map(String)));
  const displayedItems = computed(() => {
    const value = keyword.value.trim().toLocaleLowerCase();
    if (!value) return props.items;
    return props.items.filter(item => [item.label, item.sub, item.extra].some(text => String(text || "").toLocaleLowerCase().includes(value)));
  });
 
  function isPicked(key: number | string) {
    return pickedSet.value.has(String(key));
  }
 
  watch(
    () => props.show,
    value => {
      if (!value) return;
      keyword.value = "";
      picked.value = [...props.selectedKeys];
      emit("search", "");
    },
    { immediate: true },
  );
 
  function close() {
    emit("update:show", false);
  }
 
  function toggle(option: SheetOption) {
    if (props.readonly) return;
    const key = String(option.key);
    if (props.multiple) {
      picked.value = pickedSet.value.has(key)
        ? picked.value.filter(item => String(item) !== key)
        : [...picked.value, option.key];
      return;
    }
    picked.value = [option.key];
    emit("confirm", [option.key]);
    close();
  }
 
  function clearAll() {
    picked.value = [];
  }
 
  function confirmPick() {
    if (props.multiple && picked.value.length === 0) {
      uni.showToast({ title: "请至少选择一条数据", icon: "none" });
      return;
    }
    emit("confirm", [...picked.value]);
    close();
  }
 
  function onKeywordConfirm() {
    emit("search", keyword.value.trim());
  }
 
  function resetKeyword() {
    keyword.value = "";
    emit("search", "");
  }
 
  /** 触底:还有下一页时请求更多(列表已有数据时不整块替换,避免闪烁) */
  function onScrollToLower() {
    if (props.loading || !props.hasMore) return;
    emit("load-more");
  }
</script>
 
<template>
  <view v-if="show" class="sheet-mask" @click="close">
    <view class="sheet" @click.stop>
      <view class="sheet-head">
        <text>{{ title }}</text>
        <text class="sheet-close" @click="close">关闭</text>
      </view>
      <view v-if="searchable" class="sheet-search">
        <input v-model="keyword" :placeholder="searchPlaceholder" confirm-type="search" @confirm="onKeywordConfirm" />
        <text v-if="keyword" @click="resetKeyword">清除</text>
        <text v-else @click="onKeywordConfirm">搜索</text>
      </view>
      <scroll-view class="sheet-body" scroll-y lower-threshold="60" @scrolltolower="onScrollToLower">
        <view v-if="loading && !items.length" class="sheet-state">加载中...</view>
        <template v-else-if="displayedItems.length">
          <view v-for="item in displayedItems" :key="item.key" class="sheet-row" @click="toggle(item)">
            <view class="sheet-row-main">
              <text class="sheet-row-label">{{ item.label }}</text>
              <text v-if="item.sub" class="sheet-row-sub">{{ item.sub }}</text>
            </view>
            <text v-if="item.extra" class="sheet-row-extra">{{ item.extra }}</text>
            <view :class="['sheet-check', { on: isPicked(item.key) }]"><text v-if="isPicked(item.key)">✓</text></view>
          </view>
          <view v-if="loading || hasMore" class="sheet-state more">{{ loading ? '加载中...' : '上拉加载更多' }}</view>
        </template>
        <view v-else class="sheet-state">{{ emptyText }}</view>
      </scroll-view>
      <view v-if="multiple && !readonly" class="sheet-foot">
        <text>已选 {{ picked.length }} 项</text>
        <view>
          <button class="ghost" @click="clearAll">清空</button>
          <button class="primary" @click="confirmPick">确定</button>
        </view>
      </view>
    </view>
  </view>
</template>
 
<style lang="scss" scoped>
  .sheet-mask{position:fixed;top:0;right:0;bottom:0;left:0;z-index:900;display:flex;align-items:flex-end;background:rgba(16,35,63,.45)}
  .sheet{display:flex;flex-direction:column;width:100%;max-height:80vh;background:#fff;border-radius:24rpx 24rpx 0 0}
  .sheet-head{display:flex;align-items:center;justify-content:space-between;height:96rpx;padding:0 28rpx;border-bottom:1rpx solid #edf1f6}
  .sheet-head text:first-child{color:#10233f;font-size:30rpx;font-weight:600}
  .sheet-close{color:var(--color-primary, #1769d8);font-size:25rpx}
  .sheet-search{display:flex;align-items:center;height:76rpx;gap:16rpx;margin:18rpx 28rpx 6rpx;padding:0 20rpx;background:#f4f7fb;border-radius:14rpx}
  .sheet-search input{min-width:0;flex:1;font-size:26rpx}
  .sheet-search text{flex:none;color:var(--color-primary, #1769d8);font-size:24rpx}
  .sheet-body{height:620rpx;flex:none;padding:8rpx 28rpx 0;box-sizing:border-box}
  .sheet-row{display:flex;align-items:center;gap:16rpx;padding:22rpx 0;border-bottom:1rpx solid #f2f5f9}
  .sheet-row-main{display:flex;min-width:0;flex:1;flex-direction:column;gap:8rpx}
  .sheet-row-label{overflow:hidden;color:#1e3049;font-size:27rpx;text-overflow:ellipsis;white-space:nowrap}
  .sheet-row-sub{overflow:hidden;color:#8494a8;font-size:22rpx;text-overflow:ellipsis;white-space:nowrap}
  .sheet-row-extra{flex:none;color:#617287;font-size:23rpx}
  .sheet-check{display:flex;width:40rpx;height:40rpx;flex:none;align-items:center;justify-content:center;color:#fff;font-size:24rpx;background:#fff;border:2rpx solid #cfd9e6;border-radius:50%}
  .sheet-check.on{background:var(--color-primary, #1769d8);border-color:var(--color-primary, #1769d8)}
  .sheet-state{padding:60rpx 0;color:#8795a8;font-size:25rpx;text-align:center}
  .sheet-state.more{padding:26rpx 0 34rpx;font-size:24rpx}
  .sheet-foot{display:flex;align-items:center;justify-content:space-between;padding:18rpx 28rpx 34rpx;border-top:1rpx solid #edf1f6}
  .sheet-foot>text{color:#617287;font-size:24rpx}
  .sheet-foot>view{display:flex;gap:16rpx}
  .sheet-foot button{height:72rpx;min-height:72rpx;padding:0 34rpx;margin:0;font-size:26rpx;line-height:70rpx;border-radius:36rpx}
  .sheet-foot button::after{border:0}
  .ghost{color:var(--color-primary, #1769d8);background:#f2f6fc;border:1rpx solid #cfe0f7}
  .primary{color:#fff;background:var(--color-primary, #1769d8)}
</style>