/ DashKit

DashKit

用于构建交互式仪表板的网格组件。

@gravity-ui/dashkit · npm package CI storybook

DashKit

一个用于渲染仪表盘网格的库。

安装

npm i @gravity-ui/dashkit @gravity-ui/uikit

描述

该库用于在网格中排列小部件、调整它们的大小、添加新小部件以及删除它们。 小部件是一个 React 组件。例如,文本、图形和图像。

新小部件通过插件系统添加。

插件

插件是创建自定义小部件所必需的。

Props

type ItemManipulationCallback = (eventData: {
    layout: Layouts;
    oldItem: Layout;
    newItem: Layout;
    placeholder: Layout;
    e: MouseEvent;
    element: HTMLElement;
}) => void;

interface DashKitProps {
  config: Config;
  editMode: boolean;
  onItemEdit: ({id}: {id: string}) => void;
  onChange: (data: {config: Config; itemsStateAndParams: ItemsStateAndParams}) => void;
  onDrop: (dropProps: ItemDropProps) => void;
  onItemMountChange: (item: ConfigItem, state: {isAsync: boolead; isMounted: boolean}) => void;
  onItemRender: (item: ConfigItem) => void;

  onDragStart?: ItemManipulationCallback;
  onDrag?: ItemManipulationCallback;
  onDragStop?: ItemManipulationCallback;
  onResizeStart?: ItemManipulationCallback;
  onResize?: ItemManipulationCallback;
  onResizeStop?: ItemManipulationCallback;

  defaultGlobalParams: GlobalParams;
  globalParams: GlobalParams;
  itemsStateAndParams: ItemsStateAndParams;
  settings: SettingsProps;
  context: ContextProps;
  overlayControls?: Record<string, OverlayControlItem[]> | null;
  overlayMenuItems?: MenuItems[] | null;
  noOverlay?: boolean;

  focusable?: boolean;
  onItemFocus: (item: ConfigItem) => void;
  onItemBlur: (item: ConfigItem) => void;

  draggableHandleClassName?: string;
  getPreparedCopyItemOptions?: (options: PreparedCopyItemOptions) => PreparedCopyItemOptions;
  onCopyFulfill?: (error: null | Error, data?: PreparedCopyItemOptions) => void;
}
  • config: 配置
  • editMode: 是否启用编辑模式。
  • onItemEdit: 点击编辑小部件时调用。
  • onChange: 配置或 itemsStateAndParams 更改时调用。
  • onDrop: 使用 (#DashKitDnDWrapper) 从 ActionPanel 拖放项目时调用
  • onItemMountChange: 项目挂载状态更改时调用
  • onItemRender: 项目渲染完成时调用
  • defaultGlobalParams, globalParams: 影响所有小部件的参数。在 DataLens 中,defaultGlobalParams 是在仪表盘设置中设置的全局参数。globalParams 是可以在 URL 中设置的全局参数。
  • itemsStateAndParams: itemsStateAndParams
  • settings: DashKit 设置。
  • context: 将作为 props 传递给所有小部件的对象。
  • overlayControls: 在编辑时覆盖小部件控件的对象。如果未传递,将显示基本控件。如果传递 null,则仅显示关闭按钮或自定义菜单。
  • overlayMenuItems: 自定义下拉菜单项
  • noOverlay: 如果为 true,则在编辑时不会显示覆盖层和控件。
  • focusable: 如果为 true,则网格项将可聚焦。
  • onItemFocus: 当 focusable 为 true 且项目获得焦点时调用。
  • onItemBlur: 当 focusable 为 true 且项目失去焦点时调用。
  • draggableHandleClassName: 使小部件可拖动的元素的 CSS 类名。
  • onDragStart: ReactGridLayout 在项目拖动开始时调用
  • onDrag: ReactGridLayout 在项目拖动过程中调用
  • onDragStop: ReactGridLayout 在项目拖动停止时调用
  • onResizeStart: ReactGridLayout 在项目调整大小开始时调用
  • onResize: ReactGridLayout 在项目调整大小过程中调用
  • onResizeStop: ReactGridLayout 在项目调整大小停止时调用
  • getPreparedCopyItemOptions: 在将复制的项目保存到本地存储之前,用于将复制的项目转换为可序列化对象。它应该替代已弃用的 context.getPreparedCopyItemOptions prop。
  • onCopyFulfill: 在项目复制成功完成时(error=nulldata 已定义)调用,否则以 error: Error(无 data)调用。

用法

DashKit 配置

在使用 DashKit 作为 React 组件之前,必须对其进行配置。

  • 设置语言

    import {configure, Lang} from '@gravity-ui/uikit';
    
    configure({lang: Lang.En});
    
  • DashKit.setSettings

    用于全局 DashKit 设置(例如,小部件之间的边距、默认小部件大小和小部件覆盖菜单)

    import {DashKit} from '@gravity-ui/dashkit';
    
    DashKit.setSettings({
      gridLayout: {margin: [8, 8]},
      isMobile: true,
      // menu: [] as Array<MenuItem>,
    });
    
  • DashKit.registerPlugins

    注册和配置插件

    import {DashKit} from '@gravity-ui/dashkit';
    import {pluginTitle, pluginText} from '@gravity-ui/dashkit';
    
    DashKit.registerPlugins(
      pluginTitle,
      pluginText.setSettings({
        apiHandler({text}) {
          return api.getMarkdown(text);
        },
      }),
    );
    
    DashKit.registerPlugins({
      type: 'custom',
      defaultLayout: {
        w: 10,
        h: 8,
      },
      renderer: function CustomPlugin() {
        return <div>Custom widget with custom controls</div>;
      },
    });
    

Config

export interface Config {
  salt: string; // 用于形成唯一 ID
  counter: number; // 用于形成唯一 ID,仅递增
  items: ConfigItem[]; //  初始小部件状态
  layout: ConfigLayout[]; // 网格上的小部件位置 https://github.com/react-grid-layout
  aliases: ConfigAliases; // 参数的别名,参见 #Params
  connections: ConfigConnection[]; // 小部件之间的链接,参见 #Params
}

Config 示例:

import {DashKitProps} from '@gravity-ui/dashkit';
<!-- Language options -->
<div class="language-options">
  <a href="/en/README.md">English</a>
  <a href="/zh/README.md">中文</a>
</div>

# DashKit

DashKit 是一个用于创建和管理仪表板的库。它提供了一个灵活的框架来构建交互式仪表板,允许用户自定义布局、添加和删除小部件,并配置小部件的参数。

## 安装

```bash
npm install @gravity-ui/dashkit
# or
yarn add @gravity-ui/dashkit

用法

配置

DashKit 的核心是其配置对象,它定义了仪表板的布局、小部件及其数据。

const config: DashKitProps['config'] = {
  salt: '0.46703554571365613',
  counter: 4,
  items: [
    {
      id: 'tT',
      data: {
        size: 'm',
        text: 'Caption',
        showInTOC: true,
      },
      type: 'title',
      namespace: 'default',
      orderId: 1,
    },
    {
      id: 'Ea',
      data: {
        text: 'mode _editActive',
        _editActive: true,
      },
      type: 'text',
      namespace: 'default',
    },
    {
      id: 'zR',
      data: {
        text: '### Text',
      },
      type: 'text',
      namespace: 'default',
      orderId: 0,
    },
    {
      id: 'Dk',
      data: {
        foo: 'bar',
      },
      type: 'custom',
      namespace: 'default',
      orderId: 5,
    },
  ],
  layout: [
    {
      h: 2,
      i: 'tT',
      w: 36,
      x: 0,
      y: 0,
    },
    {
      h: 6,
      i: 'Ea',
      w: 12,
      x: 0,
      y: 2,
    },
    {
      h: 6,
      i: 'zR',
      w: 12,
      x: 12,
      y: 2,
    },
    {
      h: 4,
      i: 'Dk',
      w: 8,
      x: 0,
      y: 8,
    },
  ],
  aliases: {},
  connections: [],
};

添加新项

const newLayout = updateLayout: [
  {
    h: 6,
    i: 'Ea',
    w: 12,
    x: 0,
    y: 6,
  },
  {
    h: 4,
    i: 'Dk',
    w: 8,
    x: 0,
    y: 12,
  },
];

const newConfig = DashKit.setItem({
  item: {
    data: {
      text: `Some text`,
    },
    namespace: 'default',
    type: 'text',
    // Optional. If new item needed to be inserted in current layout with predefined dimensions
    layout: { // Current item inseterted before 'Ea'
      h: 6,
      w: 12,
      x: 0,
      y: 2,
    },,
  },
  config: config,
  options: {
    // Optional. New layout values for existing items when new element is dropped from ActionPanel
    updateLayout: newLayout,
  },
});

修改现有项

const newConfig = DashKit.setItem({
  item: {
    id: 'tT', // item.id
    data: {
      size: 'm',
      text: `New caption`,
    },
    namespace: 'default',
    type: 'title',
  },
  config: config,
});

删除项

import {DashKitProps} from '@gravity-ui/dashkit';

const oldItemsStateAndParams: DashKitProps['itemsStateAndParams'] = {};

const {config: newConfig, itemsStateAndParams} = DashKit.removeItem({
  id: 'tT', // item.id
  config: config,
  itemsStateAndParams: this.state.itemsStateAndParams,
});

参数

type Params = Record<string, string | string[]>;

DashKit 根据小部件、链接和别名的默认参数生成参数。这些参数是 ChartKit 库所必需的。

生成顺序:

  1. defaultGlobalParams
  2. 默认小部件参数 item.default
  3. globalParams
  4. 根据队列从 itemsStateAndParams 中获取的参数。

itemsStateAndParams

一个对象,用于存储小部件参数和状态以及参数更改队列。 它有一个 __meta__ 字段,用于存储队列和元信息。

interface StateAndParamsMeta = {
    __meta__: {
        queue: {id: string}[]; // 队列
        version: number; // itemsStateAndParams 的当前版本
    };
}

以及小部件状态和参数:

interface ItemsStateAndParamsBase {
  [itemId: string]: {
    state?: Record<string, any>;
    params?: Params;
  };
}
type ItemsStateAndParams = StateAndParamsMeta & ItemsStateAndParamsBase;

菜单

您可以在编辑模式下指定自定义的 DashKit 小部件覆盖菜单。

type MenuItem = {
  id: string; // 唯一 ID
  title?: string; // 字符串标题
  icon?: ReactNode; // 图标节点
  iconSize?: number | string; // 图标大小,以像素为单位的数字或带单位的字符串
  handler?: (item: ConfigItem) => void; // 自定义菜单项操作处理程序
  visible?: (item: ConfigItem) => boolean; // 用于过滤菜单项的可选可见性处理程序
  className?: string; // 自定义类属性
};

// 在设置中使用菜单项数组
<Dashkit overlayMenuItems={[] as Array<MenuItem> | null} />

[已弃用]
// overlayMenuItems 属性的优先级高于 setSettings 菜单
DashKit.setSettings({menu: [] as Array<MenuItem>});

从 ActionPanel 拖拽项目

DashKitDnDWrapper

type DraggedOverItem = {
  h: number;
  w: number;
  type: string;
  parent: string;
  i?: number;
};

interface DashKitDnDWrapperProps {
  dragImageSrc?: string;
  onDragStart?: (dragProps: ItemDragProps) => void;
  onDragEnd?: () => void;
  onDropDragOver?: (draggedItem: DraggedOverItem, sharedItem: DraggedOverItem | null) => void | boolean;
}
  • dragImageSrc: 拖拽图像预览,默认为透明的 1px png base64
  • onDragStart: 当元素从 ActionPanel 拖拽时调用的回调函数
  • onDragEnd: 当元素被放置或拖拽取消时调用的回调函数

```ts
type ItemDragProps = {
    type: string; // 插件类型
    layout?: { // 可选。用于预览和初始化的布局项大小
        w?: number;
        h?: number;
    };
    extra?: any; // 自定义用户上下文
};
type ItemDropProps = {
    commit: () => void; // 在完成所有配置操作后应调用此回调
    dragProps: ItemDragProps; // 项拖拽属性
    itemLayout: ConfigLayout; // 计算出的项布局尺寸
    newLayout: ConfigLayout[]; // 元素被放置后的新布局
};

示例:

const overlayMenuItems = [
  {
    id: 'chart',
    icon: <Icon data={ChartColumn} />,
    title: '图表',
    qa: 'chart',
    dragProps: { // ItemDragProps
        type: 'custom', // 已注册的插件类型
    },
  }
]

const onDrop = (dropProps: ItemDropProps) => {
  // ... 将元素添加到您的配置中
  dropProps.commit();
}

<DashKitDnDWrapper>
  <DashKit editMode={true} config={config} onChange={onChange} onDrop={onDrop} />
  <ActionPanel items={overlayMenuItems} />
</DashKitDnDWrapper>

CSS API

名称描述
操作面板变量
--dashkit-action-panel-color背景颜色
--dashkit-action-panel-border-color边框颜色
--dashkit-action-panel-border-radius边框圆角
操作面板项变量
--dashkit-action-panel-item-color背景颜色
--dashkit-action-panel-item-text-color文本颜色
--dashkit-action-panel-item-color-hover悬停背景颜色
--dashkit-action-panel-item-text-color-hover悬停文本颜色
覆盖层变量
--dashkit-overlay-border-color边框颜色
--dashkit-overlay-color背景颜色
--dashkit-overlay-opacity透明度
网格项变量
--dashkit-grid-item-edit-opacity透明度
--dashkit-grid-item-border-radius边框圆角
占位符变量
--dashkit-placeholder-color背景颜色
--dashkit-placeholder-opacity透明度

使用示例

.custom-theme-wrapper {
  --dashkit-grid-item-edit-opacit: 1;
  --dashkit-overlay-color: var(--g-color-base-float);
  --dashkit-overlay-border-color: var(--g-color-base-float);
  --dashkit-overlay-opacity: 0.5;

  --dashkit-action-panel-border-color: var(--g-color-line-info);
  --dashkit-action-panel-color: var(--g-color-base-float-accent);
  --dashkit-action-panel-border-radius: var(--g-border-radius-xxl);
}
// ....

const CustomThemeWrapper = (props: {
  dashkitProps: DashkitProps;
  actionPanelProps: ActionPanelProps;
}) => {
  return (
    <div className="custom-theme-wrapper">
      <Dashkit {...props.dashkitProps} />
      <ActionPanel {...props.actionPanelProps} />
    </div>
  );
};

开发

构建与监听

  • 构建依赖 npm ci
  • 构建项目 npm run build
  • 构建 Storybook npm run start

默认情况下,Storybook 运行在 http://localhost:7120/。 当 Storybook 运行时,项目中的新更改不一定会被捕获,因此最好手动重新构建项目并重启 Storybook。

开发机器上用于开发的 Nginx 配置示例

server {
    server_name dashkit.username.ru;

    include common/ssl;

    access_log /home/username/logs/common.access.log;
    error_log /home/username/logs/common.error.log;

    root /home/username/projects/dashkit;

    location / {
        try_files $uri @node;
    }

    location @node {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:7120;
        proxy_redirect off;
    }
}

关于库
星标
16
版本
9.1.1
最后更新
17.06.2025
代码仓库
github.com/gravity-ui/dashkit
许可证
MIT License
维护者