feat(dropdown-filter): new component

This commit is contained in:
Timi
2026-11-19 14:36:12 +08:00
parent 2556fc26ff
commit 0796e73ab1
9 changed files with 558 additions and 1 deletions
+7
View File
@@ -93,6 +93,13 @@ export const COMPONENT_LIST: ComponentMeta[] = [
category: "布局组件",
entry: "src/components/master-detail-page/index.vue"
},
{
name: "DropdownFilter",
title: "DropdownFilter 筛选栏",
description: "列表页顶部的搜索框与下拉筛选,取值和事件统一收口。",
category: "布局组件",
entry: "src/components/dropdown-filter/index.vue"
},
// ---------- 编辑器组件 ----------
{
+121
View File
@@ -0,0 +1,121 @@
# DropdownFilter 筛选栏
列表页顶部的「搜索框 + 下拉筛选」。结构、取值和事件统一收在组件里,
数据从哪来、什么时候发请求、怎么同步到 URL,仍归业务。
外观不写在组件里,而是由 `page-layout.less` 的 `.tui-filters` 提供——
手写同样结构(`.tui-filters` 包住 `.t-input` 和 `.menus`)会得到同一套样式,
从旧项目搬过来的筛选栏只需要换一个根类名。
## 基础用法
:::preview 搜索 + 下拉筛选 || 输入关键词、切换品类或状态,观察筛选栏与列表的联动
demo-preview=../demos/dropdown-filter/basic.vue
:::
```vue
<dropdown-filter
v-model="keyword"
:filters="filters"
placeholder="搜索产品名称或编码"
@filter-change="handleFilterChange"
>
<template #suffix-icon>
<t-button size="extra-small" theme="primary" variant="outline" content="选择" />
</template>
</dropdown-filter>
```
`filters` 的每一项对应一个 `t-dropdown-item`,`label` 由业务算好——
组件不会根据 `value` 反查选项文本:
```ts
const filters = computed<DropdownFilterItem[]>(() => [
{
key: "category",
label: categoryOptions.find(item => item.value === category.value)?.label || "全部品类",
value: category.value,
options: categoryOptions
}
]);
```
## 一个事件管全部筛选
`filter-change` 同时承载搜索框和下拉项,业务只接一个口:
| 来源 | `key` | 说明 |
| --- | --- | --- |
| 搜索框 | `keyword` | 固定值,值已转成字符串 |
| 下拉项 | 配置里的 `key` | 值原样来自 TDesign DropdownItem |
`v-model` 照常可用,用来读搜索框内容;只接 `filter-change` 也能跑通。
```ts
function handleFilterChange(change: DropdownFilterChange): void {
if (change.key === "keyword") {
// 这里做防抖,组件不管节流
return;
}
// 其余 key 直接写回对应的 ref,再重新拉数据
}
```
搜索框每次输入都会触发事件,防抖放在业务侧——不同页面的输入成本差别很大,
组件里定一个固定间隔只会在某些页面上变成累赘。
## 放进列表页骨架
筛选栏要贴在内容区顶部、滚上去被毛玻璃盖住,需要页面骨架配合:
- 页面根节点:`tui-page-layout` + `tui-navbar`
- 内容区(`MasterDetailPage` 自带,或者手写 `main`):追加 `list`
- 滚动容器:`t-pull-down-refresh` 加 `pull-refresh`
- 筛选栏:`DropdownFilter`,或者手写 `.tui-filters`
```vue
<div class="tui-page-layout tui-navbar">
<t-navbar fixed title="库存流水" left-arrow @left-click="$router.back()" />
<master-detail-page class="list" :split="isWide" :selected="!!selectedId">
<template #master>
<t-pull-down-refresh class="pull-refresh" v-model="isRefreshing" @refresh="reload">
<dropdown-filter v-model="keyword" :filters="filters" @filter-change="handleFilterChange" />
<t-cell-group theme="card">
<!-- 列表 -->
</t-cell-group>
</t-pull-down-refresh>
</template>
</master-detail-page>
</div>
```
这三层各管一件事,缺一层都会露馅:
| 位置 | 类名 | 作用 |
| --- | --- | --- |
| 内容区 | `list` | 清掉内容区自己的上下内边距,间距改由滚动容器承担 |
| 滚动容器 | `pull-refresh` | 接管滚动,撑满剩余高度,底部让出安全区 |
| 筛选栏 | `tui-filters` | 吸顶、毛玻璃、搜索框与菜单连成一块 |
### 菜单展开时筛选栏被滚走
TDesign 展开下拉菜单时会往 `.t-pull-down-refresh__track` 写 `overflow: hidden`,
track 一旦成为滚动容器,`sticky` 就失效。骨架里已经处理了两条路:
- 浏览器支持 `:has()`:滚动容器里出现 `.tui-filters` 就自动放开 `overflow`
- 不支持,或者筛选栏被别的元素包了一层:给滚动容器加 `has-dropdown-filter`
```vue
<t-pull-down-refresh class="pull-refresh has-dropdown-filter" v-model="isRefreshing" @refresh="reload">
```
## 深色模式
筛选栏只用 TDesign 语义变量(`--td-bg-color-container`、`--td-component-border`),
深色主题下由 TDesign 的变量切换接管,不需要额外写一套。
## API
<api-table name="DropdownFilter" />
+2 -1
View File
@@ -1,6 +1,6 @@
# 组件总览
共 21 个组件,分为布局、编辑器、交互与 AI 对话四类。
共 22 个组件,分为布局、编辑器、交互与 AI 对话四类。
## 布局组件
@@ -13,6 +13,7 @@
| [MasterDetail](/components/master-detail) | 宽屏下列表与详情并排,窄屏只渲染列表。 |
| [PaneTransition](/components/pane-transition) | 与路由无关的左右推入效果,用于栏位内部的视图切换。 |
| [MasterDetailPage](/components/master-detail-page) | 把列表页骨架、主从分栏与栏内过渡打包成一个组件,列表页不再手写 master-detail。 |
| [DropdownFilter](/components/dropdown-filter) | 列表页顶部的搜索框与下拉筛选,取值和事件统一收口。 |
## 编辑器组件
+152
View File
@@ -0,0 +1,152 @@
<template>
<div class="demo">
<div class="stage">
<div class="tui-page-content list">
<t-pull-down-refresh class="pull-refresh" :value="false">
<dropdown-filter
v-model="keyword"
:filters="filters"
placeholder="搜索产品名称或编码"
@filter-change="handleFilterChange"
>
<template #suffix-icon>
<t-button size="extra-small" theme="primary" variant="outline" content="选择" />
</template>
</dropdown-filter>
<div class="list-body">
<t-cell-group v-if="visibleList.length" theme="card">
<t-cell
v-for="item in visibleList"
:key="item.id"
:title="item.name"
:description="item.code"
:note="item.status === 'on' ? '在售' : '停售'"
/>
</t-cell-group>
<p v-else class="empty" v-text="'没有匹配的产品'" />
</div>
</t-pull-down-refresh>
</div>
</div>
<p class="log">
<span v-text="'最近一次 filter-change:'" />
<code v-text="lastChange" />
</p>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from "vue";
import {
DropdownFilter,
type DropdownFilterChange,
type DropdownFilterItem,
type DropdownFilterOption
} from "timi-tdesign-mobile";
const keyword = ref("");
const category = ref("all");
const status = ref("all");
const lastChange = ref("—");
const productList = [
{ id: "p1", name: "氨基酸洁面慕斯", code: "P-1001", category: "cleanser", status: "on" },
{ id: "p2", name: "玻尿酸精华液", code: "P-1002", category: "serum", status: "on" },
{ id: "p3", name: "烟酰胺原液", code: "P-1003", category: "serum", status: "off" },
{ id: "p4", name: "舒缓修护面霜", code: "P-1004", category: "cream", status: "on" },
{ id: "p5", name: "清透防晒乳", code: "P-1005", category: "cream", status: "off" },
{ id: "p6", name: "氨基酸洗发水", code: "P-1006", category: "body", status: "on" }
];
const categoryOptions: DropdownFilterOption[] = [
{ label: "全部品类", value: "all" },
{ label: "洁面", value: "cleanser" },
{ label: "精华", value: "serum" },
{ label: "面霜", value: "cream" },
{ label: "身体护理", value: "body" }
];
const statusOptions: DropdownFilterOption[] = [
{ label: "全部状态", value: "all" },
{ label: "在售", value: "on" },
{ label: "停售", value: "off" }
];
const filters = computed<DropdownFilterItem[]>(() => [
{
key: "category",
label: categoryOptions.find(item => item.value === category.value)?.label || "全部品类",
value: category.value,
options: categoryOptions
},
{
key: "status",
label: statusOptions.find(item => item.value === status.value)?.label || "全部状态",
value: status.value,
options: statusOptions
}
]);
const visibleList = computed(() => {
const text = keyword.value.trim().toLowerCase();
return productList.filter(item => {
const matchedText = !text || `${item.name}${item.code}`.toLowerCase().includes(text);
const matchedCategory = category.value === "all" || item.category === category.value;
const matchedStatus = status.value === "all" || item.status === status.value;
return matchedText && matchedCategory && matchedStatus;
});
});
function handleFilterChange(change: DropdownFilterChange): void {
lastChange.value = `${change.key} = ${String(change.value)}`;
if (change.key === "category") {
category.value = String(change.value);
}
if (change.key === "status") {
status.value = String(change.value);
}
}
</script>
<style lang="less" scoped>
.demo {
.stage {
height: 20rem;
position: relative;
overflow: hidden;
border-radius: .75rem;
background: var(--vp-c-bg-alt);
.tui-page-content {
padding-top: 0;
}
.pull-refresh {
padding-top: 1rem;
}
}
.list-body {
margin: 0 1rem 1rem;
.empty {
margin: 0;
padding: 2rem 0;
font-size: .8125rem;
text-align: center;
color: var(--vp-c-text-3);
}
}
.log {
margin: .75rem 0 0;
font-size: .8125rem;
color: var(--vp-c-text-3);
code {
font-weight: 600;
color: var(--vp-c-brand-1);
}
}
}
</style>