interface.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import React, { ReactNode, MutableRefObject } from 'react';
  2. import type { BaseProps } from '../_base/baseComponent';
  3. import type { PaginationProps } from '../pagination';
  4. import type { CheckboxProps } from '../checkbox';
  5. import type { Locale } from '../locale/interface';
  6. import type { ArrayElement } from '../_base/base';
  7. import { strings } from '@douyinfe/semi-foundation/table/constants';
  8. import type {
  9. BaseRowKeyType,
  10. BaseSortOrder,
  11. BaseGroupBy,
  12. BaseGroupByFn,
  13. BaseFixed,
  14. BaseAlign,
  15. BaseChangeInfoSorter,
  16. BaseSorter,
  17. BaseFilter,
  18. BaseChangeInfoFilter,
  19. BaseIncludeGroupRecord,
  20. BaseEllipsis
  21. } from '@douyinfe/semi-foundation/table/foundation';
  22. import type { ColumnFilterProps } from './ColumnFilter';
  23. import { TableCellProps } from './TableCell';
  24. export interface TableProps<RecordType extends Record<string, any> = any> extends BaseProps {
  25. bordered?: boolean;
  26. children?: ReactNode;
  27. childrenRecordName?: string;
  28. className?: string;
  29. clickGroupedRowToExpand?: boolean;
  30. columns?: ColumnProps<RecordType>[];
  31. components?: TableComponents;
  32. dataSource?: RecordType[];
  33. defaultExpandAllGroupRows?: boolean;
  34. defaultExpandAllRows?: boolean;
  35. defaultExpandedRowKeys?: (string | number)[];
  36. empty?: ReactNode;
  37. expandAllGroupRows?: boolean;
  38. expandAllRows?: boolean;
  39. expandCellFixed?: Fixed;
  40. expandIcon?: ExpandIcon;
  41. expandedRowKeys?: (string | number)[];
  42. expandedRowRender?: ExpandedRowRender<RecordType>;
  43. expandRowByClick?: boolean;
  44. footer?: Footer<RecordType>;
  45. getVirtualizedListRef?: GetVirtualizedListRef;
  46. groupBy?: GroupBy<RecordType>;
  47. hideExpandedColumn?: boolean;
  48. id?: string;
  49. indentSize?: number;
  50. keepDOM?: boolean;
  51. loading?: boolean;
  52. pagination?: TablePagination;
  53. prefixCls?: string;
  54. renderGroupSection?: RenderGroupSection;
  55. renderPagination?: RenderPagination;
  56. resizable?: Resizable<RecordType>;
  57. rowExpandable?: RowExpandable<RecordType>;
  58. rowKey?: RowKey<RecordType>;
  59. rowSelection?: RowSelection<RecordType>;
  60. scroll?: Scroll;
  61. showHeader?: boolean;
  62. size?: Size;
  63. style?: React.CSSProperties;
  64. title?: Title<RecordType>;
  65. virtualized?: Virtualized;
  66. onChange?: OnChange<RecordType>;
  67. onExpand?: OnExpand<RecordType>;
  68. onExpandedRowsChange?: OnExpandedRowsChange<RecordType>;
  69. onGroupedRow?: OnGroupedRow<RecordType>;
  70. onHeaderRow?: OnHeaderRow<RecordType>;
  71. onRow?: OnRow<RecordType>;
  72. sticky?: Sticky;
  73. direction?: Direction
  74. }
  75. export interface ColumnProps<RecordType extends Record<string, any> = any> {
  76. [x: string]: any;
  77. align?: Align;
  78. children?: Array<ColumnProps<RecordType>>;
  79. className?: string;
  80. colSpan?: number;
  81. /** use `dataIndex` to get current column data item from record. If you use `sorter` or `onFilter`, a unique `dataIndex` is required */
  82. dataIndex?: string;
  83. defaultFilteredValue?: any[];
  84. defaultSortOrder?: SortOrder;
  85. filterChildrenRecord?: boolean;
  86. filterDropdown?: ColumnFilterProps['filterDropdown'];
  87. /** render filter Dropdown panel content */
  88. renderFilterDropdown?: ColumnFilterProps['renderFilterDropdown'];
  89. /** filter Dropdown props */
  90. filterDropdownProps?: ColumnFilterProps['filterDropdownProps'];
  91. filterDropdownVisible?: boolean;
  92. filterIcon?: FilterIcon;
  93. filterMultiple?: boolean;
  94. filteredValue?: any[];
  95. /** `filters` is not required if you use `renderFilterDropdown` */
  96. filters?: Filter[];
  97. fixed?: Fixed;
  98. /** the key required by React. If you have already set the `dataIndex`, the key does not need to be set again. */
  99. key?: string | number;
  100. render?: ColumnRender<RecordType>;
  101. renderFilterDropdownItem?: RenderFilterDropdownItem;
  102. sortChildrenRecord?: boolean;
  103. sortOrder?: SortOrder;
  104. /** enable sorting, `dataIndex` is required at the same time */
  105. sorter?: Sorter<RecordType>;
  106. sortIcon?: SortIcon;
  107. title?: ColumnTitle;
  108. useFullRender?: boolean;
  109. width?: string | number;
  110. onCell?: OnCell<RecordType>;
  111. /** enable filtering, `dataIndex` is required at the same time */
  112. onFilter?: OnFilter<RecordType>;
  113. onFilterDropdownVisibleChange?: OnFilterDropdownVisibleChange;
  114. onHeaderCell?: OnHeaderCell<RecordType>;
  115. ellipsis?: BaseEllipsis;
  116. resize?: boolean;
  117. showSortTip?: boolean;
  118. /**
  119. * self control whether to update cell for performance reasons
  120. */
  121. shouldCellUpdate?: (props: TableCellProps, prevProps: TableCellProps) => boolean
  122. }
  123. export type Align = BaseAlign;
  124. export type SortOrder = BaseSortOrder;
  125. export type SortIcon = (props: { sortOrder: SortOrder }) => ReactNode;
  126. export type FilterIcon = boolean | React.ReactNode | FilterIconRenderFunction;
  127. export interface Filter extends BaseFilter {
  128. value?: any;
  129. text?: React.ReactNode;
  130. children?: Filter[]
  131. }
  132. export type Fixed = BaseFixed;
  133. export type OnCell<RecordType> = (record?: RecordType, rowIndex?: number) => OnCellReturnObject;
  134. export type OnFilter<RecordType> = (filteredValue?: any, record?: RecordType) => boolean;
  135. export type OnFilterDropdownVisibleChange = (visible?: boolean) => void;
  136. export type OnHeaderCell<RecordType> = (record?: RecordType, columnIndex?: number, index?: number) => OnHeaderCellReturnObject;
  137. export type ColumnRender<RecordType> = (text: any, record: RecordType, index: number, options?: RenderOptions) => ColumnRenderReturnType;
  138. export type RenderFilterDropdownItem = (itemInfo?: FilterDropdownItem) => ReactNode;
  139. export type Sorter<RecordType> = BaseSorter<RecordType>;
  140. export type ColumnTitle = React.ReactNode | ((ColumnTitleProps?: ColumnTitleProps) => React.ReactNode);
  141. export type FilterIconRenderFunction = (filtered: boolean) => React.ReactNode;
  142. export type ColumnTitleProps = {
  143. sorter?: React.ReactNode;
  144. filter?: React.ReactNode;
  145. selection?: React.ReactNode
  146. };
  147. export type ColumnRenderReturnType = React.ReactNode | RenderReturnObject;
  148. export interface RenderReturnObject {
  149. [x: string]: any;
  150. children: React.ReactNode;
  151. props: {
  152. [x: string]: any;
  153. colSpan?: number;
  154. rowSpan?: number
  155. }
  156. }
  157. export interface FilterDropdownItem {
  158. [x: string]: any;
  159. value?: any;
  160. text?: React.ReactNode;
  161. onChange?: React.MouseEventHandler<HTMLLIElement>;
  162. level?: number;
  163. filterMultiple?: boolean;
  164. checked?: boolean
  165. }
  166. export interface RenderOptions {
  167. expandIcon?: React.ReactNode;
  168. selection?: React.ReactNode;
  169. indentText?: React.ReactNode
  170. }
  171. export interface OnCellReturnObject extends React.TdHTMLAttributes<HTMLElement> {
  172. [x: string]: any;
  173. style?: React.CSSProperties;
  174. className?: string;
  175. onClick?: (e: React.MouseEvent) => void
  176. }
  177. export interface OnHeaderCellReturnObject extends React.ThHTMLAttributes<HTMLElement> {
  178. [x: string]: any;
  179. style?: React.CSSProperties;
  180. className?: string;
  181. onClick?: (e: React.MouseEvent) => void
  182. }
  183. interface OnRowReturnOmit {
  184. ref?: React.RefObject<any>
  185. }
  186. export interface OnRowReturnObject extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLTableRowElement>, HTMLTableRowElement>, keyof OnRowReturnOmit> {
  187. [x: string]: any;
  188. className?: string;
  189. style?: React.CSSProperties;
  190. onClick?: (e: React.MouseEvent) => void
  191. }
  192. export interface OnGroupedRowReturnObject extends React.HTMLAttributes<HTMLTableRowElement> {
  193. [x: string]: any;
  194. style?: React.CSSProperties;
  195. onClick?: (e: React.MouseEvent) => void
  196. }
  197. export type OnHeaderRowReturnObject = Omit<React.HTMLAttributes<HTMLTableRowElement>, 'ref' | 'style'>;
  198. export interface Scroll {
  199. x?: number | string;
  200. y?: number | string;
  201. scrollToFirstRowOnChange?: boolean
  202. }
  203. export interface Data {
  204. [x: string]: any;
  205. key?: string | number
  206. }
  207. export type TableComponent<P> = React.ComponentType<P> | React.ForwardRefExoticComponent<P> | keyof React.ReactHTML;
  208. export interface TableComponents {
  209. table?: TableComponent<any>;
  210. header?: {
  211. outer?: TableComponent<any>;
  212. wrapper?: TableComponent<any>;
  213. row?: TableComponent<any>;
  214. cell?: TableComponent<any>
  215. };
  216. body?: {
  217. outer?: TableComponent<any>;
  218. wrapper?: TableComponent<any>;
  219. row?: TableComponent<any>;
  220. cell?: TableComponent<any>;
  221. colgroup?: {
  222. wrapper?: TableComponent<any>;
  223. col?: TableComponent<any>
  224. }
  225. };
  226. footer?: {
  227. wrapper?: TableComponent<any>;
  228. row?: TableComponent<any>;
  229. cell?: TableComponent<any>;
  230. outer?: TableComponent<any>
  231. }
  232. }
  233. export interface RowSelectionProps<RecordType> {
  234. className?: string;
  235. disabled?: boolean;
  236. fixed?: Fixed;
  237. getCheckboxProps?: GetCheckboxProps<RecordType>;
  238. hidden?: boolean;
  239. selectedRowKeys?: (string | number)[];
  240. title?: ReactNode;
  241. width?: string | number;
  242. onChange?: RowSelectionOnChange<RecordType>;
  243. onSelect?: RowSelectionOnSelect<RecordType>;
  244. onSelectAll?: RowSelectionOnSelectAll<RecordType>;
  245. onCell?: ColumnProps['onCell'];
  246. onHeaderCell?: ColumnProps['onHeaderCell'];
  247. renderCell?: RowSelectionRenderCell<RecordType>;
  248. /**
  249. * self control whether to update cell for performance reasons
  250. */
  251. shouldCellUpdate?: (props: TableCellProps, prevProps: TableCellProps) => boolean
  252. }
  253. export type RowSelectionRenderCell<RecordType> = (renderCellArgs: {
  254. selected: boolean;
  255. record: RecordType;
  256. originNode: JSX.Element;
  257. inHeader: boolean;
  258. disabled: boolean;
  259. indeterminate: boolean;
  260. index?: number;
  261. selectRow?: (selected: boolean, e: Event) => void;
  262. selectAll?: (selected: boolean, e: Event) => void
  263. }) => ReactNode;
  264. export type GetCheckboxProps<RecordType> = (record: RecordType) => Omit<CheckboxProps, 'defaultChecked' | 'checked' | 'indeterminate' | 'onChange'>;
  265. export type RowSelectionOnChange<RecordType> = (selectedRowKeys?: (string | number)[], selectedRows?: RecordType[]) => void;
  266. export type RowSelectionOnSelect<RecordType> = (
  267. record?: RecordType,
  268. selected?: boolean,
  269. selectedRows?: RecordType[],
  270. nativeEvent?: React.MouseEvent
  271. ) => void;
  272. export type RowSelectionOnSelectAll<RecordType> = (selected?: boolean, selectedRows?: RecordType[], changedRows?: RecordType[]) => void;
  273. export type ExpandIcon = ((expanded?: boolean) => React.ReactNode) | React.ReactNode;
  274. export type ExpandedRowRender<RecordType> = (record?: RecordType, index?: number, expanded?: boolean) => React.ReactNode;
  275. export type Footer<RecordType> = ReactNode | ((pageData?: RecordType[]) => React.ReactNode);
  276. export type FormatPageText = ((pageInfo?: { currentStart?: number; currentEnd?: number; total?: number }) => React.ReactNode) | boolean;
  277. /**
  278. * ref to react-window `VariableSizeList` instance
  279. *
  280. * only work when `virtualized` is truthy
  281. *
  282. * @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/58aabc0cfd2baf08f5f71a2712ae7baa6cb2a3ce/types/react-window/index.d.ts#L378
  283. */
  284. export type GetVirtualizedListRef = (ref: MutableRefObject<{
  285. /**
  286. * Scroll to the specified offset (scrollTop or scrollLeft, depending on the direction prop).
  287. */
  288. scrollTo(scrollOffset: number): void;
  289. /**
  290. * Scroll to the specified item.
  291. *
  292. * By default, the List will scroll as little as possible to ensure the item is visible.
  293. * You can control the alignment of the item though by specifying a second alignment parameter. Acceptable values are:
  294. *
  295. * - auto (default) - Scroll as little as possible to ensure the item is visible. (If the item is already visible, it won't scroll at all.)
  296. * - smart
  297. * - If the item is already visible, don't scroll at all.
  298. * - If it is less than one viewport away, scroll as little as possible so that it becomes visible.
  299. * - If it is more than one viewport away, scroll so that it is centered within the list.
  300. * - center - Center align the item within the list.
  301. * - end - Align the item to the end of the list (the bottom for vertical lists or the right for horizontal lists).
  302. * - start - Align the item to the beginning of the list (the top for vertical lists or the left for horizontal lists).
  303. */
  304. scrollToItem(index: number, align?: "auto" | "smart" | "center" | "end" | "start"): void
  305. }>) => void;
  306. export type GroupByFunction<RecordType> = BaseGroupByFn<RecordType>;
  307. export type GroupBy<RecordType> = BaseGroupBy<RecordType>;
  308. export type Size = ArrayElement<typeof strings.SIZES>;
  309. export type Title<RecordType> = React.ReactNode | ((pageData?: RecordType[]) => React.ReactNode);
  310. export type PaginationPosition = ArrayElement<typeof strings.PAGINATION_POSITIONS>;
  311. export type Pagination = TablePaginationProps | boolean;
  312. export type TablePagination = Pagination;
  313. export interface ChangeInfoFilter<RecordType> extends BaseChangeInfoFilter<RecordType> {
  314. filters?: Filter[];
  315. onFilter?: OnFilter<RecordType>
  316. }
  317. export type ChangeInfoSorter<RecordType> = BaseChangeInfoSorter<RecordType>;
  318. export interface ChangeInfo<RecordType> {
  319. pagination?: TablePaginationProps;
  320. filters?: ChangeInfoFilter<RecordType>[];
  321. sorter?: ChangeInfoSorter<RecordType>;
  322. extra?: Record<string, any>
  323. }
  324. export type OnChange<RecordType> = (changeInfo: ChangeInfo<RecordType>) => void;
  325. export type OnRow<RecordType> = (record?: RecordType, index?: number) => OnRowReturnObject;
  326. export type OnGroupedRow<RecordType> = (record?: RecordType, index?: number) => OnGroupedRowReturnObject;
  327. export type OnHeaderRow<RecordType> = (columns?: ColumnProps<RecordType>[], index?: number) => OnHeaderRowReturnObject;
  328. export type OnExpandedRowsChange<RecordType> = (expandedRows?: IncludeGroupRecord<RecordType>[]) => void;
  329. export type OnExpand<RecordType> = (expanded?: boolean, record?: IncludeGroupRecord<RecordType>, mouseEvent?: React.MouseEvent) => void;
  330. export type RenderGroupSection = (groupKey?: string | number, group?: (string | number)[]) => ReactNode | {
  331. [x: string]: any;
  332. children: ReactNode
  333. };
  334. export type RenderPagination = (paginationProps: TablePaginationProps) => ReactNode;
  335. export type RowExpandable<RecordType> = (record?: RecordType) => boolean;
  336. export type RowKey<RecordType> = BaseRowKeyType | ((record?: RecordType) => string);
  337. export type RowSelection<RecordType> = RowSelectionProps<RecordType> | boolean;
  338. export type VirtualizedOnScrollArgs = {
  339. scrollDirection?: "forward" | "backward";
  340. scrollOffset?: number;
  341. scrollUpdateWasRequested?: boolean
  342. };
  343. export type VirtualizeItemSizeRow = {
  344. sectionRow?: boolean;
  345. expandedRow?: boolean
  346. };
  347. export type VirtualizedItemSizeFn = (index?: number, row?: VirtualizeItemSizeRow) => number;
  348. export type VirtualizedItemSize = number | VirtualizedItemSizeFn;
  349. export type VirtualizedOnScroll = (object: VirtualizedOnScrollArgs) => void;
  350. export interface VirtualizedProps {
  351. [x: string]: any;
  352. itemSize?: VirtualizedItemSize;
  353. onScroll?: VirtualizedOnScroll
  354. }
  355. export type Virtualized = boolean | VirtualizedProps;
  356. export interface TablePaginationProps extends BaseProps, PaginationProps {
  357. position?: PaginationPosition;
  358. formatPageText?: FormatPageText
  359. }
  360. export type Resizable<RecordType> = ResizableProps<RecordType> | boolean;
  361. export interface ResizableProps<RecordType> {
  362. onResize?: ResizeFn<RecordType>;
  363. onResizeStart?: ResizeFn<RecordType>;
  364. onResizeStop?: ResizeFn<RecordType>
  365. }
  366. export type ResizeFn<RecordType> = (column: RecordType) => RecordType;
  367. export interface BodyScrollEvent extends React.UIEvent {
  368. [x: string]: any;
  369. currentTarget: any;
  370. target: any
  371. }
  372. export type BodyScrollPosition = 'both' | 'middle' | 'left' | 'right';
  373. export type TableLocale = Locale['Table'];
  374. export type Direction = "ltr" | "rtl";
  375. export type IncludeGroupRecord<RecordType> = BaseIncludeGroupRecord<RecordType>;
  376. export type Sticky = boolean | {
  377. top?: number
  378. }