feat(master-detail): add cover sub-mode and providePane

- MasterDetail / MasterDetailPage: add `sub-mode` ("shift" | "cover", default
  "shift", so existing callers are unaffected). `cover` leaves the master pane
  untouched and puts the sub pane over the detail column: the case where the
  list must stay visible while drilling into another module. The detail pane
  fades out in place instead of sliding into the master column, so it stays
  mounted and keeps its scroll position and internal state.
- use-master-detail: export `providePane(context)` and return `close` from
  useMasterDetailPane. A page can now hand a derived pane context to its whole
  subtree, which is how a host rewrites sub view ids (prefixing them with the
  owning record). The alternative - a wrapper component - breaks the
  `.detail > .tui-page-layout` selectors in page-layout.less and sends the
  in-pane navbar back to `position: fixed`.
- docs: cover vs shift, and how to prefix sub view ids without a wrapper.
This commit is contained in:
Timi
2026-11-18 15:08:44 +08:00
parent a50f134b25
commit bdaee75b81
8 changed files with 200 additions and 35 deletions
+58 -29
View File
@@ -86,6 +86,24 @@ export function usePane(): PaneContext | null {
return inject(PANE_KEY, null);
}
/**
* 把一套栏内上下文提供给后代。
*
* 用在"页面要改写子页标识"的场合:列表页在最外层 provide 一套派生上下文,
* 右栏里的页面调 `openSub()` 时标识先经过它,不必在中间塞一层容器组件。
*
* 不要用容器组件代替它:栏内页面的骨架样式(导航栏退化成栏内头部、内容区铺满)
* 挂在 `.detail > .tui-page-layout` 这类直系选择器上,多包一层就全落空。
*
* 注意 provide 只在当前组件的后代里生效,且会遮蔽上层的同一套上下文,
* 所以派生时要把 `close` / `openSub` 转发给宿主,别自己重写一套。
*
* @param context 派生出来的上下文,通常转发 `usePane()` 拿到的宿主上下文
*/
export function providePane(context: PaneContext): void {
provide(PANE_KEY, context);
}
/**
* 主从布局的选中态管理。
*
@@ -99,7 +117,7 @@ export function useMasterDetail(options: UseMasterDetailOptions = {}): UseMaster
const isWide = layout.isWide;
// 右栏里的页面通过 usePane().close() 把控制权交回来
provide(PANE_KEY, {
providePane({
close: (saved = false) => options.onClose?.(saved),
openSub: view => options.onOpenSub?.(view) ?? false
});
@@ -226,6 +244,13 @@ export interface UseMasterDetailPaneReturn {
openSub: (view: string) => boolean;
/** 关闭子页面,回到详情层。 */
closeSub: () => void;
/**
* 关闭右栏最上面一层:有子页先关子页,其次关编辑,最后关详情。
*
* 和 `usePane().close()` 是同一套逻辑,给"页面要派生一套栏内上下文"时转发用
* (见 `providePane`)。
*/
close: (saved?: boolean) => void;
}
/**
@@ -313,35 +338,38 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
? options.edit(id)
: options.detail(id)),
onOpenSub: requestSub,
onClose: saved => {
if (popPane(saved)) {
return;
}
// 降级:没有可回退的层级,只改 query
pendingNav.value = "replace";
paneDirection.value = "back";
// 有子页先关子页,回到详情
if (subView.value) {
subView.value = "";
return;
}
// 取消/保存编辑:回到详情,保存过就让详情重新加载
if (editing.value) {
editing.value = false;
if (saved) {
detailVersion.value++;
}
return;
}
// 关闭详情:右滑出,回到空栏
selectedId.value = "";
}
onClose: saved => closePane(saved)
});
/** 关闭右栏最上面一层;页面派生上下文时原样转发这一个。 */
function closePane(saved = false): void {
if (popPane(saved)) {
return;
}
// 降级:没有可回退的层级,只改 query
pendingNav.value = "replace";
paneDirection.value = "back";
// 有子页先关子页,回到详情
if (subView.value) {
subView.value = "";
return;
}
// 取消/保存编辑:回到详情,保存过就让详情重新加载
if (editing.value) {
editing.value = false;
if (saved) {
detailVersion.value++;
}
return;
}
// 关闭详情:右滑出,回到空栏
selectedId.value = "";
}
function openDetail(id: string): void {
// 正在编辑时直接点列表里另一项:栈顶是编辑层,就地改成新客户的详情的话,
// 下面那条旧客户详情会重新可达,返回时会掉回上一个客户。
@@ -494,7 +522,8 @@ export function useMasterDetailPane(options: UseMasterDetailPaneOptions): UseMas
openDetail,
openEdit,
openSub: requestSub,
closeSub
closeSub,
close: closePane
};
}