You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/assets/guide/en/cell_type/cellType.md
+69Lines changed: 69 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -253,3 +253,72 @@ The following shows an example of `cellType: ()=>{}`: (Please refer to [Example]
253
253
keepAspectRatio:true,
254
254
}
255
255
```
256
+
257
+
## Extension: Custom Column Types (TypeScript)
258
+
259
+
In real-world applications, in addition to VTable’s built-in `cellType` values (such as `text`, `link`, `image`, `video`, `progressbar`, `sparkline`, and `chart`), it is often necessary to attach **business-specific configurations** to column definitions, for example:
260
+
261
+
- Permission or visibility control: `permissionKey`
262
+
- Tracking and analytics metadata: `trackId` / `trackParams`
- DSL fields used by higher-level wrapper components: `renderAs` / `variant`
265
+
266
+
These fields are usually **not consumed directly by VTable itself**. Instead, they are read and handled by business-layer abstractions, such as a unified `cellRender`, custom hooks, or column factory utilities.
267
+
268
+
To support this use case, VTable provides a **type-level extension mechanism** that allows developers to extend `ColumnDefine` via TypeScript _module augmentation_. This enables defining custom column types with full type inference, validation, and IDE autocomplete support.
269
+
270
+
### 1) Define a custom column type
271
+
272
+
```ts
273
+
import type { HeaderDefine } from '@visactor/vtable';
274
+
275
+
// Custom column body definition (example: a customized button column)
276
+
export interface IColoredButtonColumnBody {
277
+
cellType: 'button';
278
+
field: string;
279
+
280
+
/** Business-specific extension field: highlight when the value is negative */
281
+
highlightOnNegative?: boolean;
282
+
283
+
text?: string;
284
+
style?: any;
285
+
}
286
+
287
+
// Full column definition (Header + Body)
288
+
export type ColoredButtonColumnDefine = IColoredButtonColumnBody & HeaderDefine;
289
+
```
290
+
291
+
### 2) Register the type via module augmentation
292
+
293
+
```ts
294
+
declare module '@visactor/vtable/es/ts-types' {
295
+
interface CustomColumnBodyDefineMap {
296
+
coloredButton: IColoredButtonColumnBody;
297
+
}
298
+
299
+
interface CustomColumnDefineMap {
300
+
coloredButtonColumn: ColoredButtonColumnDefine;
301
+
}
302
+
}
303
+
```
304
+
305
+
### 3) Use the custom column type in `columns`
306
+
307
+
```ts
308
+
import type { ColumnsDefine } from '@visactor/vtable';
309
+
310
+
const columns: ColumnsDefine = [
311
+
{
312
+
field: 'value',
313
+
title: 'Custom Button Column',
314
+
cellType: 'button',
315
+
text: 'check',
316
+
highlightOnNegative: true // ✅ extended field from CustomColumnDefineMap
317
+
}
318
+
];
319
+
```
320
+
321
+
> **Note:**
322
+
> The example above demonstrates a **type-level extension** only.
323
+
> Whether fields such as `highlightOnNegative` produce actual visual or behavioral effects depends on business-side rendering logic (e.g., custom `cellRender`, hooks, or column factories).
324
+
> VTable itself does not interpret or handle these fields automatically.
0 commit comments