类型与常量
本页列出 @org-tree/core 根入口导出的公开类型和运行时常量。
FieldMap
ts
interface FieldMap {
id: string
parentId: string
weight?: string
}将业务字段映射为结构字段。默认值:
ts
const DEFAULT_FIELD_MAP = {
id: 'id',
parentId: 'parentId',
}weight 只参与 down 布局的同级分层。
DataSource<FM>
ts
type DataSource<FM extends FieldMap = FieldMap> = /* 业务节点对象 */默认是允许任意键值的对象。传入字面量 FieldMap 后,会要求存在对应的 ID 字段:
ts
type Node = DataSource<{
id: 'nodeId'
parentId: 'pid'
}>PositionedNode
ts
interface PositionedNode {
nodeId: string
x: number
y: number
width: number
height: number
info: DataSource
children?: PositionedNode[]
}info 是原始业务节点,children 是当前参与布局的直接子节点。
NodeSize
ts
interface NodeSize {
width: number
height: number
}默认值:
ts
const DEFAULT_NODE_SIZE = {
width: 221,
height: 90,
}NodeSizeInput
ts
type NodeSizeInput =
| NodeSize
| ((item: DataSource) => NodeSize)支持统一尺寸或按原始节点动态计算。
TransformChildrenFn
ts
type TransformChildrenFn = (
parent: DataSource,
children: DataSource[],
) => DataSource[]布局树构造时递归执行。常用于同级排序和业务分组。
IsLeafFn
ts
type IsLeafFn = (node: DataSource) => boolean动态模式下判断节点是否为叶子。默认实现:
ts
const DEFAULT_IS_LEAF = (_node: DataSource) => false默认所有动态节点都可能有孩子。
LayoutConfig
ts
interface LayoutConfig {
fieldMap?: FieldMap
spaceX: number
spaceY: number
nodeSize?: NodeSizeInput
transformChildren?: TransformChildrenFn
isLeaf?: IsLeafFn
}| 字段 | 必填 | 默认值 | 说明 |
|---|---|---|---|
spaceX | 是 | — | 横向间距参数 |
spaceY | 是 | — | 纵向间距参数 |
fieldMap | 否 | id/parentId | 业务字段映射 |
nodeSize | 否 | 221 × 90 | 固定或动态节点尺寸 |
transformChildren | 否 | 原顺序 | 布局前转换直接子节点 |
isLeaf | 否 | 始终 false | 动态模式叶子判断 |
OrgTreeApi
ts
interface OrgTreeApi {
init: () => Promise<DataSource[]>
loadChildren: (nodeId: string) => Promise<DataSource[]>
}只用于动态模式。
TreeDirection
ts
type TreeDirection = 'right' | 'down'未配置时使用 'down'。
OrgTreeConfig
ts
type OrgTreeConfig = OrgTreeStaticConfig | OrgTreeApiConfigOrgTreeStaticConfig
ts
interface OrgTreeStaticConfig {
el: string | HTMLElement
direction?: TreeDirection
layout: LayoutConfig
data: DataSource[]
api?: never
}OrgTreeApiConfig
ts
interface OrgTreeApiConfig {
el: string | HTMLElement
direction?: TreeDirection
layout: LayoutConfig
api: OrgTreeApi
data?: never
}两种配置通过 data / api 形成互斥联合类型。
NodeStatus
ts
interface NodeStatus {
highlight?: boolean
visible?: boolean
expanded?: boolean
hasChildren?: boolean
loading?: boolean
}| 字段 | 说明 |
|---|---|
highlight | 业务高亮标记 |
visible | 业务可见性标记 |
expanded | 节点是否已展开 |
hasChildren | 是否存在或可能存在子节点 |
loading | 是否正在等待子节点数据 |
UpdateType
ts
enum UpdateType {
Init = 'init',
Expand = 'expand',
Collapse = 'collapse',
}作为 onUpdate 的第二个参数,说明本次布局更新原因。