iter(g111): 面包屑支持超长路径折叠
This commit is contained in:
@@ -1,20 +1,41 @@
|
||||
import { h } from '@ui-arch/frame';
|
||||
import { effect, h, signal } from '@ui-arch/frame';
|
||||
import type { BreadcrumbProps } from './types.js';
|
||||
|
||||
/*** 面包屑:nav + ol 语义结构,末项为当前页(不可点、aria-current);有 href 渲染 <a>,无 href 渲染 button 交 onNavigate ***/
|
||||
/*** 面包屑:nav + ol 语义结构,末项为当前页(不可点、aria-current);条目超过 maxItems 时把中段折叠成「…」按钮 ***/
|
||||
export function breadcrumb(props: BreadcrumbProps): HTMLElement {
|
||||
// —— 状态集中声明 ——
|
||||
const items = props.items;
|
||||
const sep = props.separator ?? '/';
|
||||
const sep = props.separator ?? '/'; // 分隔符
|
||||
const max = props.maxItems ?? Number.POSITIVE_INFINITY; // 展示条数上限:超出折叠中段
|
||||
const expanded = signal(false); // 折叠段是否已展开(展开后不再折叠)
|
||||
const list = h('ol', { class: 'ua-crumb-list' });
|
||||
|
||||
const entries = items.map((item, i) => {
|
||||
const last = i === items.length - 1; // 末项即当前页
|
||||
const node = last
|
||||
? h('span', { class: 'ua-crumb-current', 'aria-current': 'page' }, item.label)
|
||||
: item.href
|
||||
? h('a', { class: 'ua-crumb-link', href: item.href }, item.label)
|
||||
: h('button', { type: 'button', class: 'ua-crumb-link', onClick: () => props.onNavigate?.(item, i) }, item.label);
|
||||
return h('li', { class: 'ua-crumb-item' }, node, last ? null : h('span', { class: 'ua-crumb-sep', 'aria-hidden': 'true' }, sep));
|
||||
});
|
||||
/*** 逐项渲染:有 href 用 <a>,无 href 用 button 交 onNavigate;折叠按钮紧跟首项插入一次 ***/
|
||||
const render = (): void => {
|
||||
const fold = !expanded() && items.length > max; // 是否需要折叠
|
||||
const tail = Math.max(2, max - 1); // 尾部保留条数:至少当前页 + 上一级
|
||||
const kept = fold ? [0, ...items.slice(-tail).map((_, k) => items.length - tail + k)] : items.map((_, i) => i);
|
||||
const nodes: HTMLElement[] = [];
|
||||
kept.forEach((idx, pos) => {
|
||||
const item = items[idx];
|
||||
const last = idx === items.length - 1; // 末项即当前页
|
||||
const node = last
|
||||
? h('span', { class: 'ua-crumb-current', 'aria-current': 'page' }, item.label)
|
||||
: item.href
|
||||
? h('a', { class: 'ua-crumb-link', href: item.href }, item.label)
|
||||
: h('button', { type: 'button', class: 'ua-crumb-link', onClick: () => props.onNavigate?.(item, idx) }, item.label);
|
||||
nodes.push(h('li', { class: 'ua-crumb-item' }, node, last ? null : h('span', { class: 'ua-crumb-sep', 'aria-hidden': 'true' }, sep)));
|
||||
if (!fold || pos > 0) return; // 卫语句:折叠按钮只插一次
|
||||
const hidden = items.length - kept.length; // 被折叠的条数
|
||||
nodes.push(h('li', { class: 'ua-crumb-item' }, h(
|
||||
'button',
|
||||
{ type: 'button', class: 'ua-crumb-fold', 'aria-label': `展开折叠的 ${hidden} 项`, onClick: () => expanded(true) },
|
||||
'…',
|
||||
), h('span', { class: 'ua-crumb-sep', 'aria-hidden': 'true' }, sep)));
|
||||
});
|
||||
list.replaceChildren(...nodes);
|
||||
};
|
||||
|
||||
return h('nav', { class: 'ua-breadcrumb', 'aria-label': '面包屑导航' }, h('ol', { class: 'ua-crumb-list' }, ...entries));
|
||||
effect(render); // expanded 变化即重绘
|
||||
return h('nav', { class: 'ua-breadcrumb', 'aria-label': '面包屑导航' }, list);
|
||||
}
|
||||
|
||||
@@ -149,6 +149,7 @@ export interface BreadcrumbItem {
|
||||
export interface BreadcrumbProps {
|
||||
items: BreadcrumbItem[]; // 路径条目,末项视为当前页(不可点)
|
||||
separator?: string; // 分隔符;缺省 '/'
|
||||
maxItems?: number; // 最多展示几项;超出时中段折叠成「…」按钮,点开即完整展开;缺省不折叠
|
||||
onNavigate?: (item: BreadcrumbItem, index: number) => void; // 无 href 条目的点击回调
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user