interface.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 { ScrollDirection, CSSDirection, VariableSizeList } from 'react-window';
  23. import type { ColumnFilterProps } from './ColumnFilter';
  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. }
  118. export type Align = BaseAlign;
  119. export type SortOrder = BaseSortOrder;
  120. export type SortIcon = (props: { sortOrder: SortOrder }) => ReactNode;
  121. export type FilterIcon = boolean | React.ReactNode | FilterIconRenderFunction;
  122. export interface Filter extends BaseFilter {
  123. value?: any;
  124. text?: React.ReactNode;
  125. children?: Filter[]
  126. }
  127. export type Fixed = BaseFixed;
  128. export type OnCell<RecordType> = (record?: RecordType, rowIndex?: number) => OnCellReturnObject;
  129. export type OnFilter<RecordType> = (filteredValue?: any, record?: RecordType) => boolean;
  130. export type OnFilterDropdownVisibleChange = (visible?: boolean) => void;
  131. export type OnHeaderCell<RecordType> = (record?: RecordType, columnIndex?: number, index?: number) => OnHeaderCellReturnObject;
  132. export type ColumnRender<RecordType> = (text: any, record: RecordType, index: number, options?: RenderOptions) => ColumnRenderReturnType;
  133. export type RenderFilterDropdownItem = (itemInfo?: FilterDropdownItem) => ReactNode;
  134. export type Sorter<RecordType> = BaseSorter<RecordType>;
  135. export type ColumnTitle = React.ReactNode | ((ColumnTitleProps?: ColumnTitleProps) => React.ReactNode);
  136. export type FilterIconRenderFunction = (filtered: boolean) => React.ReactNode;
  137. export type ColumnTitleProps = {
  138. sorter?: React.ReactNode;
  139. filter?: React.ReactNode;
  140. selection?: React.ReactNode
  141. };
  142. export type ColumnRenderReturnType = React.ReactNode | RenderReturnObject;
  143. export interface RenderReturnObject {
  144. [x: string]: any;
  145. children: React.ReactNode;
  146. props: {
  147. [x: string]: any;
  148. colSpan?: number;
  149. rowSpan?: number
  150. }
  151. }
  152. export interface FilterDropdownItem {
  153. [x: string]: any;
  154. value?: any;
  155. text?: React.ReactNode;
  156. onChange?: React.MouseEventHandler<HTMLLIElement>;
  157. level?: number;
  158. filterMultiple?: boolean;
  159. checked?: boolean
  160. }
  161. export interface RenderOptions {
  162. expandIcon?: React.ReactNode;
  163. selection?: React.ReactNode;
  164. indentText?: React.ReactNode
  165. }
  166. export interface OnCellReturnObject extends React.TdHTMLAttributes<HTMLElement> {
  167. [x: string]: any;
  168. style?: React.CSSProperties;
  169. className?: string;
  170. onClick?: (e: React.MouseEvent) => void
  171. }
  172. export interface OnHeaderCellReturnObject extends React.ThHTMLAttributes<HTMLElement> {
  173. [x: string]: any;
  174. style?: React.CSSProperties;
  175. className?: string;
  176. onClick?: (e: React.MouseEvent) => void
  177. }
  178. interface OnRowReturnOmit {
  179. ref?: React.RefObject<any>
  180. }
  181. export interface OnRowReturnObject extends Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLTableRowElement>, HTMLTableRowElement>, keyof OnRowReturnOmit> {
  182. [x: string]: any;
  183. className?: string;
  184. style?: React.CSSProperties;
  185. onClick?: (e: React.MouseEvent) => void
  186. }
  187. export interface OnGroupedRowReturnObject extends React.HTMLAttributes<HTMLTableRowElement> {
  188. [x: string]: any;
  189. style?: React.CSSProperties;
  190. onClick?: (e: React.MouseEvent) => void
  191. }
  192. export type OnHeaderRowReturnObject = Omit<React.HTMLAttributes<HTMLTableRowElement>, 'ref' | 'style'>;
  193. export interface Scroll {
  194. x?: number | string;
  195. y?: number | string;
  196. scrollToFirstRowOnChange?: boolean
  197. }
  198. export interface Data {
  199. [x: string]: any;
  200. key?: string | number
  201. }
  202. export type TableComponent<P> = React.ComponentType<P> | React.ForwardRefExoticComponent<P> | keyof React.ReactHTML;
  203. export interface TableComponents {
  204. table?: TableComponent<any>;
  205. header?: {
  206. outer?: TableComponent<any>;
  207. wrapper?: TableComponent<any>;
  208. row?: TableComponent<any>;
  209. cell?: TableComponent<any>
  210. };
  211. body?: {
  212. outer?: TableComponent<any>;
  213. wrapper?: TableComponent<any>;
  214. row?: TableComponent<any>;
  215. cell?: TableComponent<any>;
  216. colgroup?: {
  217. wrapper?: TableComponent<any>;
  218. col?: TableComponent<any>
  219. }
  220. };
  221. footer?: {
  222. wrapper?: TableComponent<any>;
  223. row?: TableComponent<any>;
  224. cell?: TableComponent<any>;
  225. outer?: TableComponent<any>
  226. }
  227. }
  228. export interface RowSelectionProps<RecordType> {
  229. className?: string;
  230. disabled?: boolean;
  231. fixed?: Fixed;
  232. getCheckboxProps?: GetCheckboxProps<RecordType>;
  233. hidden?: boolean;
  234. selectedRowKeys?: (string | number)[];
  235. title?: ReactNode;
  236. width?: string | number;
  237. onChange?: RowSelectionOnChange<RecordType>;
  238. onSelect?: RowSelectionOnSelect<RecordType>;
  239. onSelectAll?: RowSelectionOnSelectAll<RecordType>;
  240. onCell?: ColumnProps['onCell'];
  241. onHeaderCell?: ColumnProps['onHeaderCell'];
  242. renderCell?: RowSelectionRenderCell<RecordType>
  243. }
  244. export type RowSelectionRenderCell<RecordType> = (renderCellArgs: {
  245. selected: boolean;
  246. record: RecordType;
  247. originNode: JSX.Element;
  248. inHeader: boolean;
  249. disabled: boolean;
  250. indeterminate: boolean;
  251. index?: number;
  252. selectRow?: (selected: boolean, e: Event) => void;
  253. selectAll?: (selected: boolean, e: Event) => void
  254. }) => ReactNode;
  255. export type GetCheckboxProps<RecordType> = (record: RecordType) => Omit<CheckboxProps, 'defaultChecked' | 'checked' | 'indeterminate' | 'onChange'>;
  256. export type RowSelectionOnChange<RecordType> = (selectedRowKeys?: (string | number)[], selectedRows?: RecordType[]) => void;
  257. export type RowSelectionOnSelect<RecordType> = (
  258. record?: RecordType,
  259. selected?: boolean,
  260. selectedRows?: RecordType[],
  261. nativeEvent?: React.MouseEvent
  262. ) => void;
  263. export type RowSelectionOnSelectAll<RecordType> = (selected?: boolean, selectedRows?: RecordType[], changedRows?: RecordType[]) => void;
  264. export type ExpandIcon = ((expanded?: boolean) => React.ReactNode) | React.ReactNode;
  265. export type ExpandedRowRender<RecordType> = (record?: RecordType, index?: number, expanded?: boolean) => React.ReactNode;
  266. export type Footer<RecordType> = ReactNode | ((pageData?: RecordType[]) => React.ReactNode);
  267. export type FormatPageText = ((pageInfo?: { currentStart?: number; currentEnd?: number; total?: number }) => React.ReactNode) | boolean;
  268. export type GetVirtualizedListRef = (ref: MutableRefObject<VariableSizeList>) => void;
  269. export type GroupByFunction<RecordType> = BaseGroupByFn<RecordType>;
  270. export type GroupBy<RecordType> = BaseGroupBy<RecordType>;
  271. export type Size = ArrayElement<typeof strings.SIZES>;
  272. export type Title<RecordType> = React.ReactNode | ((pageData?: RecordType[]) => React.ReactNode);
  273. export type PaginationPosition = ArrayElement<typeof strings.PAGINATION_POSITIONS>;
  274. export type Pagination = TablePaginationProps | boolean;
  275. export type TablePagination = Pagination;
  276. export interface ChangeInfoFilter<RecordType> extends BaseChangeInfoFilter<RecordType> {
  277. filters?: Filter[];
  278. onFilter?: OnFilter<RecordType>
  279. }
  280. export type ChangeInfoSorter<RecordType> = BaseChangeInfoSorter<RecordType>;
  281. export interface ChangeInfo<RecordType> {
  282. pagination?: TablePaginationProps;
  283. filters?: ChangeInfoFilter<RecordType>[];
  284. sorter?: ChangeInfoSorter<RecordType>;
  285. extra?: Record<string, any>
  286. }
  287. export type OnChange<RecordType> = (changeInfo: ChangeInfo<RecordType>) => void;
  288. export type OnRow<RecordType> = (record?: RecordType, index?: number) => OnRowReturnObject;
  289. export type OnGroupedRow<RecordType> = (record?: RecordType, index?: number) => OnGroupedRowReturnObject;
  290. export type OnHeaderRow<RecordType> = (columns?: ColumnProps<RecordType>[], index?: number) => OnHeaderRowReturnObject;
  291. export type OnExpandedRowsChange<RecordType> = (expandedRows?: IncludeGroupRecord<RecordType>[]) => void;
  292. export type OnExpand<RecordType> = (expanded?: boolean, record?: IncludeGroupRecord<RecordType>, mouseEvent?: React.MouseEvent) => void;
  293. export type RenderGroupSection = (groupKey?: string | number, group?: (string | number)[]) => ReactNode | {
  294. [x: string]: any;
  295. children: ReactNode
  296. };
  297. export type RenderPagination = (paginationProps: TablePaginationProps) => ReactNode;
  298. export type RowExpandable<RecordType> = (record?: RecordType) => boolean;
  299. export type RowKey<RecordType> = BaseRowKeyType | ((record?: RecordType) => string);
  300. export type RowSelection<RecordType> = RowSelectionProps<RecordType> | boolean;
  301. export type VirtualizedOnScrollArgs = {
  302. scrollDirection?: ScrollDirection;
  303. scrollOffset?: number;
  304. scrollUpdateWasRequested?: boolean
  305. };
  306. export type VirtualizeItemSizeRow = {
  307. sectionRow?: boolean;
  308. expandedRow?: boolean
  309. };
  310. export type VirtualizedItemSizeFn = (index?: number, row?: VirtualizeItemSizeRow) => number;
  311. export type VirtualizedItemSize = number | VirtualizedItemSizeFn;
  312. export type VirtualizedOnScroll = (object: VirtualizedOnScrollArgs) => void;
  313. export interface VirtualizedProps {
  314. [x: string]: any;
  315. itemSize?: VirtualizedItemSize;
  316. onScroll?: VirtualizedOnScroll
  317. }
  318. export type Virtualized = boolean | VirtualizedProps;
  319. export interface TablePaginationProps extends BaseProps, PaginationProps {
  320. position?: PaginationPosition;
  321. formatPageText?: FormatPageText
  322. }
  323. export type Resizable<RecordType> = ResizableProps<RecordType> | boolean;
  324. export interface ResizableProps<RecordType> {
  325. onResize?: ResizeFn<RecordType>;
  326. onResizeStart?: ResizeFn<RecordType>;
  327. onResizeStop?: ResizeFn<RecordType>
  328. }
  329. export type ResizeFn<RecordType> = (column: RecordType) => RecordType;
  330. export interface BodyScrollEvent extends React.UIEvent {
  331. [x: string]: any;
  332. currentTarget: any;
  333. target: any
  334. }
  335. export type BodyScrollPosition = 'both' | 'middle' | 'left' | 'right';
  336. export type TableLocale = Locale['Table'];
  337. export type Direction = CSSDirection;
  338. export type IncludeGroupRecord<RecordType> = BaseIncludeGroupRecord<RecordType>;
  339. export type Sticky = boolean | {
  340. top?: number
  341. }