跳转到正文

工具栏生成器 ​

Toolbar 通过数据组合按钮、下拉菜单、按钮组、分隔线和间距。它继承按钮组和通用列表的能力,适合表格操作区、编辑操作区等场景。

基本使用 ​

请选择一个操作

条目类型 ​

type内容主要选项
item 或省略普通按钮text、icon、btnType、url、disabled、onClick
dropdown下拉按钮items、menu、dropdown、caret
btn-group嵌套按钮组items、btnProps、size
divider分隔线可用 className 调整样式
space占位间距space 设置尺寸,flex 设置弹性
angle右向分隔箭头可用 className 调整样式

type 决定条目类型,按钮的配色和外观使用 btnType。普通条目带有 items 或 dropdown 且未指定类型时,会按下拉按钮处理。

js
toolbar.render({items: [
    {type: 'btn-group', items: [
        {text: '加粗', icon: {"className": "example-icon example-icon-bold", "style": {"display": "inline-block", "width": "1em", "height": "1em", "backgroundColor": "currentColor", "mask": "url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Ctext%20x%3D%224%22%20y%3D%2219%22%20font-family%3D%22sans-serif%22%20font-size%3D%2220%22%20font-weight%3D%22bold%22%3EB%3C%2Ftext%3E%3C%2Fsvg%3E) center / contain no-repeat"}}},
        {text: '斜体', icon: {"className": "example-icon example-icon-italic", "style": {"display": "inline-block", "width": "1em", "height": "1em", "backgroundColor": "currentColor", "mask": "url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Ctext%20x%3D%224%22%20y%3D%2219%22%20font-family%3D%22sans-serif%22%20font-size%3D%2220%22%20font-weight%3D%22bold%22%3EI%3C%2Ftext%3E%3C%2Fsvg%3E) center / contain no-repeat"}}},
        {text: '下划线', icon: {"className": "example-icon example-icon-underline", "style": {"display": "inline-block", "width": "1em", "height": "1em", "backgroundColor": "currentColor", "mask": "url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Ctext%20x%3D%224%22%20y%3D%2219%22%20font-family%3D%22sans-serif%22%20font-size%3D%2220%22%20font-weight%3D%22bold%22%3EU%3C%2Ftext%3E%3C%2Fsvg%3E) center / contain no-repeat"}}},
    ]},
    {type: 'space', flex: 1},
    {text: '完成', btnType: 'primary'},
]});

嵌套按钮组的操作应在其自身的 onClickItem 或按钮 onClick 中处理。下拉菜单项的回调也在菜单内部定义,详见菜单生成器。

共享样式与布局 ​

btnProps 为按钮设置共享属性,btnType、size 提供统一外观;单项配置可以覆盖共享配置。工具栏默认共享 btnType: 'ghost'。

js
toolbar.render({
    size: 'sm',
    btnType: 'secondary',
    gap: '0.75rem',
});

数值 gap 生成公开的 gap-* 工具类,例如 gap: 2 对应 gap-2;CSS 长度使用字符串。需要换行时使用 className: 'flex-wrap'。图标按钮应通过 attrs: {'aria-label': '保存'} 提供可访问名称,hint 可提供悬停提示。

点击、动态条目和更新 ​

onClickItem(info) 接收原始 item、index、最终的 renderedItem、event 和 relativeTarget。单项 onClick(event, info) 可以处理独立操作。

items 支持数组、同步返回数组的函数和 datalist 引用(如 #actions)。远程数据可在请求完成后调用 render({items}) 更新:

js
toolbar.render({items: [
    {id: 'save', text: '保存', icon: {"className": "example-icon example-icon-save", "style": {"display": "inline-block", "width": "1em", "height": "1em", "backgroundColor": "currentColor", "mask": "url(data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20d%3D%22M4%203h13l4%204v14H3V3zM7%203v6h9V3M7%2021v-8h10v8%22%20fill%3D%22none%22%20stroke%3D%22black%22%20stroke-width%3D%221.7%22%20stroke-linecap%3D%22round%22%20stroke-linejoin%3D%22round%22%2F%3E%3C%2Fsvg%3E) center / contain no-repeat"}}, disabled: false},
    {id: 'publish', text: '发布', btnType: 'primary'},
]});

只通过 setOptions() 更新配置不会完成 UI 更新;需要重新渲染时使用 render()。

选项 ​

名称类型默认值说明
itemsItem[] | (() => Item[]) | DatalistRef—数组、同步工厂或 #id。
itemKeystring—条目标识字段;未设置时使用 key 或条目索引。
classNameClassNameLike—工具栏附加类名。
gapnumber | string—工具类级别或 CSS 间距值。
size"xs" | "sm" | "md" | "lg" | "xl"—共享按钮尺寸。
btnTypestring—共享按钮外观。
btnPropsPartial<ButtonProps>—共享按钮属性,默认 btnType 为 ghost。
itemPropsPartial<Item>—共享条目属性。
relativeTargetunknown—关联业务对象,可在点击回调中取得。
onClickItem(info: object) => void—顶层条目点击回调。
getItems(items: Item[]) => Item[] | undefined—渲染前转换条目数组。
itemRenderItemRender | Record<string, ItemRender>—自定义条目内容。

按钮、下拉按钮和按钮组的完整选项分别参见按钮、下拉菜单和按钮组。

源码工作区入口与销毁 ​

以下入口用于 ZUI 源码工作区,需要解析 @zui/* 并编译 TypeScript;使用 JSX 时还需配置 Preact。标准 npm 包通过 zui 和 zui/css 接入。

ts
import {Toolbar} from '@zui/toolbar';
import {Toolbar as ToolbarView} from '@zui/toolbar/react';
import type {ToolbarOptions, ToolbarItemOptions} from '@zui/toolbar';

Preact 用法为 render(<ToolbarView {...options} />, element),其中 render 从 preact 引入;页面需加载 ZUI 样式。

js
const instance = zui.Toolbar.get('#documentToolbar');
toolbar.destroy();

在单页应用移除工具栏时销毁原生实例。