foundation.ts 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. /* eslint-disable prefer-destructuring */
  2. /* eslint-disable max-depth */
  3. /* eslint-disable max-nested-callbacks */
  4. /* eslint-disable max-len */
  5. /* eslint-disable no-param-reassign */
  6. /* eslint-disable eqeqeq */
  7. /* eslint-disable @typescript-eslint/no-empty-function */
  8. import {
  9. get,
  10. merge,
  11. isFunction,
  12. each,
  13. find,
  14. some,
  15. pull,
  16. isSet,
  17. filter,
  18. isMap,
  19. slice,
  20. isEqual
  21. } from 'lodash';
  22. import memoizeOne from 'memoize-one';
  23. import { ArrayElement } from '../utils/type';
  24. import { BaseCheckboxProps } from '../checkbox/checkboxFoundation';
  25. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  26. import { strings, numbers } from './constants';
  27. import { mergeQueries, flattenColumns, filterColumns } from './utils';
  28. import { pullAll, withOrderSort } from '../utils/array';
  29. export interface BaseColumnProps<RecordType> {
  30. align?: BaseAlign;
  31. children?: Array<BaseColumnProps<RecordType>>;
  32. className?: string;
  33. colSpan?: number;
  34. dataIndex?: string;
  35. defaultFilteredValue?: any[];
  36. defaultSortOrder?: BaseSortOrder;
  37. filterChildrenRecord?: boolean;
  38. filterDropdown?: any;
  39. filterDropdownProps?: Record<string, any>;
  40. filterDropdownVisible?: boolean;
  41. filterIcon?: any;
  42. filterMultiple?: boolean;
  43. filteredValue?: any[];
  44. filters?: BaseFilter[];
  45. fixed?: BaseFixed;
  46. key?: string | number;
  47. onCell?: BaseOnCell<RecordType>;
  48. onFilter?: BaseOnFilter<RecordType>;
  49. onFilterDropdownVisibleChange?: BaseOnFilterDropdownVisibleChange;
  50. onHeaderCell?: BaseOnHeaderCell<RecordType>;
  51. render?: (...args: any[]) => any;
  52. renderFilterDropdownItem?: (...args: any[]) => any;
  53. sortChildrenRecord?: boolean;
  54. sortOrder?: BaseSortOrder;
  55. sorter?: BaseSorter<RecordType>;
  56. title?: any;
  57. useFullRender?: boolean;
  58. width?: string | number;
  59. }
  60. export interface TableAdapter<RecordType> extends DefaultAdapter {
  61. resetScrollY: () => void;
  62. setSelectedRowKeys: (selectedRowKeys: BaseRowKeyType[]) => void;
  63. setDisabledRowKeys: (disabledRowKeys: BaseRowKeyType[]) => void;
  64. setCurrentPage: (currentPage: number) => void;
  65. setPagination: (pagination: BasePagination) => void;
  66. setGroups: (groups: Map<string, RecordType[]>) => void;
  67. setDataSource: (dataSource: RecordType[]) => void;
  68. setExpandedRowKeys: (expandedRowKeys: BaseRowKeyType[]) => void;
  69. setQuery: (query?: BaseColumnProps<RecordType>) => void;
  70. setQueries: (queries: BaseColumnProps<RecordType>[]) => void;
  71. setFlattenData: (flattenData: RecordType[]) => void;
  72. setAllRowKeys: (allRowKeys: BaseRowKeyType[]) => void;
  73. setHoveredRowKey: (hoveredRowKey: BaseRowKeyType) => void;
  74. setCachedFilteredSortedDataSource: (filteredSortedDataSource: RecordType[]) => void;
  75. setCachedFilteredSortedRowKeys: (filteredSortedRowKeys: BaseRowKeyType[]) => void;
  76. getCurrentPage: () => number;
  77. getCurrentPageSize: () => number;
  78. getCachedFilteredSortedDataSource: () => RecordType[];
  79. getCachedFilteredSortedRowKeys: () => BaseRowKeyType[];
  80. getCachedFilteredSortedRowKeysSet: () => Set<BaseRowKeyType>;
  81. notifyFilterDropdownVisibleChange: (visible: boolean, dataIndex: string) => void;
  82. notifyChange: (changeInfo: { pagination: BasePagination; filters: BaseChangeInfoFilter<RecordType>[]; sorter: BaseChangeInfoSorter<RecordType>; extra: any }) => void;
  83. notifyExpand: (expanded?: boolean, record?: BaseIncludeGroupRecord<RecordType>, mouseEvent?: any) => void;
  84. notifyExpandedRowsChange: (expandedRows: BaseIncludeGroupRecord<RecordType>[]) => void;
  85. notifySelect: (record?: BaseIncludeGroupRecord<RecordType>, selected?: boolean, selectedRows?: BaseIncludeGroupRecord<RecordType>[], nativeEvent?: any) => void;
  86. notifySelectAll: (selected?: boolean, selectedRows?: BaseIncludeGroupRecord<RecordType>[], changedRows?: BaseIncludeGroupRecord<RecordType>[], e?: any) => void;
  87. notifySelectInvert: (record?: RecordType[], selected?: boolean, selectedRows?: BaseIncludeGroupRecord<RecordType>[], nativeEvent?: any) => void;
  88. notifySelectionChange: (selectedRowKeys: BaseRowKeyType[], selectedRows: BaseIncludeGroupRecord<RecordType>[]) => void;
  89. isAnyColumnFixed: (columns?: BaseColumnProps<RecordType>[]) => boolean;
  90. useFixedHeader: () => boolean;
  91. setHeadWidths: (headWidths: Array<BaseHeadWidth>, index?: number) => void;
  92. getHeadWidths: (index?: number) => number[];
  93. getCellWidths: (flattenedColumns: BaseColumnProps<RecordType>[], flattenedWidths?: BaseHeadWidth[], ignoreScrollBarKey?: boolean) => number[];
  94. mergedRowExpandable: (record: RecordType) => boolean;
  95. isAnyColumnUseFullRender: (columns: BaseColumnProps<RecordType>[]) => boolean;
  96. getNormalizeColumns: () => (columns: BaseColumnProps<RecordType>[], children: any) => BaseColumnProps<RecordType>[];
  97. getHandleColumns: () => (queries: BaseColumnProps<RecordType>[], cachedColumns: BaseColumnProps<RecordType>[]) => BaseColumnProps<RecordType>[];
  98. getMergePagination: () => (pagination: BasePagination) => BasePagination;
  99. setBodyHasScrollbar: (bodyHasScrollBar: boolean) => void;
  100. }
  101. class TableFoundation<RecordType> extends BaseFoundation<TableAdapter<RecordType>> {
  102. memoizedWithFnsColumns: (
  103. queries: BaseColumnProps<RecordType>[],
  104. cachedColumns: BaseColumnProps<RecordType>[],
  105. rowSelectionUpdate: boolean,
  106. hideExpandedColumn: boolean,
  107. bodyHasScrollBar: boolean,
  108. ) => BaseColumnProps<RecordType>[];
  109. memoizedFilterColumns: (columns: BaseColumnProps<RecordType>[], ignoreKeys?: string[]) => BaseColumnProps<RecordType>[];
  110. memoizedFlattenFnsColumns: (columns: BaseColumnProps<RecordType>[], childrenColumnName?: string) => BaseColumnProps<RecordType>[];
  111. memoizedPagination: (pagination: BasePagination) => BasePagination;
  112. constructor(adapter: TableAdapter<RecordType>) {
  113. super({ ...adapter });
  114. /**
  115. * memoized function list
  116. */
  117. const handleColumns: (queries: BaseColumnProps<RecordType>[], cachedColumns: BaseColumnProps<RecordType>[]) => BaseColumnProps<RecordType>[] = this._adapter.getHandleColumns();
  118. const mergePagination: (pagination: BasePagination) => BasePagination = this._adapter.getMergePagination();
  119. this.memoizedWithFnsColumns = memoizeOne(handleColumns, isEqual);
  120. this.memoizedFilterColumns = memoizeOne(filterColumns);
  121. this.memoizedFlattenFnsColumns = memoizeOne(flattenColumns);
  122. this.memoizedPagination = memoizeOne(mergePagination, isEqual);
  123. }
  124. init() {
  125. const dataSource = [...this.getProp('dataSource')];
  126. const { queries } = this._adapter.getStates();
  127. const filteredSortedDataSource = this.getFilteredSortedDataSource(dataSource, queries);
  128. const pageData = this.getCurrentPageData(filteredSortedDataSource);
  129. this.setAdapterPageData(pageData);
  130. this.initExpandedRowKeys(pageData);
  131. this.initSelectedRowKeys(pageData);
  132. // cache dataSource after mount, and then calculate it on demand
  133. this.setCachedFilteredSortedDataSource(filteredSortedDataSource);
  134. }
  135. initExpandedRowKeys({ groups }: { groups?: Map<string, RecordType[]> } = {}) {
  136. const {
  137. defaultExpandAllRows,
  138. defaultExpandedRowKeys = [],
  139. expandedRowKeys: propExpandedRowKeys = [],
  140. dataSource = [],
  141. expandAllRows,
  142. defaultExpandAllGroupRows,
  143. expandAllGroupRows,
  144. } = this.getProps();
  145. const expandedRowKeys: BaseRowKeyType[] = [];
  146. if (defaultExpandAllRows || expandAllRows) {
  147. this._addNoDuplicatedItemsToArr(
  148. expandedRowKeys,
  149. this.getAllRowKeys(dataSource),
  150. groups && isMap(groups) && groups.size ? Array.from(groups.keys()) : []
  151. );
  152. } else if (defaultExpandAllGroupRows || expandAllGroupRows) {
  153. this._addNoDuplicatedItemsToArr(
  154. expandedRowKeys,
  155. groups && isMap(groups) && groups.size ? Array.from(groups.keys()) : []
  156. );
  157. } else if (Array.isArray(defaultExpandedRowKeys) && defaultExpandedRowKeys.length) {
  158. this._addNoDuplicatedItemsToArr(expandedRowKeys, defaultExpandedRowKeys);
  159. } else if (Array.isArray(propExpandedRowKeys) && propExpandedRowKeys.length) {
  160. this._addNoDuplicatedItemsToArr(expandedRowKeys, propExpandedRowKeys);
  161. }
  162. this._adapter.setExpandedRowKeys(expandedRowKeys);
  163. }
  164. initSelectedRowKeys({ disabledRowKeys }: { disabledRowKeys?: BaseRowKeyType[] }) {
  165. const rowSelection = this.getProp('rowSelection');
  166. const rowKeys: BaseRowKeyType[] = [];
  167. if (rowSelection) {
  168. const selectedRowKeys = get(rowSelection, 'selectedRowKeys');
  169. const defaultSelectedRowKeys = get(rowSelection, 'defaultSelectedRowKeys');
  170. if (Array.isArray(selectedRowKeys)) {
  171. this._addNoDuplicatedItemsToArr(rowKeys, selectedRowKeys);
  172. } else if (Array.isArray(defaultSelectedRowKeys)) {
  173. this._addNoDuplicatedItemsToArr(rowKeys, defaultSelectedRowKeys);
  174. }
  175. if (Array.isArray(disabledRowKeys) && disabledRowKeys.length) {
  176. pull(rowKeys, ...disabledRowKeys);
  177. }
  178. this._adapter.setSelectedRowKeys(rowKeys);
  179. }
  180. }
  181. /**
  182. * Get filtered and sorted data
  183. * @param {Object[]} dataSource
  184. * @param {Object[]} queries
  185. * @returns {Object[]} sortedDataSource
  186. */
  187. getFilteredSortedDataSource(dataSource: RecordType[], queries: BaseColumnProps<RecordType>[]) {
  188. const filteredDataSource = this.filterDataSource(dataSource, queries.filter(
  189. query => {
  190. /**
  191. * 这里无需判断 filteredValue 是否为数组,初始化时它是 `undefined`,点击选择空时为 `[]`
  192. * 初始化时我们应该用 `defaultFilteredValue`,点击后我们应该用 `filteredValue`
  193. *
  194. * There is no need to judge whether `filteredValue` is an array here, because it is `undefined` when initialized, and `[]` when you click to select empty
  195. * When initializing we should use `defaultFilteredValue`, after clicking we should use `filteredValue`
  196. */
  197. const currentFilteredValue = query.filteredValue ? query.filteredValue : query.defaultFilteredValue;
  198. return (
  199. isFunction(query.onFilter) &&
  200. Array.isArray(query.filters) &&
  201. query.filters.length &&
  202. Array.isArray(currentFilteredValue) &&
  203. currentFilteredValue.length
  204. );
  205. }
  206. ));
  207. const sortedDataSource = this.sortDataSource(filteredDataSource, queries.filter(query => query && isFunction(query.sorter)));
  208. return sortedDataSource;
  209. }
  210. /**
  211. * get current page data
  212. *
  213. * @param {Array} dataSource
  214. * @param {object} pagination
  215. * @param {object} queries
  216. * @returns {{dataSource: RecordType[], groups: Map<string, Set<string>>, pagination: object, disabledRowKeys: string[], queries: BaseColumnProps[], allRowKeys: string[]}}
  217. */
  218. getCurrentPageData(dataSource?: RecordType[], pagination?: BasePagination, queries?: BaseColumnProps<RecordType>[]) {
  219. const filteredSortedDataSource = this._adapter.getCachedFilteredSortedDataSource();
  220. dataSource = dataSource == null ? [...filteredSortedDataSource] : dataSource;
  221. pagination =
  222. pagination == null ? this.getState('pagination') && { ...this.getState('pagination') } : pagination;
  223. queries = queries == null ? [...this.getState('queries')] : queries;
  224. let groups;
  225. if (this.getProp('groupBy') != null) {
  226. const { groups: groupedGroups, dataSource: groupedData } = this.groupDataSource(dataSource);
  227. dataSource = groupedData;
  228. groups = groupedGroups;
  229. }
  230. pagination = this.normalizePagination(pagination, dataSource);
  231. dataSource = this.limitPageDataSource(dataSource, pagination);
  232. const disabledRowKeys = this.getAllDisabledRowKeys(dataSource);
  233. const allRowKeys = this.getAllRowKeys(dataSource);
  234. const pageData: BasePageData<RecordType> = {
  235. dataSource,
  236. groups,
  237. pagination,
  238. disabledRowKeys,
  239. allRowKeys,
  240. queries,
  241. };
  242. return pageData;
  243. }
  244. /**
  245. * group dataSource, return grouped row keys
  246. *
  247. * @param {*[]} dataSource
  248. * @param {Function|string} groupBy
  249. */
  250. groupDataSource(dataSource: RecordType[], groupBy?: BaseGroupBy<RecordType>) {
  251. groupBy = groupBy == null ? this.getProp('groupBy') : groupBy;
  252. const groups = new Map();
  253. const newDataSource = [];
  254. if (groupBy != null) {
  255. each(dataSource, (record, index) => {
  256. const groupKey = typeof groupBy === 'function' ? groupBy(record) : get(record, groupBy);
  257. if (groupKey != null && groupKey !== '') {
  258. const recordKey = this.getRecordKey(record);
  259. let group = groups.get(groupKey);
  260. if (!isSet(group)) {
  261. group = new Set([recordKey]);
  262. groups.set(groupKey, group);
  263. } else {
  264. group.add(recordKey);
  265. }
  266. }
  267. });
  268. }
  269. if (groups && groups.size) {
  270. groups.forEach((set, key) => {
  271. if (isSet(set)) {
  272. set.forEach(realKey => {
  273. newDataSource.push(this._getRecord(realKey));
  274. });
  275. }
  276. });
  277. } else {
  278. newDataSource.push(...dataSource);
  279. }
  280. return { groups, dataSource: newDataSource };
  281. }
  282. /**
  283. * sort data
  284. *
  285. * @param {Array} dataSource
  286. * @param {Array} sorters
  287. * @returns {Array}
  288. */
  289. sortDataSource(dataSource: RecordType[], sorters: BaseSorterInfo<RecordType>[]) {
  290. each(sorters, sorterObj => {
  291. // const sorterObj = last(sorters) || {};
  292. const { sorter, sortOrder, defaultSortOrder, sortChildrenRecord } = sorterObj;
  293. const currentSortOrder = this.isSortOrderValid(sortOrder) ? sortOrder : defaultSortOrder;
  294. if (isFunction(sorter) && (currentSortOrder && strings.SORT_DIRECTIONS.includes(currentSortOrder))) {
  295. if (sortChildrenRecord) {
  296. const childrenRecordName = this.getProp('childrenRecordName');
  297. dataSource =
  298. dataSource &&
  299. dataSource.map(record => {
  300. const children = this._getRecordChildren(record);
  301. if (Array.isArray(children) && children.length) {
  302. return {
  303. ...record,
  304. [childrenRecordName]: this.sortDataSource(children, [sorterObj]),
  305. };
  306. }
  307. return record;
  308. });
  309. }
  310. dataSource.sort(withOrderSort(sorter, currentSortOrder));
  311. return false;
  312. }
  313. return undefined;
  314. });
  315. return dataSource;
  316. }
  317. /**
  318. * set page number
  319. */
  320. setPage = (currentPage: number, currentPageSize: number) => {
  321. currentPage = currentPage || this._adapter.getCurrentPage();
  322. const currentPagination = this.getState('pagination');
  323. const { dataSource, pagination, disabledRowKeys, allRowKeys } = this.getCurrentPageData(null, {
  324. ...currentPagination,
  325. currentPage,
  326. pageSize: currentPageSize,
  327. });
  328. if (!this._pagerIsControlled() && currentPage > 0) {
  329. this._adapter.setDisabledRowKeys(disabledRowKeys);
  330. this._adapter.setAllRowKeys(allRowKeys);
  331. this._adapter.setPagination(pagination);
  332. this._adapter.setDataSource(dataSource);
  333. }
  334. this._notifyChange(pagination);
  335. };
  336. /**
  337. * filter data source
  338. *
  339. * @param {*[]} dataSource
  340. * @param {*[]} filters
  341. * @returns {*[]}
  342. */
  343. filterDataSource(dataSource: RecordType[], filters: BaseChangeInfoFilter<RecordType>[]) {
  344. let filteredData: Map<string, RecordType> | null = null;
  345. let hasValidFilters = false;
  346. const childrenRecordName = this.getProp('childrenRecordName');
  347. each(filters, filterObj => {
  348. const { onFilter, filteredValue, filterChildrenRecord, defaultFilteredValue } = filterObj;
  349. const currentFilteredValue = Array.isArray(filteredValue) ? filteredValue : defaultFilteredValue;
  350. if (typeof onFilter === 'function' && Array.isArray(currentFilteredValue) && currentFilteredValue.length) {
  351. hasValidFilters = true;
  352. if (filteredData === null) {
  353. filteredData = new Map();
  354. } else {
  355. dataSource = Array.from(filteredData && filteredData.values());
  356. filteredData = new Map();
  357. }
  358. each(currentFilteredValue, value => {
  359. each(dataSource, record => {
  360. const childrenRecords = get(record, childrenRecordName);
  361. const recordKey = this.getRecordKey(record);
  362. let filteredChildren;
  363. if (Array.isArray(childrenRecords) && childrenRecords.length && filterChildrenRecord) {
  364. filteredChildren = this.filterDataSource(childrenRecords, [filterObj]);
  365. }
  366. if (Array.isArray(filteredChildren) && filteredChildren.length) {
  367. if (recordKey != null) {
  368. const children = get(filteredData.get(recordKey), childrenRecordName, []);
  369. filteredData.set(recordKey, {
  370. ...record,
  371. [childrenRecordName]: filteredChildren.reduce(
  372. (arr, cur) => {
  373. if (
  374. arr.find((item: any) => this.getRecordKey(item) === this.getRecordKey(cur)) ==
  375. null
  376. ) {
  377. arr.push(cur);
  378. }
  379. return arr;
  380. },
  381. [...children]
  382. ),
  383. });
  384. }
  385. } else if (onFilter(value, record)) {
  386. filteredData.set(recordKey, record);
  387. }
  388. });
  389. });
  390. }
  391. });
  392. if (hasValidFilters) {
  393. dataSource = Array.from(filteredData && filteredData.values());
  394. }
  395. return dataSource;
  396. }
  397. limitPageDataSource(dataSource: RecordType[], pagination: BasePagination) {
  398. dataSource = dataSource == null ? this.getProp('dataSource') : dataSource;
  399. pagination = pagination == null ? this.getState('pagination') : pagination;
  400. let pageData = dataSource;
  401. const pageNo = get(pagination, 'currentPage');
  402. if (this.getProp('pagination') !== false && pageNo && dataSource && pagination && !this._pagerIsControlled()) {
  403. const { pageSize = numbers.DEFAULT_PAGE_SIZE } = pagination;
  404. const start = (pageNo - 1) * pageSize;
  405. const end = pageNo * pageSize;
  406. pageData = slice(dataSource, start, end);
  407. }
  408. return pageData;
  409. }
  410. normalizePagination(pagination: BasePagination, dataSource: RecordType[]) {
  411. pagination = pagination == null ? this._getPagination() : pagination;
  412. dataSource = dataSource == null ? this._getDataSource() : dataSource;
  413. const propPagination = this.getProp('pagination');
  414. if (pagination) {
  415. pagination = typeof pagination === 'object' ? { ...pagination } : {};
  416. pagination = merge(
  417. {
  418. total: (dataSource && dataSource.length) || 0,
  419. pageSize: numbers.DEFAULT_PAGE_SIZE,
  420. currentPage: get(propPagination, 'defaultCurrentPage', 1),
  421. position: strings.PAGINATION_POSITIONS[0],
  422. },
  423. pagination
  424. );
  425. if (!this._pagerIsControlled()) {
  426. const total = get(propPagination, 'total', dataSource.length);
  427. const pageSize = get(propPagination, 'pageSize', pagination.pageSize);
  428. const { currentPage } = pagination;
  429. const realTotalPage = Math.ceil(total / pageSize);
  430. pagination.total = total;
  431. if (currentPage > realTotalPage) {
  432. pagination.currentPage = 1;
  433. }
  434. }
  435. }
  436. return pagination;
  437. }
  438. setAdapterPageData(pageData: BasePageData<RecordType> = {}) {
  439. const { pagination, dataSource, disabledRowKeys, allRowKeys, groups } = pageData;
  440. this._adapter.setDisabledRowKeys(disabledRowKeys);
  441. this._adapter.setAllRowKeys(allRowKeys);
  442. this._adapter.setPagination(pagination);
  443. this._adapter.setGroups(groups);
  444. this._adapter.setDataSource(dataSource);
  445. }
  446. /**
  447. * Cache related data when initializing or updating the calculated dataSource
  448. * @param {*} filteredSortedDataSource
  449. */
  450. setCachedFilteredSortedDataSource = (filteredSortedDataSource: RecordType[]) => {
  451. this._adapter.setCachedFilteredSortedDataSource(filteredSortedDataSource);
  452. const filteredSortedRowKeys = this.getAllRowKeys(filteredSortedDataSource);
  453. this._adapter.setCachedFilteredSortedRowKeys(filteredSortedRowKeys);
  454. };
  455. destroy() { }
  456. handleClick(e: any) { }
  457. handleMouseEnter(e: any) { }
  458. handleMouseLeave(e: any) { }
  459. stopPropagation(e: any) {
  460. if (e && typeof e === 'object') {
  461. if (typeof e.stopPropagation === 'function') {
  462. e.stopPropagation();
  463. }
  464. if (e.nativeEvent && typeof e.nativeEvent.stopPropagation === 'function') {
  465. e.nativeEvent.stopPropagation();
  466. } else if (typeof e.stopImmediatePropagation === 'function') {
  467. e.stopImmediatePropagation();
  468. }
  469. }
  470. }
  471. /**
  472. * Add non-repeating elements to the array itself
  473. * @param {Array} srcArr
  474. * @param {Object} objArrs
  475. */
  476. _addNoDuplicatedItemsToArr(srcArr: any[] = [], ...objArrs: any[]) {
  477. for (const objArr of objArrs) {
  478. if (Array.isArray(objArr)) {
  479. for (const item of objArr) {
  480. if (!srcArr.includes(item)) {
  481. srcArr.push(item);
  482. }
  483. }
  484. }
  485. }
  486. return srcArr;
  487. }
  488. _notifyChange(pagination: BasePagination, filters?: BaseChangeInfoFilter<RecordType>[], sorter?: BaseChangeInfoSorter<RecordType>, extra?: RecordType) {
  489. pagination = pagination == null ? this._getPagination() : pagination;
  490. filters = filters == null ? this._getAllFilters() : filters;
  491. sorter = sorter == null ? this._getAllSorters()[0] as BaseChangeInfoSorter<RecordType> : sorter;
  492. if (get(this.getProp('scroll'), 'scrollToFirstRowOnChange')) {
  493. this._adapter.resetScrollY();
  494. }
  495. this._adapter.notifyChange({
  496. pagination: { ...pagination },
  497. filters: [...filters],
  498. sorter,
  499. extra: { ...extra },
  500. });
  501. }
  502. _rowExpansionIsControlled() {
  503. return Array.isArray(this.getProp('expandedRowKeys'));
  504. }
  505. _pagerIsControlled() {
  506. return get(this.getProp('pagination'), 'currentPage') != null;
  507. }
  508. _selectionIsControlled() {
  509. return Array.isArray(get(this.getProp('rowSelection'), 'selectedRowKeys'));
  510. }
  511. /**
  512. * Determine whether the column sorting is controlled
  513. * Controlled: the column passed the sortOrder prop
  514. * @param {String} dataIndex
  515. * @returns {Boolean}
  516. */
  517. _sorterIsControlled(dataIndex: string) {
  518. // The basis for judgment should be props columns instead of cachedColumns fix#1141
  519. const query = dataIndex && this.getQuery(dataIndex, this.getState('flattenColumns'));
  520. return Boolean(query && query.sortOrder != null);
  521. }
  522. /**
  523. * Determine whether the column is filtered and controlled
  524. * Controlled: the column passed the filteredValue prop
  525. * @param {String} dataIndex
  526. * @returns {Boolean}
  527. */
  528. _filterIsControlled(dataIndex: string) {
  529. const query = dataIndex && this.getQuery(dataIndex, this.getState('flattenColumns'));
  530. return Boolean(query && Array.isArray(query.filteredValue));
  531. }
  532. _filterShowIsControlled(dataIndex?: string) {
  533. const query = dataIndex && this.getQuery(dataIndex, this.getState('flattenColumns'));
  534. return Boolean(query && (query.filterDropdownVisible === true || query.filterDropdownVisible === false));
  535. }
  536. _getSelectedRowKeys() {
  537. const rowSelection = this.getState('rowSelection');
  538. const selectedRowKeys = get(rowSelection, 'selectedRowKeys', []);
  539. return [...selectedRowKeys];
  540. }
  541. _getSelectedRowKeysSet() {
  542. const rowSelection = this.getState('rowSelection');
  543. const selectedRowKeysSet = get(rowSelection, 'selectedRowKeysSet', new Set());
  544. return selectedRowKeysSet;
  545. }
  546. _getDataSource() {
  547. return this.getProp('dataSource') || [];
  548. }
  549. _getRecord(realKey: string | number) {
  550. return find(
  551. this.getProp('dataSource'),
  552. record => realKey != null && realKey !== '' && this.getRecordKey(record) === realKey
  553. );
  554. }
  555. _getRecordChildren(record: RecordType) {
  556. return get(record, this.getProp('childrenRecordName'));
  557. }
  558. _getPagination() {
  559. return this.getState('pagination') || {};
  560. }
  561. _getAllFilters(queries?: BaseColumnProps<RecordType>[]) {
  562. queries = queries || this.getState('queries');
  563. const filters: BaseChangeInfoFilter<RecordType>[] = [];
  564. each(queries, query => {
  565. if (
  566. Array.isArray(query.filteredValue) &&
  567. (query.filteredValue.length || this._filterIsControlled(query.dataIndex))
  568. ) {
  569. filters.push(query);
  570. }
  571. });
  572. return filters;
  573. }
  574. _getAllSorters(queries?: BaseColumnProps<RecordType>[]): BaseColumnProps<RecordType>[] {
  575. queries = queries || this.getState('queries');
  576. return filter(queries, query => query.sorter && query.sortOrder) as BaseColumnProps<RecordType>[];
  577. }
  578. _filterQueries(targetQuery: BaseColumnProps<RecordType>, queries: BaseColumnProps<RecordType>[], keys = ['dataIndex']) {
  579. queries = queries == null ? this.getState('queries') : queries;
  580. const filteredQueries: BaseColumnProps<RecordType>[] = [];
  581. const filteredIndexes: number[] = [];
  582. each(queries, (itQuery, index) => {
  583. const flag = some(keys, k => k && targetQuery[k] != null && targetQuery[k] === itQuery[k]);
  584. if (flag) {
  585. filteredQueries.push(itQuery);
  586. filteredIndexes.push(index);
  587. }
  588. });
  589. return { filteredQueries, filteredIndexes };
  590. }
  591. _mergeToQueries(query: BaseColumnProps<RecordType>, queries: BaseColumnProps<RecordType>[], keys = ['dataIndex']) {
  592. queries = queries == null ? this.getState('queries') : queries;
  593. queries = [...queries];
  594. query = { ...query };
  595. const { filteredQueries, filteredIndexes } = this._filterQueries(query, queries, keys);
  596. each(filteredQueries, (curQuery, idx) => {
  597. // assign(curQuery, query);
  598. queries[filteredIndexes[idx]] = { ...query };
  599. });
  600. return queries;
  601. }
  602. /**
  603. * get record real key
  604. * @param {RecordType} record
  605. * @returns {string}
  606. */
  607. getRecordKey(record: RecordType): string {
  608. if (!record) {
  609. return undefined;
  610. }
  611. const rowKey = this.getProp('rowKey');
  612. return typeof rowKey === 'function' ? rowKey(record) : get(record, rowKey);
  613. }
  614. isEmpty(dataSource: RecordType[]) {
  615. dataSource = dataSource == null ? this.getProp('dataSource') : dataSource;
  616. return !(Array.isArray(dataSource) && dataSource.length > 0);
  617. }
  618. handleSelectRow(realKey: BaseRowKeyType, selected: boolean, e: any) {
  619. this.stopPropagation(e);
  620. if (typeof selected === 'boolean' && realKey != null) {
  621. const selectedRowKeys = this._getSelectedRowKeys();
  622. let foundIdx = -1;
  623. const selectedRow = this.getSelectedRows(null, [realKey])[0];
  624. let selectedRows: BaseIncludeGroupRecord<RecordType>[];
  625. if ((foundIdx = selectedRowKeys.indexOf(realKey)) > -1 && selected === false) {
  626. selectedRowKeys.splice(foundIdx, 1);
  627. selectedRows = this.getSelectedRows(null, selectedRowKeys);
  628. if (!this._selectionIsControlled()) {
  629. this._adapter.setSelectedRowKeys(selectedRowKeys);
  630. }
  631. this._adapter.notifySelect(selectedRow, selected, selectedRows, e);
  632. this._adapter.notifySelectionChange(selectedRowKeys, selectedRows);
  633. } else if (selectedRowKeys.indexOf(realKey) === -1 && selected === true) {
  634. selectedRowKeys.push(realKey);
  635. selectedRows = this.getSelectedRows(null, selectedRowKeys);
  636. if (!this._selectionIsControlled()) {
  637. this._adapter.setSelectedRowKeys(selectedRowKeys);
  638. }
  639. this._adapter.notifySelect(selectedRow, selected, selectedRows, e);
  640. this._adapter.notifySelectionChange(selectedRowKeys, selectedRows);
  641. }
  642. }
  643. }
  644. /**
  645. * select all rows
  646. * @param {*} selected The future state of the select all button
  647. * @param {*} e
  648. */
  649. handleSelectAllRow(selected: boolean, e: any) {
  650. this.stopPropagation(e);
  651. if (typeof selected === 'boolean') {
  652. const curSelectedRowKeys = this._getSelectedRowKeys();
  653. let selectedRowKeys = [...curSelectedRowKeys];
  654. const selectedRowKeysSet = this._getSelectedRowKeysSet();
  655. let allRowKeys = [...this._adapter.getCachedFilteredSortedRowKeys()];
  656. const disabledRowKeys = this.getAllDisabledRowKeys();
  657. const disabledRowKeysSet = new Set(disabledRowKeys);
  658. let changedRowKeys;
  659. // Select all, if not disabled && not in selectedRowKeys
  660. if (selected) {
  661. for (const key of allRowKeys) {
  662. if (!disabledRowKeysSet.has(key) && !selectedRowKeysSet.has(key)) {
  663. selectedRowKeys.push(key);
  664. }
  665. }
  666. allRowKeys = pullAll(allRowKeys, [...disabledRowKeys, ...curSelectedRowKeys]);
  667. changedRowKeys = [...allRowKeys];
  668. } else {
  669. selectedRowKeys = pullAll(selectedRowKeys, allRowKeys);
  670. changedRowKeys = [...curSelectedRowKeys];
  671. }
  672. const changedRows = this.getSelectedRows(null, changedRowKeys || []);
  673. const selectedRows = this.getSelectedRows(null, selectedRowKeys || []);
  674. if (!this._selectionIsControlled()) {
  675. this._adapter.setSelectedRowKeys(selectedRowKeys);
  676. }
  677. this._adapter.notifySelectAll(selected, selectedRows, changedRows, e);
  678. this._adapter.notifySelectionChange(selectedRowKeys, selectedRows);
  679. }
  680. }
  681. /**
  682. * row keys => rows
  683. * @param {*} dataSource
  684. * @param {*} selectedRowKeys
  685. * @param {*} selectedRowKeysSet Recursive optimization
  686. */
  687. getSelectedRows(dataSource: RecordType[], selectedRowKeys: BaseRowKeyType[], selectedRowKeysSet?: Set<BaseRowKeyType>): BaseIncludeGroupRecord<RecordType>[] {
  688. dataSource = dataSource == null ? this._getDataSource() : dataSource;
  689. selectedRowKeys = selectedRowKeys == null ? this._getSelectedRowKeys() : selectedRowKeys;
  690. if (!isSet(selectedRowKeysSet)) {
  691. selectedRowKeysSet = new Set(selectedRowKeys);
  692. }
  693. const childrenRecordName = this.getProp('childrenRecordName');
  694. const selectedRows: BaseIncludeGroupRecord<RecordType>[] = [];
  695. if (
  696. isSet(selectedRowKeysSet) &&
  697. selectedRowKeysSet.size &&
  698. Array.isArray(dataSource) &&
  699. dataSource.length
  700. ) {
  701. // Time complexity optimization, replace the includes operation of array with has of set
  702. selectedRows.push(...dataSource.filter(data => selectedRowKeysSet.has(this.getRecordKey(data))));
  703. if (selectedRows.length < selectedRowKeys.length) {
  704. for (const item of dataSource) {
  705. const children = get(item, childrenRecordName);
  706. if (Array.isArray(children) && children.length) {
  707. const rows = this.getSelectedRows(children, selectedRowKeys, selectedRowKeysSet);
  708. selectedRows.push(...rows);
  709. }
  710. }
  711. }
  712. }
  713. return selectedRows;
  714. }
  715. getAllDisabledRowKeys(dataSource?: RecordType[], getCheckboxProps?: GetCheckboxProps<RecordType>): BaseRowKeyType[] {
  716. dataSource = dataSource == null ? this._getDataSource() : dataSource;
  717. getCheckboxProps =
  718. getCheckboxProps == null ? get(this.getProp('rowSelection'), 'getCheckboxProps') : getCheckboxProps;
  719. const childrenRecordName = this.getProp('childrenRecordName');
  720. const disabledRowKeys: BaseRowKeyType[] = [];
  721. if (Array.isArray(dataSource) && dataSource.length && typeof getCheckboxProps === 'function') {
  722. for (const record of dataSource) {
  723. const props = getCheckboxProps(record);
  724. if (props && props.disabled) {
  725. disabledRowKeys.push(this.getRecordKey(record));
  726. }
  727. const children = get(record, childrenRecordName);
  728. if (Array.isArray(children) && children.length) {
  729. const keys: BaseRowKeyType[] = this.getAllDisabledRowKeys(children, getCheckboxProps);
  730. disabledRowKeys.push(...keys);
  731. }
  732. }
  733. }
  734. return disabledRowKeys;
  735. }
  736. getAllRowKeys(dataSource: RecordType[]): BaseRowKeyType[] {
  737. dataSource = dataSource == null ? this._getDataSource() : dataSource;
  738. const childrenRecordName = this.getProp('childrenRecordName');
  739. const allRowKeys = [];
  740. if (Array.isArray(dataSource) && dataSource.length) {
  741. for (const record of dataSource) {
  742. const childrenRowKeys = [];
  743. const children = get(record, childrenRecordName);
  744. if (Array.isArray(children) && children.length) {
  745. childrenRowKeys.push(...this.getAllRowKeys(children));
  746. }
  747. allRowKeys.push(this.getRecordKey(record), ...childrenRowKeys);
  748. }
  749. }
  750. return allRowKeys;
  751. }
  752. /**
  753. * Check if the selected item is in allRowKeysSet
  754. * @param {Array} selectedRowKeys
  755. * @param {Set} allRowKeysSet
  756. */
  757. hasRowSelected(selectedRowKeys: BaseRowKeyType[], allRowKeysSet: Set<BaseRowKeyType>) {
  758. return Boolean(Array.isArray(selectedRowKeys) &&
  759. selectedRowKeys.length &&
  760. isSet(allRowKeysSet) &&
  761. allRowKeysSet.size &&
  762. selectedRowKeys.filter(key => allRowKeysSet.has(key)).length);
  763. }
  764. /**
  765. * expand processing function
  766. * @param {Boolean} expanded
  767. * @param {String} realKey
  768. * @param {Event} domEvent
  769. */
  770. handleRowExpanded(expanded: boolean, realKey: string, domEvent: any) {
  771. this.stopPropagation(domEvent);
  772. const expandedRowKeys = [...this.getState('expandedRowKeys')];
  773. const index = expandedRowKeys.indexOf(realKey);
  774. const keyIsValid = typeof realKey === 'string' || typeof realKey === 'number';
  775. if (keyIsValid && expanded && index === -1) {
  776. expandedRowKeys.push(realKey);
  777. } else if (keyIsValid && !expanded && index > -1) {
  778. expandedRowKeys.splice(index, 1);
  779. }
  780. if (!this._rowExpansionIsControlled()) {
  781. this._adapter.setExpandedRowKeys(expandedRowKeys);
  782. }
  783. const expandedRows = this.getSelectedRows(null, expandedRowKeys);
  784. let expandedRow = this.getSelectedRows(null, [realKey])[0];
  785. // groups record processing
  786. const groups = this._getGroups();
  787. if (groups) {
  788. // Construct group expandRow
  789. if (groups.has(realKey)) {
  790. expandedRow = { groupKey: realKey };
  791. }
  792. // If expandedRowKeys includes groupKey, add to expandedRows
  793. for (let i = 0, len = expandedRowKeys.length; i < len; i++) {
  794. if (groups.has(realKey)) {
  795. expandedRows.push({ groupKey: expandedRowKeys[i] });
  796. }
  797. }
  798. }
  799. this._adapter.notifyExpand(expanded, expandedRow, domEvent);
  800. this._adapter.notifyExpandedRowsChange(expandedRows);
  801. }
  802. /**
  803. * get state.groups
  804. * @returns {Map|Null}
  805. */
  806. _getGroups() {
  807. const groupBy = this._adapter.getProp('groupBy');
  808. if (groupBy !== null) {
  809. const groups = this._adapter.getState('groups');
  810. return groups;
  811. }
  812. return null;
  813. }
  814. /**
  815. * Determine whether you have selected all except for disabled
  816. * @param {Set} selectedRowKeysSet
  817. * @param {Set} disabledRowKeysSet
  818. * @param {Array} allKeys keys after sorted and filtered
  819. */
  820. allIsSelected(selectedRowKeysSet: Set<BaseRowKeyType>, disabledRowKeysSet: Set<BaseRowKeyType>, allKeys: BaseRowKeyType[]) {
  821. const filteredAllKeys = filter(allKeys, key => key != null && !disabledRowKeysSet.has(key));
  822. if (filteredAllKeys && filteredAllKeys.length) {
  823. for (const key of filteredAllKeys) {
  824. if (key != null && !selectedRowKeysSet.has(key)) {
  825. return false;
  826. }
  827. }
  828. return true;
  829. } else {
  830. return false;
  831. }
  832. }
  833. /**
  834. * This function is not used yet
  835. * @param {*} selectedRowKeys
  836. * @param {*} allKeys
  837. */
  838. allIsNotSelected(selectedRowKeys: BaseRowKeyType[], allKeys: BaseRowKeyType[]) {
  839. for (const key of allKeys) {
  840. if (key != null && Array.isArray(selectedRowKeys) && selectedRowKeys.includes(key)) {
  841. return true;
  842. }
  843. }
  844. return false;
  845. }
  846. formatPaginationInfo(pagination: BasePagination = {}, defaultPageText = '') {
  847. let info = '';
  848. const formatPageText = get(this.getProp('pagination'), 'formatPageText');
  849. const { total, pageSize, currentPage } = pagination;
  850. const currentStart = Math.min((currentPage - 1) * pageSize + 1, total);
  851. const currentEnd = Math.min(currentPage * pageSize, total);
  852. if (formatPageText || (formatPageText !== false && defaultPageText && total > 0)) {
  853. info =
  854. typeof formatPageText === 'function' ?
  855. formatPageText({ currentStart, currentEnd, total }) :
  856. defaultPageText
  857. .replace('${currentStart}', currentStart as any)
  858. .replace('${currentEnd}', currentEnd as any)
  859. .replace('${total}', total as any);
  860. }
  861. return info;
  862. }
  863. toggleShowFilter(dataIndex: string, visible: boolean) {
  864. let filterObj: BaseColumnProps<RecordType> = this.getQuery(dataIndex);
  865. const filterDropdownVisible = visible;
  866. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  867. filterObj = { ...filterObj, filterDropdownVisible };
  868. if (!this._filterShowIsControlled()) {
  869. // this._adapter.setQuery({
  870. // ...filterObj,
  871. // filterDropdownVisible,
  872. // });
  873. }
  874. this._adapter.notifyFilterDropdownVisibleChange(filterDropdownVisible, dataIndex);
  875. }
  876. /**
  877. * Called when the filter changes
  878. * @param {*} dataIndex
  879. * @param {*} data
  880. */
  881. handleFilterSelect(dataIndex: string, data: { filteredValue?: string[] } = {}) {
  882. let query: BaseColumnProps<RecordType> = this.getQuery(dataIndex);
  883. let queries = [...this._adapter.getState('queries')];
  884. const { filteredValue } = data;
  885. query = {
  886. ...query,
  887. filteredValue,
  888. };
  889. queries = mergeQueries(query, queries);
  890. const mergedQueries = this._mergeToQueries(query, null);
  891. const filters = this._getAllFilters(mergedQueries);
  892. if (!this._filterIsControlled(dataIndex)) {
  893. this._adapter.setQueries(queries);
  894. this.handleClickFilterOrSorter(queries);
  895. }
  896. this._notifyChange(null, filters);
  897. }
  898. /**
  899. * Click the sort button to call
  900. * @param {*} column
  901. * @param {*} e
  902. */
  903. handleSort(column: { dataIndex?: string; sortOrder?: BaseSortOrder } = {}, e: any) {
  904. this.stopPropagation(e);
  905. const { dataIndex } = column;
  906. let queries = this.getState('queries');
  907. let curQuery = null;
  908. queries = [...queries];
  909. each(queries, (query, idx, arr) => {
  910. if (query.sorter) {
  911. const sorterObj = { ...query };
  912. const stateSortOrder = get(sorterObj, 'sortOrder');
  913. const defaultSortOrder = get(sorterObj, 'defaultSortOrder', false);
  914. let querySortOrder = this.isSortOrderValid(stateSortOrder) ? stateSortOrder : defaultSortOrder;
  915. if (dataIndex && dataIndex === sorterObj.dataIndex) {
  916. if (querySortOrder === strings.SORT_DIRECTIONS[0]) {
  917. querySortOrder = strings.SORT_DIRECTIONS[1];
  918. } else if (querySortOrder === strings.SORT_DIRECTIONS[1]) {
  919. querySortOrder = false;
  920. } else {
  921. querySortOrder = strings.SORT_DIRECTIONS[0];
  922. }
  923. } else {
  924. // This results in the current click only supports single column sorting
  925. querySortOrder = false;
  926. }
  927. arr[idx] = { ...sorterObj, sortOrder: querySortOrder };
  928. if (dataIndex === sorterObj.dataIndex) {
  929. curQuery = arr[idx];
  930. }
  931. }
  932. });
  933. if (!this._sorterIsControlled(dataIndex)) {
  934. this._adapter.setQueries(queries);
  935. this.handleClickFilterOrSorter(queries);
  936. }
  937. // notify sort event
  938. this._notifyChange(null, null, curQuery, null);
  939. }
  940. /**
  941. * Recalculate the cached data after clicking filter or sorter
  942. * @param {*} queries
  943. */
  944. handleClickFilterOrSorter(queries: BaseColumnProps<RecordType>[]) {
  945. const dataSource = [...this.getProp('dataSource')];
  946. const sortedDataSource = this.getFilteredSortedDataSource(dataSource, queries);
  947. this.setCachedFilteredSortedDataSource(sortedDataSource);
  948. const pageData = this.getCurrentPageData(sortedDataSource);
  949. this.setAdapterPageData(pageData);
  950. }
  951. getQuery(dataIndex: string, queries?: BaseColumnProps<RecordType>[]) {
  952. queries = queries || this.getState('queries');
  953. if (dataIndex != null) {
  954. return find(queries, query => query.dataIndex === dataIndex);
  955. }
  956. return undefined;
  957. }
  958. getCellWidths(flattenedColumns: BaseColumnProps<RecordType>[], flattenedWidths?: BaseHeadWidth[], ignoreScrollBarKey?: boolean) {
  959. return this._adapter.getCellWidths(flattenedColumns, flattenedWidths, ignoreScrollBarKey);
  960. }
  961. setHeadWidths(headWidths: Array<BaseHeadWidth>, index?: number) {
  962. return this._adapter.setHeadWidths(headWidths, index);
  963. }
  964. getHeadWidths(index: number) {
  965. return this._adapter.getHeadWidths(index);
  966. }
  967. mergedRowExpandable(record: RecordType) {
  968. return this._adapter.mergedRowExpandable(record);
  969. }
  970. setBodyHasScrollbar(bodyHasScrollbar: boolean) {
  971. this._adapter.setBodyHasScrollbar(bodyHasScrollbar);
  972. }
  973. isSortOrderValid = (sortOrder: BaseSortOrder) => strings.SORT_DIRECTIONS.includes(sortOrder as any) || sortOrder === false;
  974. }
  975. export type BaseRowKeyType = string | number;
  976. export interface BasePagination {
  977. total?: number;
  978. currentPage?: number;
  979. pageSize?: number;
  980. position?: ArrayElement<typeof strings.PAGINATION_POSITIONS>;
  981. defaultCurrentPage?: number;
  982. formatPageText?: any;
  983. }
  984. export interface BaseHeadWidth {
  985. width: number;
  986. key: string;
  987. }
  988. export interface BasePageData<RecordType> {
  989. dataSource?: RecordType[];
  990. groups?: Map<string, RecordType[]>;
  991. pagination?: BasePagination;
  992. disabledRowKeys?: BaseRowKeyType[];
  993. allRowKeys?: BaseRowKeyType[];
  994. queries?: BaseColumnProps<RecordType>[];
  995. }
  996. export type GetCheckboxProps<RecordType> = (record?: RecordType) => BaseCheckboxProps;
  997. export type BaseGroupBy<RecordType> = string | number | BaseGroupByFn<RecordType>;
  998. export type BaseGroupByFn<RecordType> = (record?: RecordType) => string | number;
  999. export interface BaseSorterInfo<RecordType> {
  1000. [x: string]: any;
  1001. dataIndex?: string;
  1002. sortOrder?: BaseSortOrder;
  1003. sorter?: BaseSorter<RecordType>;
  1004. }
  1005. export type BaseSortOrder = boolean | ArrayElement<typeof strings.SORT_DIRECTIONS>;
  1006. export type BaseSorter<RecordType> = boolean | ((a?: RecordType, b?: RecordType) => number);
  1007. export interface BaseChangeInfoFilter<RecordType> {
  1008. dataIndex?: string;
  1009. value?: any;
  1010. text?: any;
  1011. filters?: BaseFilter[];
  1012. onFilter?: (filteredValue?: any, record?: RecordType) => boolean;
  1013. filteredValue?: any[];
  1014. defaultFilteredValue?: any[];
  1015. children?: BaseFilter[];
  1016. filterChildrenRecord?: boolean;
  1017. }
  1018. export interface BaseFilter {
  1019. value?: any;
  1020. text?: any;
  1021. children?: BaseFilter[];
  1022. }
  1023. export type BaseFixed = ArrayElement<typeof strings.FIXED_SET>;
  1024. export type BaseAlign = ArrayElement<typeof strings.ALIGNS>;
  1025. export type BaseOnCell<RecordType> = (record?: RecordType, rowIndex?: number) => BaseOnCellReturnObject;
  1026. export interface BaseOnCellReturnObject {
  1027. [x: string]: any;
  1028. style?: Record<string, any>;
  1029. className?: string;
  1030. onClick?: (e: any) => void;
  1031. }
  1032. export type BaseOnFilter<RecordType> = (filteredValue?: any, record?: RecordType) => boolean;
  1033. export type BaseOnFilterDropdownVisibleChange = (visible?: boolean) => void;
  1034. export type BaseOnHeaderCell<RecordType> = (record?: RecordType, columnIndex?: number) => BaseOnHeaderCellReturnObject;
  1035. export interface BaseOnHeaderCellReturnObject {
  1036. [x: string]: any;
  1037. style?: Record<string, any>;
  1038. className?: string;
  1039. onClick?: (e: any) => void;
  1040. }
  1041. export interface BaseChangeInfoSorter<RecordType> {
  1042. [x: string]: any;
  1043. dataIndex: string;
  1044. sortOrder: BaseSortOrder;
  1045. sorter: BaseSorter<RecordType>;
  1046. }
  1047. export type BaseIncludeGroupRecord<RecordType> = RecordType | { groupKey: string };
  1048. export default TableFoundation;