feat: master-detail third pane

- MasterDetail / MasterDetailPage: add `sub` prop and `#sub` slot. The three
  columns share one container and are placed by left/right, so the detail pane
  moves between columns without remounting and keeps its scroll position.
- useMasterDetailPane: add subView / openSub / closeSub, keep the sub view in
  query and browser history alongside detail and view.
- page-layout: sub pane reuses the in-pane navbar rules; the list navbar slides
  out on the third level instead of being hidden.
- docs: demos for the third pane, the layered navbars and PaneTransition;
  restore transition durations in demo previews under prefers-reduced-motion,
  where VitePress zeroes them. Fix the missing `split` ref in the master-detail
  demo and the incorrect note about the back arrow.
This commit is contained in:
Timi
2026-11-11 23:55:30 +08:00
parent 81bbbc0662
commit ba0c7bf9a2
14 changed files with 1185 additions and 85 deletions
+9
View File
@@ -59,6 +59,7 @@ const splitList: Array<{ value: boolean | "auto"; text: string }> = [
{ value: false, text: "强制单栏" }
];
const split = ref<boolean | "auto">("auto");
const selectedId = ref("c1");
const current = computed(() => customerList.find(item => item.id === selectedId.value));
</script>
@@ -89,10 +90,18 @@ const current = computed(() => customerList.find(item => item.id === selectedId.
.frame {
height: 320px;
position: relative;
overflow: hidden;
border-radius: .75rem;
border: 1px solid var(--td-component-border);
background: var(--td-bg-color-page);
// 组件自带 tui-page-content,真实页面里它绝对定位铺满内容区,
// demo 里补上同样的定位上下文,否则两栏没有高度可分配
:deep(.tui-master-detail-page) {
inset: 0;
position: absolute;
}
}
.list {
@@ -0,0 +1,249 @@
<template>
<div class="demo">
<div class="bar">
<span class="hint" v-text="hint" />
<button type="button" class="chip" @click="reset">
<span>回到空栏</span>
</button>
</div>
<div class="frame">
<!-- `--tui-master-width` 要和 master-width 一致,否则列表页导航栏的宽度对不上 -->
<div class="tui-page-layout tui-navbar" style="--tui-master-width: 220px">
<t-navbar fixed :z-index="20" title="交易订单" left-arrow />
<master-detail-page
:split="true"
:selected="!!selectedId"
:master-width="220"
:sub="!!subView"
>
<template #master>
<div class="list">
<button
v-for="item in orderList"
:key="item.id"
type="button"
class="item"
:class="{ on: selectedId === item.id }"
@click="openDetail(item.id)"
>
<span class="no" v-text="item.no" />
<span class="amount" v-text="item.amount" />
</button>
</div>
</template>
<template #detail>
<!-- 整页结构直接塞进右栏:导航栏会退化成栏内头部 -->
<div class="tui-page-layout tui-navbar pane">
<t-navbar fixed :z-index="20" title="订单详情" left-arrow @left-click="openDetail('')" />
<main class="tui-page-content scroll column">
<div class="body">
<div class="headline" v-text="current?.no" />
<p class="row" v-text="`成交金额:${current?.amount}`" />
<p class="row" v-text="`客户:${current?.customer}`" />
<div class="actions">
<button type="button" class="action" @click="openSub('fulfillments')">
<span>查看履约记录</span>
</button>
<button type="button" class="action" @click="openSub('create')">
<span>创建履约单</span>
</button>
</div>
</div>
</main>
</div>
</template>
<template #sub>
<!-- key 跟着子页走,PaneTransition 才认得出是两个视图在切换 -->
<div v-if="subView" :key="subView" class="tui-page-layout tui-navbar pane">
<t-navbar fixed :z-index="20" :title="subTitle" left-arrow @left-click="closeSub" />
<main class="tui-page-content scroll column">
<div class="body">
<p v-for="line in subLines" :key="line" class="row" v-text="line" />
</div>
</main>
</div>
</template>
</master-detail-page>
</div>
</div>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from "vue";
import { MasterDetailPage } from "timi-tdesign-mobile";
const orderList = [
{ id: "o1", no: "SO-20261108-004", amount: "¥ 1,280.00", customer: "林秀兰" },
{ id: "o2", no: "SO-20261105-012", amount: "¥ 680.00", customer: "陈国强" },
{ id: "o3", no: "SO-20261030-008", amount: "¥ 2,150.00", customer: "王丽华" }
];
const subPages: Record<string, { title: string; lines: string[] }> = {
fulfillments: {
title: "履约记录",
lines: [
"FV-20261110-001 · 已核销 · 09:12",
"FV-20261103-006 · 已核销 · 14:40",
"FV-20261028-012 · 待核销 · 10:05"
]
},
create: {
title: "创建履约单",
lines: [
"来源订单:SO-20261108-004",
"客户:林秀兰",
"履约时间:2026-11-11 15:00"
]
}
};
const selectedId = ref("o1");
// 默认停在第三层:三条导航栏的位置关系一进来就能看到
const subView = ref("fulfillments");
const current = computed(() => orderList.find(item => item.id === selectedId.value));
const subPage = computed(() => subPages[subView.value]);
const subTitle = computed(() => subPage.value?.title ?? "");
const subLines = computed(() => subPage.value?.lines ?? []);
const hint = computed(() => {
if (!selectedId.value) {
return "空栏:导航栏满宽";
}
return subView.value ? "第三层:列表导航栏已让位" : "第二层:导航栏收窄到主栏宽";
});
function openDetail(id: string): void {
selectedId.value = id;
subView.value = "";
}
function openSub(view: string): void {
if (selectedId.value) {
subView.value = view;
}
}
function closeSub(): void {
subView.value = "";
}
function reset(): void {
selectedId.value = "";
subView.value = "";
}
</script>
<style scoped lang="less">
.demo {
.bar {
gap: .5rem;
display: flex;
align-items: center;
margin-bottom: .75rem;
justify-content: space-between;
}
.hint {
color: var(--td-text-color-secondary);
font-size: .8125rem;
}
.chip {
padding: .375rem .875rem;
color: var(--td-text-color-secondary);
font-size: .8125rem;
background: var(--td-bg-color-container);
border-radius: 99rem;
border: 1px solid var(--td-component-border);
cursor: pointer;
}
.frame {
height: 340px;
position: relative;
overflow: hidden;
border-radius: .75rem;
border: 1px solid var(--td-component-border);
background: var(--td-bg-color-page);
// 真实页面里 .tui-page-layout 是路由页,高度由 PageTransition 给;
// demo 里用 100vh 会撑爆卡片,压成卡片高度
:deep(.tui-page-layout) {
height: 100%;
min-height: 0;
}
}
.list {
height: 100%;
overflow-y: auto;
}
.item {
gap: .125rem;
width: 100%;
display: flex;
padding: .625rem .75rem;
text-align: left;
align-items: flex-start;
flex-direction: column;
background: none;
border-radius: .5rem;
border: 0;
cursor: pointer;
&.on {
background: var(--td-brand-color-1);
}
.no {
color: var(--td-text-color-primary);
font-size: .8125rem;
}
.amount {
color: var(--td-text-color-secondary);
font-size: .75rem;
}
}
.pane {
.body {
padding: 1rem;
}
.headline {
margin-bottom: .75rem;
color: var(--td-text-color-primary);
font-size: .9375rem;
font-weight: 600;
}
.row {
margin: 0 0 .5rem;
color: var(--td-text-color-secondary);
font-size: .8125rem;
line-height: 1.6;
}
.actions {
gap: .5rem;
display: flex;
margin-top: 1rem;
flex-wrap: wrap;
}
.action {
padding: .375rem .75rem;
color: var(--td-brand-color);
font-size: .8125rem;
background: var(--td-brand-color-1);
border-radius: .5rem;
border: 0;
cursor: pointer;
}
}
}
</style>
+287
View File
@@ -0,0 +1,287 @@
<template>
<div class="demo">
<div class="bar">
<span class="hint" v-text="hint" />
<button type="button" class="chip" :class="{ on: split }" @click="split = !split">
<span v-text="split ? '分栏' : '单栏'" />
</button>
</div>
<div class="frame">
<master-detail-page
:split="split"
:selected="!!selectedId"
:master-width="220"
:sub="!!subView"
>
<template #master>
<div class="list">
<button
v-for="item in customerList"
:key="item.id"
type="button"
class="item"
:class="{ on: selectedId === item.id }"
@click="openDetail(item.id)"
>
<span class="name" v-text="item.name" />
<span class="phone" v-text="item.phone" />
</button>
</div>
</template>
<template #detail>
<div class="pane">
<h3 class="title" v-text="current?.name" />
<p class="row" v-text="`联系电话:${current?.phone}`" />
<p class="row" v-text="`护理次数:${current?.visits} 次`" />
<div class="actions">
<button type="button" class="action" @click="openSub('fulfillments')">
<span>查看履约记录</span>
</button>
<button type="button" class="action" @click="openSub('create')">
<span>创建履约单</span>
</button>
</div>
</div>
</template>
<template #sub>
<!-- key 跟着子页走,PaneTransition 才认得出是两个视图在切换 -->
<div v-if="subView" :key="subView" class="pane">
<div class="pane-head">
<button type="button" class="back" @click="closeSub">
<span>返回</span>
</button>
<strong class="sub-title" v-text="subTitle" />
</div>
<p v-for="line in subLines" :key="line" class="row" v-text="line" />
</div>
</template>
</master-detail-page>
</div>
<ol class="steps">
<li v-for="step in steps" :key="step" v-text="step" />
</ol>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from "vue";
import { MasterDetailPage } from "timi-tdesign-mobile";
const customerList = [
{ id: "c1", name: "林秀兰", phone: "138 0012 3456", visits: 18 },
{ id: "c2", name: "陈国强", phone: "139 0088 1234", visits: 8 },
{ id: "c3", name: "王丽华", phone: "137 0011 5678", visits: 12 },
{ id: "c4", name: "张桂芳", phone: "135 6620 8899", visits: 26 }
];
const steps = [
"点列表项:列表 + 详情",
"点详情里的按钮:详情退到主栏,右栏换成子页(列表整列滑出左侧)",
"点子页的返回:还原成列表 + 详情,列表的滚动位置还在"
];
/** 子页内容,真实项目里由 #sub 槽里的组件自己渲染。 */
const subPages: Record<string, { title: string; lines: string[] }> = {
fulfillments: {
title: "履约记录",
lines: [
"FV-20261110-001 · 已核销 · 09:12",
"FV-20261103-006 · 已核销 · 14:40",
"FV-20261028-012 · 待核销 · 10:05",
"FV-20261020-003 · 已核销 · 16:22"
]
},
create: {
title: "创建履约单",
lines: [
"来源订单:SO-20261108-004",
"客户:林秀兰",
"产品:护理套餐 A × 1",
"履约时间:2026-11-11 15:00"
]
}
};
const split = ref(true);
const selectedId = ref("c1");
// 默认就停在第三层:一进来看到的就是「详情 + 子页」
const subView = ref("fulfillments");
const current = computed(() => customerList.find(item => item.id === selectedId.value));
const subPage = computed(() => subPages[subView.value]);
const subTitle = computed(() => subPage.value?.title ?? "");
const subLines = computed(() => subPage.value?.lines ?? []);
const hint = computed(() => {
if (!split.value) {
return "单栏下只显示一层,sub 不生效";
}
if (!selectedId.value) {
return "先点一条,再在详情里打开子页";
}
return subView.value ? "第三层:详情 + 子页" : "第二层:列表 + 详情";
});
function openDetail(id: string): void {
selectedId.value = id;
// 换一项时子页属于上一条,跟着收掉
subView.value = "";
}
function openSub(view: string): void {
if (!split.value || !selectedId.value) {
return;
}
subView.value = view;
}
function closeSub(): void {
subView.value = "";
}
</script>
<style scoped lang="less">
.demo {
.bar {
gap: .5rem;
display: flex;
align-items: center;
margin-bottom: .75rem;
justify-content: space-between;
}
.hint {
color: var(--td-text-color-secondary);
font-size: .8125rem;
}
.chip {
padding: .375rem .875rem;
color: var(--td-text-color-secondary);
font-size: .8125rem;
background: var(--td-bg-color-container);
border-radius: 99rem;
border: 1px solid var(--td-component-border);
cursor: pointer;
&.on {
color: var(--td-brand-color);
border-color: var(--td-brand-color);
background: var(--td-brand-color-1);
}
}
.frame {
height: 320px;
position: relative;
overflow: hidden;
border-radius: .75rem;
border: 1px solid var(--td-component-border);
background: var(--td-bg-color-page);
// 组件自带 tui-page-content,真实页面里它绝对定位铺满内容区,
// demo 里补上同样的定位上下文,否则三栏没有高度可分配
:deep(.tui-master-detail-page) {
inset: 0;
position: absolute;
}
}
.list {
height: 100%;
overflow-y: auto;
}
.item {
gap: .125rem;
width: 100%;
display: flex;
padding: .625rem .875rem;
text-align: left;
align-items: flex-start;
flex-direction: column;
background: none;
border-radius: .5rem;
border: 0;
cursor: pointer;
&.on {
background: var(--td-brand-color-1);
}
.name {
color: var(--td-text-color-primary);
font-size: .875rem;
}
.phone {
color: var(--td-text-color-secondary);
font-size: .75rem;
}
}
.pane {
padding: 1rem;
.title {
margin: 0 0 .75rem;
color: var(--td-text-color-primary);
font-size: 1rem;
font-weight: 600;
}
.row {
margin: 0 0 .5rem;
color: var(--td-text-color-secondary);
font-size: .8125rem;
line-height: 1.6;
}
}
.pane-head {
gap: .5rem;
display: flex;
margin-bottom: .75rem;
align-items: center;
.back {
padding: .25rem .625rem;
color: var(--td-brand-color);
font-size: .75rem;
background: none;
border-radius: 99rem;
border: 1px solid var(--td-brand-color);
cursor: pointer;
}
.sub-title {
color: var(--td-text-color-primary);
font-size: .9375rem;
}
}
.actions {
gap: .5rem;
display: flex;
margin-top: 1rem;
flex-wrap: wrap;
}
.action {
padding: .375rem .75rem;
color: var(--td-brand-color);
font-size: .8125rem;
background: var(--td-brand-color-1);
border-radius: .5rem;
border: 0;
cursor: pointer;
}
.steps {
margin: .75rem 0 0;
padding-left: 1.125rem;
color: var(--td-text-color-placeholder);
font-size: .75rem;
line-height: 1.8;
}
}
</style>
+109
View File
@@ -0,0 +1,109 @@
<template>
<div class="demo">
<div class="bar">
<button type="button" class="chip" :disabled="view !== 'list'" @click="go('detail', 'forward')">
<span>进入详情</span>
</button>
<button type="button" class="chip" :disabled="view !== 'detail'" @click="go('edit', 'forward')">
<span>进入编辑</span>
</button>
<button type="button" class="chip" :disabled="view !== 'edit'" @click="go('detail', 'back')">
<span>编辑 → 详情</span>
</button>
<button type="button" class="chip" :disabled="view === 'list'" @click="go('list', 'back')">
<span>返回列表</span>
</button>
<button type="button" class="chip" :disabled="view === 'list'" @click="go('list', 'none')">
<span>直接替换</span>
</button>
</div>
<div class="frame">
<pane-transition :direction="direction">
<div v-if="view === 'list'" key="list" class="pane">
<h4 class="title" v-text="'客户列表'" />
<p class="row" v-text="'当前在列表层,按「进入详情」看 forward 推入。'" />
</div>
<div v-else-if="view === 'detail'" key="detail" class="pane">
<h4 class="title" v-text="'客户详情'" />
<p class="row" v-text="'forward:新内容从右侧推过来,旧内容左移让位。'" />
<p class="row" v-text="'back:新内容从左侧覆盖回来,旧内容滑出右侧。'" />
</div>
<div v-else key="edit" class="pane">
<h4 class="title" v-text="'编辑客户'" />
<p class="row" v-text="'编辑层。按「编辑 → 详情」看 back 反向播放。'" />
</div>
</pane-transition>
</div>
</div>
</template>
<script lang="ts" setup>
import { ref } from "vue";
import { PaneTransition, type PaneDirection } from "timi-tdesign-mobile";
type ViewName = "list" | "detail" | "edit";
const view = ref<ViewName>("list");
const direction = ref<PaneDirection>("forward");
function go(next: ViewName, nextDirection: PaneDirection): void {
direction.value = nextDirection;
view.value = next;
}
</script>
<style scoped lang="less">
.demo {
.bar {
gap: .5rem;
display: flex;
margin-bottom: .75rem;
flex-wrap: wrap;
}
.chip {
padding: .375rem .875rem;
color: var(--td-text-color-secondary);
font-size: .8125rem;
background: var(--td-bg-color-container);
border-radius: 99rem;
border: 1px solid var(--td-component-border);
cursor: pointer;
&:disabled {
opacity: .4;
cursor: not-allowed;
}
}
.frame {
height: 220px;
position: relative;
overflow: hidden;
border-radius: .75rem;
border: 1px solid var(--td-component-border);
background: var(--td-bg-color-page);
}
.pane {
height: 100%;
padding: 1rem;
box-sizing: border-box;
background: var(--td-bg-color-container);
}
.title {
margin: 0 0 .75rem;
color: var(--td-text-color-primary);
font-size: 1rem;
font-weight: 600;
}
.row {
margin: 0 0 .5rem;
color: var(--td-text-color-secondary);
font-size: .8125rem;
line-height: 1.6;
}
}
</style>