From 0696b8092ca12045a64d19071b071f1f6d368b17 Mon Sep 17 00:00:00 2001 From: dream2023 <1098626505@qq.com> Date: Wed, 15 May 2024 09:55:15 +0800 Subject: [PATCH] fix: add table block --- .../createTableBlock.tsx | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 packages/core/client/src/application/plugin-settings-template/createTableBlock.tsx diff --git a/packages/core/client/src/application/plugin-settings-template/createTableBlock.tsx b/packages/core/client/src/application/plugin-settings-template/createTableBlock.tsx new file mode 100644 index 0000000000..119295caf2 --- /dev/null +++ b/packages/core/client/src/application/plugin-settings-template/createTableBlock.tsx @@ -0,0 +1,85 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import React from 'react'; +import { TableProps } from 'antd'; +import { ISchema } from '@formily/json-schema'; + +import { AllDataBlockProps } from '../../data-source'; +import { useTableBlockProps } from '../../modules/blocks/data-blocks/table'; +import { SchemaComponent } from '../../schema-component'; + +interface CreateTableBlockOptions { + collectionName: string; + columns: Record>; + tableProps?: TableProps; + tableBlockProps?: Partial; +} + +export const createTableBlock = (options: CreateTableBlockOptions) => { + const { collectionName, columns, tableProps = {}, tableBlockProps = {} } = options; + + const getTableColumns = (columns: CreateTableBlockOptions['columns']) => { + return Object.keys(columns).reduce((acc, fieldName) => { + acc[fieldName] = { + type: 'void', + 'x-decorator': 'TableV2.Column.Decorator', + 'x-component': 'TableV2.Column', + properties: { + [fieldName]: { + type: 'string', + 'x-component': 'CollectionField', + 'x-pattern': 'readPretty', + 'x-component-props': columns[fieldName], + }, + }, + }; + return acc; + }, {}); + }; + + const schema: ISchema = { + type: 'void', + name: 'root', + properties: { + test: { + type: 'void', + 'x-decorator': 'TableBlockProvider', + 'x-decorator-props': { + collection: collectionName, + action: 'list', + showIndex: true, + dragSort: false, + ...tableBlockProps, + }, + properties: { + table: { + type: 'array', + 'x-component': 'TableV2', + 'x-use-component-props': useTableBlockProps, // 自动注入 Table 所需的 props + 'x-component-props': { + rowKey: 'id', + rowSelection: { + type: 'checkbox', + }, + ...tableProps, + }, + properties: getTableColumns(columns), + }, + }, + }, + }, + }; + + const TableBlockComponent = () => { + return ; + }; + + return TableBlockComponent; +};