2024-03-18 18:45:31 +08:00
|
|
|
import * as React from 'react';
|
2020-03-02 12:09:38 +08:00
|
|
|
import CaretDownOutlined from '@ant-design/icons/CaretDownOutlined';
|
|
|
|
import CaretUpOutlined from '@ant-design/icons/CaretUpOutlined';
|
2022-06-22 14:57:09 +08:00
|
|
|
import classNames from 'classnames';
|
2022-04-27 16:27:13 +08:00
|
|
|
import KeyCode from 'rc-util/lib/KeyCode';
|
2024-03-18 18:45:31 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
import type { AnyObject } from '../../_util/type';
|
2022-06-22 14:57:09 +08:00
|
|
|
import type { TooltipProps } from '../../tooltip';
|
|
|
|
import Tooltip from '../../tooltip';
|
2022-05-07 14:31:54 +08:00
|
|
|
import type {
|
2022-06-22 14:57:09 +08:00
|
|
|
ColumnGroupType,
|
2019-11-15 14:35:25 +08:00
|
|
|
ColumnsType,
|
2022-06-22 14:57:09 +08:00
|
|
|
ColumnTitleProps,
|
2019-11-15 14:35:25 +08:00
|
|
|
ColumnType,
|
|
|
|
CompareFn,
|
2022-06-22 14:57:09 +08:00
|
|
|
Key,
|
2019-11-15 14:35:25 +08:00
|
|
|
SorterResult,
|
2024-03-18 18:45:31 +08:00
|
|
|
SorterTooltipProps,
|
2022-06-22 14:57:09 +08:00
|
|
|
SortOrder,
|
2020-02-29 22:01:13 +08:00
|
|
|
TableLocale,
|
2022-06-22 14:57:09 +08:00
|
|
|
TransformColumns,
|
2019-11-15 14:35:25 +08:00
|
|
|
} from '../interface';
|
2022-11-05 12:44:37 +08:00
|
|
|
import { getColumnKey, getColumnPos, renderColumnTitle, safeColumnTitle } from '../util';
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2020-02-29 22:01:13 +08:00
|
|
|
const ASCEND = 'ascend';
|
|
|
|
const DESCEND = 'descend';
|
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const getMultiplePriority = <RecordType extends AnyObject = AnyObject>(
|
|
|
|
column: ColumnType<RecordType>,
|
|
|
|
): number | false => {
|
2019-11-15 14:35:25 +08:00
|
|
|
if (typeof column.sorter === 'object' && typeof column.sorter.multiple === 'number') {
|
|
|
|
return column.sorter.multiple;
|
|
|
|
}
|
|
|
|
return false;
|
2024-08-11 10:58:13 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const getSortFunction = <RecordType extends AnyObject = AnyObject>(
|
2019-11-15 14:35:25 +08:00
|
|
|
sorter: ColumnType<RecordType>['sorter'],
|
2024-08-11 10:58:13 +08:00
|
|
|
): CompareFn<RecordType> | false => {
|
2019-11-15 14:35:25 +08:00
|
|
|
if (typeof sorter === 'function') {
|
|
|
|
return sorter;
|
|
|
|
}
|
|
|
|
if (sorter && typeof sorter === 'object' && sorter.compare) {
|
|
|
|
return sorter.compare;
|
|
|
|
}
|
|
|
|
return false;
|
2024-08-11 10:58:13 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const nextSortDirection = (sortDirections: SortOrder[], current: SortOrder | null) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
if (!current) {
|
|
|
|
return sortDirections[0];
|
|
|
|
}
|
|
|
|
return sortDirections[sortDirections.indexOf(current) + 1];
|
2024-08-11 10:58:13 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-13 10:34:52 +08:00
|
|
|
export interface SortState<RecordType = AnyObject> {
|
2019-11-15 14:35:25 +08:00
|
|
|
column: ColumnType<RecordType>;
|
|
|
|
key: Key;
|
|
|
|
sortOrder: SortOrder | null;
|
|
|
|
multiplePriority: number | false;
|
|
|
|
}
|
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const collectSortStates = <RecordType extends AnyObject = AnyObject>(
|
2019-11-15 14:35:25 +08:00
|
|
|
columns: ColumnsType<RecordType>,
|
|
|
|
init: boolean,
|
|
|
|
pos?: string,
|
2024-08-11 10:58:13 +08:00
|
|
|
): SortState<RecordType>[] => {
|
2019-11-15 14:35:25 +08:00
|
|
|
let sortStates: SortState<RecordType>[] = [];
|
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const pushState = (column: ColumnsType<RecordType>[number], columnPos: string) => {
|
2020-03-20 23:33:14 +08:00
|
|
|
sortStates.push({
|
|
|
|
column,
|
2024-08-11 10:58:13 +08:00
|
|
|
key: getColumnKey<RecordType>(column, columnPos),
|
|
|
|
multiplePriority: getMultiplePriority<RecordType>(column),
|
2020-03-20 23:33:14 +08:00
|
|
|
sortOrder: column.sortOrder!,
|
|
|
|
});
|
2024-08-11 10:58:13 +08:00
|
|
|
};
|
2020-03-20 23:33:14 +08:00
|
|
|
|
2020-01-06 20:28:47 +08:00
|
|
|
(columns || []).forEach((column, index) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
const columnPos = getColumnPos(index, pos);
|
2020-03-20 23:33:14 +08:00
|
|
|
if ((column as ColumnGroupType<RecordType>).children) {
|
|
|
|
if ('sortOrder' in column) {
|
|
|
|
// Controlled
|
|
|
|
pushState(column, columnPos);
|
|
|
|
}
|
|
|
|
sortStates = [
|
|
|
|
...sortStates,
|
2024-08-11 10:58:13 +08:00
|
|
|
...collectSortStates<RecordType>(
|
|
|
|
(column as ColumnGroupType<RecordType>).children,
|
|
|
|
init,
|
|
|
|
columnPos,
|
|
|
|
),
|
2020-03-20 23:33:14 +08:00
|
|
|
];
|
2019-12-11 14:14:52 +08:00
|
|
|
} else if (column.sorter) {
|
2019-11-15 14:35:25 +08:00
|
|
|
if ('sortOrder' in column) {
|
|
|
|
// Controlled
|
2020-03-20 23:33:14 +08:00
|
|
|
pushState(column, columnPos);
|
2019-11-15 14:35:25 +08:00
|
|
|
} else if (init && column.defaultSortOrder) {
|
|
|
|
// Default sorter
|
|
|
|
sortStates.push({
|
|
|
|
column,
|
|
|
|
key: getColumnKey(column, columnPos),
|
2024-08-11 10:58:13 +08:00
|
|
|
multiplePriority: getMultiplePriority<RecordType>(column),
|
2019-11-15 14:35:25 +08:00
|
|
|
sortOrder: column.defaultSortOrder!,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return sortStates;
|
2024-08-11 10:58:13 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const injectSorter = <RecordType extends AnyObject = AnyObject>(
|
2019-11-15 14:35:25 +08:00
|
|
|
prefixCls: string,
|
|
|
|
columns: ColumnsType<RecordType>,
|
2022-04-27 16:27:13 +08:00
|
|
|
sorterStates: SortState<RecordType>[],
|
2019-11-15 14:35:25 +08:00
|
|
|
triggerSorter: (sorterSates: SortState<RecordType>) => void,
|
|
|
|
defaultSortDirections: SortOrder[],
|
2020-02-29 22:01:13 +08:00
|
|
|
tableLocale?: TableLocale,
|
2024-03-18 18:45:31 +08:00
|
|
|
tableShowSorterTooltip?: boolean | SorterTooltipProps,
|
2019-11-15 14:35:25 +08:00
|
|
|
pos?: string,
|
2024-08-11 10:58:13 +08:00
|
|
|
): ColumnsType<RecordType> => {
|
|
|
|
const finalColumns = (columns || []).map((column, index) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
const columnPos = getColumnPos(index, pos);
|
|
|
|
let newColumn: ColumnsType<RecordType>[number] = column;
|
2019-12-11 14:14:52 +08:00
|
|
|
if (newColumn.sorter) {
|
2019-11-15 14:35:25 +08:00
|
|
|
const sortDirections: SortOrder[] = newColumn.sortDirections || defaultSortDirections;
|
2020-02-29 22:01:13 +08:00
|
|
|
const showSorterTooltip =
|
|
|
|
newColumn.showSorterTooltip === undefined
|
|
|
|
? tableShowSorterTooltip
|
|
|
|
: newColumn.showSorterTooltip;
|
2024-03-18 18:45:31 +08:00
|
|
|
|
2019-11-15 14:35:25 +08:00
|
|
|
const columnKey = getColumnKey(newColumn, columnPos);
|
2022-04-27 16:27:13 +08:00
|
|
|
const sorterState = sorterStates.find(({ key }) => key === columnKey);
|
2023-05-22 18:03:45 +08:00
|
|
|
const sortOrder = sorterState ? sorterState.sortOrder : null;
|
|
|
|
const nextSortOrder = nextSortDirection(sortDirections, sortOrder);
|
2023-05-22 12:03:57 +08:00
|
|
|
|
|
|
|
let sorter: React.ReactNode;
|
|
|
|
if (column.sortIcon) {
|
2023-05-22 18:03:45 +08:00
|
|
|
sorter = column.sortIcon({ sortOrder });
|
2023-05-22 12:03:57 +08:00
|
|
|
} else {
|
|
|
|
const upNode: React.ReactNode = sortDirections.includes(ASCEND) && (
|
|
|
|
<CaretUpOutlined
|
|
|
|
className={classNames(`${prefixCls}-column-sorter-up`, {
|
2023-05-22 18:03:45 +08:00
|
|
|
active: sortOrder === ASCEND,
|
2023-05-22 12:03:57 +08:00
|
|
|
})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
const downNode: React.ReactNode = sortDirections.includes(DESCEND) && (
|
|
|
|
<CaretDownOutlined
|
|
|
|
className={classNames(`${prefixCls}-column-sorter-down`, {
|
2023-05-22 18:03:45 +08:00
|
|
|
active: sortOrder === DESCEND,
|
2023-05-22 12:03:57 +08:00
|
|
|
})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
sorter = (
|
|
|
|
<span
|
|
|
|
className={classNames(`${prefixCls}-column-sorter`, {
|
|
|
|
[`${prefixCls}-column-sorter-full`]: !!(upNode && downNode),
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<span className={`${prefixCls}-column-sorter-inner`} aria-hidden="true">
|
|
|
|
{upNode}
|
|
|
|
{downNode}
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-29 22:01:13 +08:00
|
|
|
const { cancelSort, triggerAsc, triggerDesc } = tableLocale || {};
|
|
|
|
let sortTip: string | undefined = cancelSort;
|
|
|
|
if (nextSortOrder === DESCEND) {
|
|
|
|
sortTip = triggerDesc;
|
|
|
|
} else if (nextSortOrder === ASCEND) {
|
|
|
|
sortTip = triggerAsc;
|
|
|
|
}
|
2021-02-02 22:05:31 +08:00
|
|
|
const tooltipProps: TooltipProps =
|
2023-10-19 10:08:10 +08:00
|
|
|
typeof showSorterTooltip === 'object'
|
|
|
|
? {
|
|
|
|
title: sortTip,
|
|
|
|
...showSorterTooltip,
|
|
|
|
}
|
|
|
|
: { title: sortTip };
|
2019-11-15 14:35:25 +08:00
|
|
|
newColumn = {
|
|
|
|
...newColumn,
|
2023-05-22 18:03:45 +08:00
|
|
|
className: classNames(newColumn.className, { [`${prefixCls}-column-sort`]: sortOrder }),
|
2020-02-29 22:01:13 +08:00
|
|
|
title: (renderProps: ColumnTitleProps<RecordType>) => {
|
2024-03-18 18:45:31 +08:00
|
|
|
const columnSortersClass = `${prefixCls}-column-sorters`;
|
|
|
|
const renderColumnTitleWrapper = (
|
|
|
|
<span className={`${prefixCls}-column-title`}>
|
|
|
|
{renderColumnTitle(column.title, renderProps)}
|
|
|
|
</span>
|
|
|
|
);
|
2020-02-29 22:01:13 +08:00
|
|
|
const renderSortTitle = (
|
2024-03-18 18:45:31 +08:00
|
|
|
<div className={columnSortersClass}>
|
|
|
|
{renderColumnTitleWrapper}
|
2023-05-22 12:03:57 +08:00
|
|
|
{sorter}
|
2020-02-29 22:01:13 +08:00
|
|
|
</div>
|
|
|
|
);
|
2024-03-18 18:45:31 +08:00
|
|
|
if (showSorterTooltip) {
|
|
|
|
if (
|
|
|
|
typeof showSorterTooltip !== 'boolean' &&
|
|
|
|
showSorterTooltip?.target === 'sorter-icon'
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={`${columnSortersClass} ${prefixCls}-column-sorters-tooltip-target-sorter`}
|
|
|
|
>
|
|
|
|
{renderColumnTitleWrapper}
|
|
|
|
<Tooltip {...tooltipProps}>{sorter}</Tooltip>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return <Tooltip {...tooltipProps}>{renderSortTitle}</Tooltip>;
|
|
|
|
}
|
|
|
|
return renderSortTitle;
|
2020-02-29 22:01:13 +08:00
|
|
|
},
|
2022-11-19 13:47:33 +08:00
|
|
|
onHeaderCell: (col) => {
|
2024-06-22 21:59:12 +08:00
|
|
|
const cell: React.HTMLAttributes<HTMLElement> = column.onHeaderCell?.(col) || {};
|
2019-11-15 14:35:25 +08:00
|
|
|
const originOnClick = cell.onClick;
|
2022-04-27 16:27:13 +08:00
|
|
|
const originOKeyDown = cell.onKeyDown;
|
2019-11-15 14:35:25 +08:00
|
|
|
cell.onClick = (event: React.MouseEvent<HTMLElement>) => {
|
|
|
|
triggerSorter({
|
|
|
|
column,
|
|
|
|
key: columnKey,
|
2020-02-29 22:01:13 +08:00
|
|
|
sortOrder: nextSortOrder,
|
2024-08-11 10:58:13 +08:00
|
|
|
multiplePriority: getMultiplePriority<RecordType>(column),
|
2019-11-15 14:35:25 +08:00
|
|
|
});
|
2022-04-27 16:27:13 +08:00
|
|
|
originOnClick?.(event);
|
|
|
|
};
|
|
|
|
cell.onKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
|
|
|
|
if (event.keyCode === KeyCode.ENTER) {
|
|
|
|
triggerSorter({
|
|
|
|
column,
|
|
|
|
key: columnKey,
|
|
|
|
sortOrder: nextSortOrder,
|
2024-08-11 10:58:13 +08:00
|
|
|
multiplePriority: getMultiplePriority<RecordType>(column),
|
2022-04-27 16:27:13 +08:00
|
|
|
});
|
|
|
|
originOKeyDown?.(event);
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-05 12:44:37 +08:00
|
|
|
const renderTitle = safeColumnTitle(column.title, {});
|
|
|
|
const displayTitle = renderTitle?.toString();
|
|
|
|
|
2022-01-11 13:18:42 +08:00
|
|
|
// Inform the screen-reader so it can tell the visually impaired user which column is sorted
|
2023-05-22 18:03:45 +08:00
|
|
|
if (sortOrder) {
|
|
|
|
cell['aria-sort'] = sortOrder === 'ascend' ? 'ascending' : 'descending';
|
2022-08-21 16:39:54 +08:00
|
|
|
} else {
|
2022-12-22 20:47:46 +08:00
|
|
|
cell['aria-label'] = displayTitle || '';
|
2022-01-11 13:18:42 +08:00
|
|
|
}
|
2019-11-15 14:35:25 +08:00
|
|
|
cell.className = classNames(cell.className, `${prefixCls}-column-has-sorters`);
|
2022-04-27 16:27:13 +08:00
|
|
|
cell.tabIndex = 0;
|
2022-10-13 17:44:19 +08:00
|
|
|
if (column.ellipsis) {
|
2022-11-05 12:44:37 +08:00
|
|
|
cell.title = (renderTitle ?? '').toString();
|
2022-10-13 17:44:19 +08:00
|
|
|
}
|
2019-11-15 14:35:25 +08:00
|
|
|
return cell;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('children' in newColumn) {
|
|
|
|
newColumn = {
|
|
|
|
...newColumn,
|
|
|
|
children: injectSorter(
|
|
|
|
prefixCls,
|
|
|
|
newColumn.children,
|
2022-04-27 16:27:13 +08:00
|
|
|
sorterStates,
|
2019-11-15 14:35:25 +08:00
|
|
|
triggerSorter,
|
|
|
|
defaultSortDirections,
|
2020-02-29 22:01:13 +08:00
|
|
|
tableLocale,
|
|
|
|
tableShowSorterTooltip,
|
2019-11-15 14:35:25 +08:00
|
|
|
columnPos,
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return newColumn;
|
|
|
|
});
|
2024-08-11 10:58:13 +08:00
|
|
|
return finalColumns;
|
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const stateToInfo = <RecordType extends AnyObject = AnyObject>(
|
2024-03-23 21:59:55 +08:00
|
|
|
sorterStates: SortState<RecordType>,
|
|
|
|
): SorterResult<RecordType> => {
|
2019-11-15 14:35:25 +08:00
|
|
|
const { column, sortOrder } = sorterStates;
|
2024-03-23 23:27:59 +08:00
|
|
|
return {
|
|
|
|
column,
|
|
|
|
order: sortOrder,
|
|
|
|
field: column.dataIndex as SorterResult<RecordType>['field'],
|
|
|
|
columnKey: column.key,
|
|
|
|
};
|
2024-03-23 21:59:55 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const generateSorterInfo = <RecordType extends AnyObject = AnyObject>(
|
2019-11-15 14:35:25 +08:00
|
|
|
sorterStates: SortState<RecordType>[],
|
2024-03-23 23:27:59 +08:00
|
|
|
): SorterResult<RecordType> | SorterResult<RecordType>[] => {
|
2024-03-23 21:59:55 +08:00
|
|
|
const list = sorterStates
|
|
|
|
.filter(({ sortOrder }) => sortOrder)
|
|
|
|
.map<SorterResult<RecordType>>(stateToInfo);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
// =========== Legacy compatible support ===========
|
|
|
|
// https://github.com/ant-design/ant-design/pull/19226
|
|
|
|
if (list.length === 0 && sorterStates.length) {
|
2024-03-23 21:59:55 +08:00
|
|
|
const lastIndex = sorterStates.length - 1;
|
2019-11-15 14:35:25 +08:00
|
|
|
return {
|
2024-03-23 21:59:55 +08:00
|
|
|
...stateToInfo(sorterStates[lastIndex]),
|
2019-11-15 14:35:25 +08:00
|
|
|
column: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-17 10:13:25 +08:00
|
|
|
if (list.length <= 1) {
|
|
|
|
return list[0] || {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
2024-03-23 23:27:59 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
export const getSortData = <RecordType extends AnyObject = AnyObject>(
|
2021-01-28 01:21:58 +08:00
|
|
|
data: readonly RecordType[],
|
2019-11-15 14:35:25 +08:00
|
|
|
sortStates: SortState<RecordType>[],
|
|
|
|
childrenColumnName: string,
|
2024-08-11 10:58:13 +08:00
|
|
|
): RecordType[] => {
|
2019-11-15 14:35:25 +08:00
|
|
|
const innerSorterStates = sortStates
|
|
|
|
.slice()
|
|
|
|
.sort((a, b) => (b.multiplePriority as number) - (a.multiplePriority as number));
|
|
|
|
|
|
|
|
const cloneData = data.slice();
|
|
|
|
|
2020-12-09 17:12:32 +08:00
|
|
|
const runningSorters = innerSorterStates.filter(
|
2024-08-11 10:58:13 +08:00
|
|
|
({ column: { sorter }, sortOrder }) => getSortFunction<RecordType>(sorter) && sortOrder,
|
2020-12-09 17:12:32 +08:00
|
|
|
);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
// Skip if no sorter needed
|
|
|
|
if (!runningSorters.length) {
|
|
|
|
return cloneData;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cloneData
|
|
|
|
.sort((record1, record2) => {
|
|
|
|
for (let i = 0; i < runningSorters.length; i += 1) {
|
|
|
|
const sorterState = runningSorters[i];
|
|
|
|
const {
|
|
|
|
column: { sorter },
|
|
|
|
sortOrder,
|
|
|
|
} = sorterState;
|
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const compareFn = getSortFunction<RecordType>(sorter);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
if (compareFn && sortOrder) {
|
|
|
|
const compareResult = compareFn(record1, record2, sortOrder);
|
|
|
|
|
|
|
|
if (compareResult !== 0) {
|
2020-02-29 22:01:13 +08:00
|
|
|
return sortOrder === ASCEND ? compareResult : -compareResult;
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
})
|
2022-11-19 13:47:33 +08:00
|
|
|
.map<RecordType>((record) => {
|
2024-08-11 10:58:13 +08:00
|
|
|
const subRecords = record[childrenColumnName];
|
2019-11-15 14:35:25 +08:00
|
|
|
if (subRecords) {
|
|
|
|
return {
|
|
|
|
...record,
|
2024-08-11 10:58:13 +08:00
|
|
|
[childrenColumnName]: getSortData<RecordType>(subRecords, sortStates, childrenColumnName),
|
2019-11-15 14:35:25 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return record;
|
|
|
|
});
|
2024-08-11 10:58:13 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-13 10:34:52 +08:00
|
|
|
interface SorterConfig<RecordType = AnyObject> {
|
2019-11-15 14:35:25 +08:00
|
|
|
prefixCls: string;
|
2020-04-24 12:17:42 +08:00
|
|
|
mergedColumns: ColumnsType<RecordType>;
|
2019-11-15 14:35:25 +08:00
|
|
|
onSorterChange: (
|
2022-08-17 10:13:25 +08:00
|
|
|
sorterResult: SorterResult<RecordType> | SorterResult<RecordType>[],
|
2019-11-15 14:35:25 +08:00
|
|
|
sortStates: SortState<RecordType>[],
|
|
|
|
) => void;
|
|
|
|
sortDirections: SortOrder[];
|
2020-02-29 22:01:13 +08:00
|
|
|
tableLocale?: TableLocale;
|
2024-03-18 18:45:31 +08:00
|
|
|
showSorterTooltip?: boolean | SorterTooltipProps;
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
const useFilterSorter = <RecordType extends AnyObject = AnyObject>(
|
|
|
|
props: SorterConfig<RecordType>,
|
|
|
|
): [
|
2019-11-15 14:35:25 +08:00
|
|
|
TransformColumns<RecordType>,
|
|
|
|
SortState<RecordType>[],
|
|
|
|
ColumnTitleProps<RecordType>,
|
2022-08-17 10:13:25 +08:00
|
|
|
() => SorterResult<RecordType> | SorterResult<RecordType>[],
|
2024-08-11 10:58:13 +08:00
|
|
|
] => {
|
|
|
|
const {
|
|
|
|
prefixCls,
|
|
|
|
mergedColumns,
|
|
|
|
sortDirections,
|
|
|
|
tableLocale,
|
|
|
|
showSorterTooltip,
|
|
|
|
onSorterChange,
|
|
|
|
} = props;
|
|
|
|
|
2019-11-15 14:35:25 +08:00
|
|
|
const [sortStates, setSortStates] = React.useState<SortState<RecordType>[]>(
|
2024-08-11 10:58:13 +08:00
|
|
|
collectSortStates<RecordType>(mergedColumns, true),
|
2019-11-15 14:35:25 +08:00
|
|
|
);
|
|
|
|
|
2024-08-01 20:39:01 +08:00
|
|
|
const getColumnKeys = (columns: ColumnsType<RecordType>, pos?: string): Key[] => {
|
|
|
|
const newKeys: Key[] = [];
|
|
|
|
columns.forEach((item, index) => {
|
|
|
|
const columnPos = getColumnPos(index, pos);
|
2024-08-11 10:58:13 +08:00
|
|
|
newKeys.push(getColumnKey<RecordType>(item, columnPos));
|
2024-08-01 20:39:01 +08:00
|
|
|
if (Array.isArray((item as ColumnGroupType<RecordType>).children)) {
|
|
|
|
const childKeys = getColumnKeys((item as ColumnGroupType<RecordType>).children, columnPos);
|
|
|
|
newKeys.push(...childKeys);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return newKeys;
|
|
|
|
};
|
2024-03-23 21:59:55 +08:00
|
|
|
const mergedSorterStates = React.useMemo<SortState<RecordType>[]>(() => {
|
2019-11-15 14:35:25 +08:00
|
|
|
let validate = true;
|
2024-08-11 10:58:13 +08:00
|
|
|
const collectedStates = collectSortStates<RecordType>(mergedColumns, false);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
// Return if not controlled
|
|
|
|
if (!collectedStates.length) {
|
2024-08-01 20:39:01 +08:00
|
|
|
const mergedColumnsKeys = getColumnKeys(mergedColumns);
|
2024-07-18 21:38:17 +08:00
|
|
|
return sortStates.filter(({ key }) => mergedColumnsKeys.includes(key));
|
2019-11-15 14:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const validateStates: SortState<RecordType>[] = [];
|
|
|
|
|
|
|
|
function patchStates(state: SortState<RecordType>) {
|
|
|
|
if (validate) {
|
|
|
|
validateStates.push(state);
|
|
|
|
} else {
|
|
|
|
validateStates.push({
|
|
|
|
...state,
|
|
|
|
sortOrder: null,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let multipleMode: boolean | null = null;
|
2022-11-19 13:47:33 +08:00
|
|
|
collectedStates.forEach((state) => {
|
2019-11-15 14:35:25 +08:00
|
|
|
if (multipleMode === null) {
|
|
|
|
patchStates(state);
|
|
|
|
|
|
|
|
if (state.sortOrder) {
|
|
|
|
if (state.multiplePriority === false) {
|
|
|
|
validate = false;
|
|
|
|
} else {
|
|
|
|
multipleMode = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (multipleMode && state.multiplePriority !== false) {
|
|
|
|
patchStates(state);
|
|
|
|
} else {
|
|
|
|
validate = false;
|
|
|
|
patchStates(state);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return validateStates;
|
2020-03-01 13:11:28 +08:00
|
|
|
}, [mergedColumns, sortStates]);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
|
|
|
// Get render columns title required props
|
|
|
|
const columnTitleSorterProps = React.useMemo<ColumnTitleProps<RecordType>>(() => {
|
|
|
|
const sortColumns = mergedSorterStates.map(({ column, sortOrder }) => ({
|
|
|
|
column,
|
|
|
|
order: sortOrder,
|
|
|
|
}));
|
|
|
|
|
|
|
|
return {
|
|
|
|
sortColumns,
|
|
|
|
// Legacy
|
2024-06-22 21:59:12 +08:00
|
|
|
sortColumn: sortColumns[0]?.column,
|
|
|
|
sortOrder: sortColumns[0]?.order,
|
2019-11-15 14:35:25 +08:00
|
|
|
};
|
|
|
|
}, [mergedSorterStates]);
|
|
|
|
|
2024-03-23 21:59:55 +08:00
|
|
|
const triggerSorter = (sortState: SortState<RecordType>) => {
|
|
|
|
let newSorterStates: SortState<RecordType>[];
|
2019-11-15 14:35:25 +08:00
|
|
|
if (
|
|
|
|
sortState.multiplePriority === false ||
|
|
|
|
!mergedSorterStates.length ||
|
|
|
|
mergedSorterStates[0].multiplePriority === false
|
|
|
|
) {
|
|
|
|
newSorterStates = [sortState];
|
|
|
|
} else {
|
|
|
|
newSorterStates = [
|
|
|
|
...mergedSorterStates.filter(({ key }) => key !== sortState.key),
|
|
|
|
sortState,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
setSortStates(newSorterStates);
|
|
|
|
onSorterChange(generateSorterInfo(newSorterStates), newSorterStates);
|
2024-03-23 21:59:55 +08:00
|
|
|
};
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2020-01-13 15:04:27 +08:00
|
|
|
const transformColumns = (innerColumns: ColumnsType<RecordType>) =>
|
2020-02-29 22:01:13 +08:00
|
|
|
injectSorter(
|
|
|
|
prefixCls,
|
|
|
|
innerColumns,
|
|
|
|
mergedSorterStates,
|
|
|
|
triggerSorter,
|
|
|
|
sortDirections,
|
|
|
|
tableLocale,
|
|
|
|
showSorterTooltip,
|
|
|
|
);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2020-12-09 17:12:32 +08:00
|
|
|
const getSorters = () => generateSorterInfo(mergedSorterStates);
|
2019-11-15 14:35:25 +08:00
|
|
|
|
2024-08-11 10:58:13 +08:00
|
|
|
return [transformColumns, mergedSorterStates, columnTitleSorterProps, getSorters] as const;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useFilterSorter;
|