Skip to content

Commit

Permalink
feat: add eventPreprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
tinymins committed Aug 29, 2024
1 parent a591184 commit 79f9765
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 3 deletions.
1 change: 1 addition & 0 deletions .dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ const DRIP_TABLE_SIDEBAR = [
{ title: '默认组件库名 defaultComponentLib', link: '/drip-table/props/default-component-lib' },
{ title: '组件插槽 slots', link: '/drip-table/props/slots' },
{ title: '附加透传数据 ext', link: '/drip-table/props/ext' },
{ title: '事件预处理 eventPreprocessor', link: '/drip-table/props/event-preprocessor' },
{ title: '指定行可展开 rowExpandable', link: '/drip-table/props/row-expandable' },
{ title: '行展开渲染函数 expandedRowRender', link: '/drip-table/props/expanded-row-render' },
{ title: '子表默认展开项 defaultExpandedRowKeys', link: '/drip-table/props/default-expanded-row-keys' },
Expand Down
127 changes: 127 additions & 0 deletions docs/drip-table/props/event-preprocessor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
---
title: 事件预处理 eventPreprocessor
toc: content
---

## 表格事件预处理 eventPreprocessor

- 描述:表格 Schema 中事件函数预处理
- 类型:

```typescript
(event: unknown, props: Record<string, unknown>) => (() => void);
```

- 默认值:`undefined`

```tsx
/**
* transform: true
* defaultShowCode: true
* hideActions: ["CSB"]
*/
import React from "react";
import { message } from "antd";
import DripTable, { DripTableComponentProps, DripTableRecordTypeBase } from "drip-table";

// 自定义组件表格渲染列 Schema 类型定义。
type SampleColumnSchema = DripTableColumnSchema<'custom::Sample', {
onClick: () => void;
}>;
// 自定义组件属性接口定义。
interface SampleComponentProps<RecordType extends DripTableRecordTypeBase> extends DripTableComponentProps<RecordType, SampleColumnSchema> { }

// 自定义组件实现。
const SampleComponent = <RecordType extends DripTableRecordTypeBase>(props: SampleComponentProps<RecordType>) => (
<div onClick={props.schema.options.onClick}>Sample: CLICK ME</div>
);
SampleComponent.componentName = 'Sample';
SampleComponent.schema = { // https://ajv.js.org/json-schema.html
type: 'object',
properties: {
onClick: { instanceof: 'Function' },
},
};

// 对所有自定义组件做集合处理,准备提供给 DripTable 使用。
const customComponents = {
[SampleComponent.componentName]: SampleComponent,
};
type CustomColumnSchema = SampleColumnSchema;

const schema: DripTableSchema<CustomColumnSchema> = {
columns: [
{
key: "mock_1",
title: "商品名称",
dataIndex: "name",
component: "text",
options: {
mode: "single",
maxRow: 1,
},
},
{
key: "mock_2",
title: "商品详情",
align: "center",
dataIndex: "description",
component: "text",
options: {
mode: "single",
ellipsis: true,
maxRow: 1,
},
},
{
key: "mock_3",
title: "Custom Component",
align: "center",
dataIndex: "description",
component: "custom::Sample",
options: {
onClick: (() => {
const onClick = (props) => {
return () => {
message.info(`触发CLICK事件,注入参数:${JSON.stringify(props)}`);
console.log('ON CLICK! props injected: ', props);
};
};
onClick.bTwiceEvent = true;
return onClick;
})(),
},
},
],
};

const dataSource = [
{
id: 1,
name: "商品一",
price: 7999,
status: "onSale",
description: "商品是为了出售而生产的劳动成果,是人类社会生产力发展到一定历史阶段的产物,是用于交换的劳动产品。",
},
];

const Demo = () => {
return (
<DripTable<{
CustomColumnSchema: CustomColumnSchema;
}>
schema={schema}
dataSource={dataSource}
components={{ custom: customComponents }}
eventPreprocessor={(event, props) => {
if (event.bTwiceEvent) {
return event(props);
}
return event;
}}
/>
);
};

export default Demo;
```
1 change: 1 addition & 0 deletions docs/drip-table/props/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ toc: content
| [emptyText](/drip-table/props/empty-text) | 表格无数据时提示语 | × | [🔗 示例](/drip-table/props/empty-text) |
| [subtableTitle](/drip-table/props/subtable-title) | 子表顶部自定义渲染函数 | × | [🔗 示例](/drip-table/props/subtable-title) |
| [subtableFooter](/drip-table/props/subtable-footer) | 子表底部自定义渲染函数 | × | [🔗 示例](/drip-table/props/subtable-footer) |
| [eventPreprocessor](/drip-table/props/event-preprocessor) | Schema 函数预处理 | × | [🔗 示例](/drip-table/props/event-preprocessor) |
| [rowExpandable](/drip-table/props/row-expandable) | 获取指定行是否可展开 | × | [🔗 示例](/drip-table/props/row-expandable) |
| [expandedRowRender](/drip-table/props/expanded-row-render) | 行展开自定义渲染函数 | × | [🔗 示例](/drip-table/props/expanded-row-render) |
| [rowSelectable](/drip-table/props/row-selectable) | 获取指定行是否可选择 | × | [🔗 示例](/drip-table/props/row-selectable) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ ExtraOptions extends Partial<DripTableExtraOptions> = never,
execute,
safeExecute,
finalizeString,
eventPreprocessor: void 0,
},
)
: () => <div />;
Expand Down
1 change: 1 addition & 0 deletions packages/drip-table/src/layouts/calendar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ExtraOptions extends Partial<DripTableExtraOptions> = never,
execute,
safeExecute,
finalizeString,
eventPreprocessor: tableProps.eventPreprocessor,
};

return (
Expand Down
1 change: 1 addition & 0 deletions packages/drip-table/src/layouts/card/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ExtraOptions extends Partial<DripTableExtraOptions> = never,
execute,
safeExecute,
finalizeString,
eventPreprocessor: tableProps.eventPreprocessor,
};

const mergedColumns = useMemo(() => {
Expand Down
22 changes: 20 additions & 2 deletions packages/drip-table/src/layouts/table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import * as childrenLike from '@/utils/children-like';
import { parseCSS, parseReactCSS, setElementCSS, stringifyCSS } from '@/utils/dom';
import { encodeJSON } from '@/utils/json';
import { indexValue, parseNumber, setValue } from '@/utils/operator';
import { type SandboxEventPreprocess } from '@/utils/sandbox';
import DripTableBuiltInComponents, { type DripTableBuiltInColumnSchema, type DripTableComponentProps } from '@/components/cell-components';
import { preventEvent } from '@/components/cell-components/utils';
import Checkbox from '@/components/react-components/checkbox';
Expand Down Expand Up @@ -222,6 +223,16 @@ const hookColumRender = <
return column;
};

function hookSchemaEventRaiser<T>(schema: T, eventPreprocessor: SandboxEventPreprocess, props: Record<string, unknown>): T {
if (schema && typeof schema === 'object') {
return Object.fromEntries(
Object.entries(schema)
.map(([k, v]) => [k, typeof v === 'function' ? eventPreprocessor(v, props) : hookSchemaEventRaiser(v, eventPreprocessor, props)]),
) as T;
}
return schema;
}

/**
* 根据列 Schema,生成表格单元格渲染函数
* @param tableInfo 表格信息
Expand Down Expand Up @@ -282,6 +293,9 @@ export const columnRenderGenerator = <
if (hiddenTranslator(false, translatorContext)) {
return null;
}
const finalColumnSchema = extraProps.eventPreprocessor
? hookSchemaEventRaiser(columnSchema, extraProps.eventPreprocessor, { value, record, recordIndex, ext })
: columnSchema;
return (
<BuiltInComponent
data={record}
Expand All @@ -304,7 +318,7 @@ export const columnRenderGenerator = <
disable={Boolean(disableTranslator(false, translatorContext))}
editable={Boolean(editableTranslator(tableInfo.schema.editable, translatorContext))}
onChange={v => onChange(record, recordIndex, v)}
schema={columnSchema as unknown as DripTableBuiltInColumnSchema<ExtractDripTableExtraOption<ExtraOptions, 'CustomColumnSchema'>>}
schema={finalColumnSchema as unknown as DripTableBuiltInColumnSchema<ExtractDripTableExtraOption<ExtraOptions, 'CustomColumnSchema'>>}
ext={extraProps.ext}
components={extraProps.components as DripTableProps<DripTableRecordTypeWithSubtable<DripTableRecordTypeBase, NonNullable<React.Key>>, DripTableExtraOptions>['components']}
icons={extraProps.icons}
Expand All @@ -331,6 +345,9 @@ export const columnRenderGenerator = <
if (hiddenTranslator(false, translatorContext)) {
return null;
}
const finalColumnSchema = extraProps.eventPreprocessor
? hookSchemaEventRaiser(columnSchema, extraProps.eventPreprocessor, { value, record, recordIndex, ext })
: columnSchema;
return (
<ExtraComponent
data={record}
Expand All @@ -353,7 +370,7 @@ export const columnRenderGenerator = <
disable={Boolean(disableTranslator(false, translatorContext))}
editable={Boolean(editableTranslator(tableInfo.schema.editable, translatorContext))}
onChange={v => onChange(record, recordIndex, v)}
schema={columnSchema as ExtractDripTableExtraOption<ExtraOptions, 'CustomColumnSchema'>}
schema={finalColumnSchema as ExtractDripTableExtraOption<ExtraOptions, 'CustomColumnSchema'>}
ext={extraProps.ext}
components={extraProps.components as DripTableProps<DripTableRecordTypeWithSubtable<DripTableRecordTypeBase, NonNullable<React.Key>>, DripTableExtraOptions>['components']}
icons={extraProps.icons}
Expand Down Expand Up @@ -1256,6 +1273,7 @@ function TableLayout<
execute,
safeExecute,
finalizeString,
eventPreprocessor: tableProps.eventPreprocessor,
};
const visibleColumns = childrenLike.filterRecursive(
tableProps.schema.columns,
Expand Down
1 change: 1 addition & 0 deletions packages/drip-table/src/layouts/table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ export interface DripTableColumnRenderOptions<
execute: SandboxExecute;
safeExecute: SandboxSafeExecute;
finalizeString: FinalizeString;
eventPreprocessor: DripTableProps<RecordType, ExtraOptions>['eventPreprocessor'];
};
}
6 changes: 5 additions & 1 deletion packages/drip-table/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { SchemaObject } from 'ajv';
import type React from 'react';

import type { AjvOptions } from '@/utils/ajv';
import type { SandboxCreateExecutor } from '@/utils/sandbox';
import type { SandboxCreateExecutor, SandboxEventPreprocess } from '@/utils/sandbox';
import type { DripTableBuiltInColumnSchema, DripTableBuiltInComponentEvent, DripTableComponentProps } from '@/components/cell-components';
import type { PaginationProps } from '@/components/react-components/pagination';
import type { DripTableSlotSchema } from '@/components/react-components/slot-render';
Expand Down Expand Up @@ -891,6 +891,10 @@ export interface DripTableProps<
* 自定义沙箱生成器
*/
createExecutor?: SandboxCreateExecutor;
/**
* 新版事件还原器
*/
eventPreprocessor?: SandboxEventPreprocess;
/**
* 渲染子表时用于透传父级信息,仅限内部使用
* @internal
Expand Down
1 change: 1 addition & 0 deletions packages/drip-table/src/utils/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ const getDripTablePropsAjvSchema = (options?: AjvOptions) => {
emptyText: { instanceof: 'Function' },
subtableTitle: { instanceof: 'Function' },
subtableFooter: { instanceof: 'Function' },
eventPreprocessor: { instanceof: 'Function' },
rowExpandable: { instanceof: 'Function' },
expandedRowRender: { instanceof: 'Function' },
rowHeaderVisible: { instanceof: 'Function' },
Expand Down
6 changes: 6 additions & 0 deletions packages/drip-table/src/utils/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ export type SandboxSafeExecute =
* @returns 代码段返回结果,异常时返回默认结果
*/
<T = unknown>(script: string, context: Record<string, unknown>, defaultValue?: T) => T | undefined;

export type SandboxEventPreprocess =
/**
* Schema 中函数通用预处理
*/
(event: (...args: unknown[]) => void, props: Record<string, unknown>) => (() => void);

0 comments on commit 79f9765

Please sign in to comment.