UNPKG

11.6 kBTypeScriptView Raw
1/// <reference types="react" />
2
3import * as React from 'react';
4import CommonProps from '../util';
5import { LoadingProps } from '../loading';
6import { AffixProps } from '../affix';
7
8interface HTMLAttributesWeak extends React.HTMLAttributes<HTMLElement> {
9 title?: any;
10}
11
12export interface ColumnProps extends HTMLAttributesWeak, CommonProps {
13 /**
14 * 指定列对应的字段,支持`a.b`形式的快速取值
15 */
16 dataIndex?: string;
17
18 /**
19 * 行渲染的逻辑
20 * value, rowIndex, record, context四个属性只可读不可被更改
21 * Function(value, index, record) => Element
22 */
23 cell?: React.ReactElement<any> | React.ReactNode | ((value: any, index: number, record: any) => any);
24
25 /**
26 * 表头显示的内容
27 * value, rowIndex, record, context四个属性只可读不可被更改
28 */
29 title?: React.ReactElement<any> | React.ReactNode | (() => any);
30
31 htmlTitle?: string;
32 /**
33 * 是否支持排序
34 */
35 sortable?: boolean;
36
37 /**
38 * 列宽,注意在锁列的情况下一定需要配置宽度
39 */
40 width?: number | string;
41
42 /**
43 * 单元格的对齐方式
44 */
45 align?: 'left' | 'center' | 'right';
46 sortDirections?: Array<'desc' | 'asc' | 'default'>;
47 /**
48 * 单元格标题的对齐方式, 不配置默认读取align
49 */
50 alignHeader?: 'left' | 'center' | 'right';
51
52 /**
53 * 生成标题过滤的菜单, 格式为`[{label:'xxx', value:'xxx'}]`
54 */
55 filters?: Array<any>;
56
57 /**
58 * 过滤的模式是单选还是多选
59 */
60 filterMode?: 'single' | 'multiple';
61
62 /**
63 * 是否支持锁列,可选值为`left`,`right`, `true`
64 */
65 lock?: boolean | string;
66
67 /**
68 * 是否支持列宽调整, 当该值设为truetable的布局方式会修改为fixed.
69 */
70 resizable?: boolean;
71 /**
72 * 是否支持异步列宽调整, 当该值设为truetable的布局方式会修改为fixed.
73 */
74 asyncResizable?: boolean;
75
76 /**
77 * header cell 横跨的格数,设置为0表示不出现此 th
78 */
79 colSpan?: number;
80
81 /**
82 * 设置该列单元格的word-break样式,对于id类、中文类适合用all,对于英文句子适合用word
83 */
84 wordBreak?: 'all' | 'word';
85}
86
87export class Column extends React.Component<ColumnProps, any> {}
88
89interface HTMLAttributesWeak extends React.HTMLAttributes<HTMLElement> {
90 title?: any;
91}
92
93export interface ColumnGroupProps extends HTMLAttributesWeak, CommonProps {
94 /**
95 * 表头显示的内容
96 */
97 title?: React.ReactElement<any> | React.ReactNode | (() => any);
98}
99
100export class ColumnGroup extends React.Component<ColumnGroupProps, any> {}
101
102export interface GroupHeaderProps extends React.HTMLAttributes<HTMLElement>, CommonProps {
103 /**
104 * 行渲染的逻辑
105 */
106 cell?: React.ReactElement<any> | React.ReactNode | ((value: any, index: number, record: any) => any);
107
108 /**
109 * 是否在Children上面渲染selection
110 */
111 hasChildrenSelection?: boolean;
112
113 /**
114 * 是否在GroupHeader上面渲染selection
115 */
116 hasSelection?: boolean;
117
118 /**
119 * 当 dataSouce 里没有 children 时,是否使用内容作为数据
120 */
121 useFirstLevelDataWhenNoChildren?: boolean;
122}
123
124export class GroupHeader extends React.Component<GroupHeaderProps, any> {}
125
126export interface GroupFooterProps extends React.HTMLAttributes<HTMLElement>, CommonProps {
127 /**
128 * 行渲染的逻辑
129 */
130 cell?: React.ReactElement<any> | React.ReactNode | ((value: any, index: number, record: any) => any);
131}
132
133export class GroupFooter extends React.Component<GroupFooterProps, any> {}
134
135export interface BaseTableProps extends React.HTMLAttributes<HTMLElement>, CommonProps {
136/**
137 * 样式类名的品牌前缀
138 */
139 prefix?: string;
140
141 /**
142 * 尺寸 small为紧凑模式
143 */
144 size?: 'small' | 'medium';
145
146 /**
147 * 自定义类名
148 */
149 className?: string;
150
151 /**
152 * 自定义内联样式
153 */
154 style?: React.CSSProperties;
155 columns?: Array<any>;
156 /**
157 * 表格元素的 table-layout 属性,设为 fixed 表示内容不会影响列的布局
158 */
159 tableLayout?: 'fixed' | 'auto';
160 /**
161 * 表格的总长度,可以这么用:设置表格总长度 、设置部分列的宽度,这样表格会按照剩余空间大小,自动其他列分配宽度
162 */
163 tableWidth?: number;
164 /**
165 * 表格展示的数据源
166 */
167 dataSource?: Array<any>;
168
169 /**
170 * 表格是否具有边框
171 */
172 hasBorder?: boolean;
173
174 /**
175 * 表格是否具有头部
176 */
177 hasHeader?: boolean;
178
179 /**
180 * 表格是否是斑马线
181 */
182 isZebra?: boolean;
183
184 /**
185 * 表格是否在加载中
186 */
187 loading?: boolean;
188
189 /**
190 * 自定义国际化文案对象
191 */
192 locale?: {
193 ok: string;
194 reset: string;
195 empty: string;
196 asc: string;
197 desc: string;
198 expanded: string;
199 folded: string;
200 filter: string;
201 selectAll: string;
202 };
203
204 /**
205 * 设置数据为空的时候的表格内容展现
206 */
207 emptyContent?: React.ReactNode;
208
209 /**
210 * dataSource当中数据的主键,如果给定的数据源中的属性不包含该主键,会造成选择状态全部选中
211 */
212 primaryKey?: string;
213}
214export interface TableProps extends React.HTMLAttributes<HTMLElement>, BaseTableProps {
215
216 /**
217 * 点击表格每一行触发的事件
218 */
219 onRowClick?: (record: any, index: number, e: React.MouseEvent) => void;
220
221 /**
222 * 悬浮在表格每一行的时候触发的事件
223 */
224 onRowMouseEnter?: (record: any, index: number, e: React.MouseEvent) => void;
225
226 /**
227 * 离开表格每一行的时候触发的事件
228 */
229 onRowMouseLeave?: (record: any, index: number, e: React.MouseEvent) => void;
230
231 /**
232 * 点击列排序触发的事件
233 */
234 onSort?: (dataIndex: string, order: string) => void;
235
236 /**
237 * 点击过滤确认按钮触发的事件
238 */
239 onFilter?: (filterParams: any) => void;
240
241 /**
242 * 重设列尺寸的时候触发的事件
243 */
244 onResizeChange?: (dataIndex: string, value: number) => void;
245
246 /**
247 * 设置每一行的属性,如果返回值和其他针对行操作的属性冲突则无效。
248 */
249 getRowProps?: (record: any, index: number) => any;
250 rowProps?: (record: any, index: number) => any;
251
252 /**
253 * 设置单元格的属性,通过该属性可以进行合并单元格
254 */
255 getCellProps?: (
256 rowIndex: number,
257 colIndex: number,
258 dataIndex: string,
259 record: any
260 ) => any;
261 cellProps?: (
262 rowIndex: number,
263 colIndex: number,
264 dataIndex: string,
265 record: any
266 ) => any;
267
268 /**
269 * 自定义 Loading 组件
270 * 请务必传递 props, 使用方式: loadingComponent={props => <Loading {...props}/>}
271 */
272 loadingComponent?: (props: LoadingProps) => React.ReactNode;
273
274 /**
275 * 当前过滤的的keys,使用此属性可以控制表格的头部的过滤选项中哪个菜单被选中,格式为 {dataIndex: {selectedKeys:[]}}
276 * 示例:
277 * 假设要控制dataIndex为id的列的过滤菜单中key为one的菜单项选中
278 * `<Table filterParams={{id: {selectedKeys: ['one']}}}/>`
279 */
280 filterParams?: {[propName: string]: any};
281
282 /**
283 * 当前排序的字段,使用此属性可以控制表格的字段的排序,格式为{dataIndex: 'asc'}
284 */
285 sort?: {[propName: string]: any};
286
287 /**
288 * 自定义排序按钮,例如上下排布的: `{desc: <Icon style={{top: '6px', left: '4px'}} type={'arrow-down'} size="small" />, asc: <Icon style={{top: '-6px', left: '4px'}} type={'arrow-up'} size="small" />}`
289 */
290 sortIcons?: {desc?: React.ReactNode; asc?: React.ReactNode};
291
292 /**
293 * 额外渲染行的渲染函数
294 */
295 expandedRowRender?: (record: any, index: number) => React.ReactElement<any>;
296 rowExpandable?: (record: any) => boolean;
297
298 /**
299 * 额外渲染行的缩进,包含两个数字,第一个数字为左侧缩进,第二个数字为右侧缩进
300 */
301 expandedRowIndent?: [number, number];
302
303 /**
304 * 默认情况下展开的渲染行或者Tree, 传入此属性为受控状态
305 */
306 openRowKeys?: Array<any>;
307
308 /**
309 * 是否显示点击展开额外渲染行的+号按钮
310 */
311 hasExpandedRowCtrl?: boolean;
312
313 /**
314 * 设置额外渲染行的属性
315 */
316 getExpandedColProps?: <IRecord extends any = any>(record: IRecord, index: number) => object | Record<string | number, any>;
317
318 /**
319 * 在额外渲染行或者Tree展开或者收起的时候触发的事件
320 */
321 onRowOpen?: (
322 openRowKeys: Array<any>,
323 currentRowKey: string,
324 expanded: boolean,
325 currentRecord:any
326 ) => void;
327
328 /**
329 * 点击额外渲染行触发的事件
330 */
331 onExpandedRowClick?: (record: any, index: number, e: React.MouseEvent) => void;
332
333 /**
334 * 表头是否固定,该属性配合maxBodyHeight使用,当内容区域的高度超过maxBodyHeight的时候,在内容区域会出现滚动条
335 */
336 fixedHeader?: boolean;
337
338 /**
339 * 最大内容区域的高度,在`fixedHeader`为`true`的时候,超过这个高度会出现滚动条
340 */
341 maxBodyHeight?: number | string;
342
343 /**
344 * 是否启用选择模式
345 */
346 rowSelection?: {
347 getProps?: (record: any, index: number) => void;
348 onChange?: (selectedRowKeys: Array<any>, records: Array<any>) => void;
349 onSelect?: (
350 selected: boolean,
351 record: any,
352 records: Array<any>
353 ) => void;
354 onSelectAll?: (selected: boolean, records: Array<any>) => void;
355 selectedRowKeys?: Array<any>;
356 mode?: 'single' | 'multiple';
357 titleProps?: () => any;
358 columnProps?: () => any;
359 titleAddons?: () => any;
360 };
361
362 /**
363 * 表头是否是sticky
364 */
365 stickyHeader?: boolean;
366
367 /**
368 * 距离窗口顶部达到指定偏移量后触发
369 */
370 offsetTop?: number;
371
372 /**
373 * affix组件的的属性
374 */
375 affixProps?: AffixProps;
376
377 /**
378 * 在tree模式下的缩进尺寸, 仅在isTree为true时候有效
379 */
380 indent?: number;
381
382 /**
383 * 开启Table的tree模式, 接收的数据格式中包含children则渲染成tree table
384 */
385 isTree?: boolean;
386
387 /**
388 * 是否开启虚拟滚动
389 */
390 useVirtual?: boolean;
391
392 /**
393 * 滚动到指定行
394 */
395 scrollToRow?: number;
396
397 /**
398 * 设置行高
399 */
400 rowHeight?: number | (() => any);
401
402 /**
403 * 在内容区域滚动的时候触发的函数
404 */
405 onBodyScroll?: (start: number) => void;
406
407 /**
408 * 开启时,getExpandedColProps() / getRowProps() / expandedRowRender() 的第二个参数 index (该行所对应的序列) 将按照01,2,3,4...的顺序返回,否则返回真实index(0,2,4,6... / 1,3,5,7...)
409 */
410 expandedIndexSimulate?: boolean;
411 /**
412 * 在 hover 时出现十字参考轴,适用于表头比较复杂,需要做表头分类的场景。
413 */
414 crossline?: boolean;
415}
416
417export default class Table extends React.Component<TableProps, any> {
418 static Column: typeof Column;
419 static ColumnGroup: typeof ColumnGroup;
420 static GroupHeader: typeof GroupHeader;
421 static GroupFooter: typeof GroupFooter;
422 static StickyLock: typeof Table;
423 static Base: typeof Table;
424}