Skip to content

Commit f6bc143

Browse files
feat: added props for supplying current sort column and sort direction from outside of table
1 parent b341d90 commit f6bc143

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

src/DataTable/DataTable.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
121121
onColumnOrderChange = defaultProps.onColumnOrderChange,
122122
keepExpandableFirst = defaultProps.keepExpandableFirst,
123123
footer = defaultProps.footer,
124+
currentSortColumnId = defaultProps.currentSortColumnId,
125+
currentSortDirection = defaultProps.currentSortDirection,
124126
} = props;
125127

126128
const {
@@ -492,6 +494,8 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
492494
onDragEnter={handleDragEnter}
493495
onDragLeave={handleDragLeave}
494496
draggingColumnId={draggingColumnId}
497+
currentSortColumnId={currentSortColumnId}
498+
currentSortDirection={currentSortDirection}
495499
/>
496500
))}
497501
</HeadRow>

src/DataTable/TableCol.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ type TableColProps<T> = {
9898
onDragEnd: (e: React.DragEvent<HTMLDivElement>) => void;
9999
onDragEnter: (e: React.DragEvent<HTMLDivElement>) => void;
100100
onDragLeave: (e: React.DragEvent<HTMLDivElement>) => void;
101+
currentSortColumnId: string | number | null;
102+
currentSortDirection: SortOrder;
101103
};
102104

103105
function TableCol<T>({
@@ -118,6 +120,8 @@ function TableCol<T>({
118120
onDragEnd,
119121
onDragEnter,
120122
onDragLeave,
123+
currentSortColumnId,
124+
currentSortDirection,
121125
}: TableColProps<T>): JSX.Element | null {
122126
React.useEffect(() => {
123127
if (typeof column.selector === 'string') {
@@ -168,14 +172,14 @@ function TableCol<T>({
168172
};
169173

170174
const renderNativeSortIcon = (sortActive: boolean) => (
171-
<NativeSortIcon sortActive={sortActive} sortDirection={sortDirection} />
175+
<NativeSortIcon sortActive={sortActive} sortDirection={currentSortDirection} />
172176
);
173177

174178
const renderCustomSortIcon = () => (
175179
<span className={[sortDirection, '__rdt_custom_sort_icon__'].join(' ')}>{sortIcon}</span>
176180
);
177181

178-
const sortActive = !!(column.sortable && equalizeId(selectedColumn.id, column.id));
182+
const sortActive = !!(column.sortable && currentSortColumnId !== null && equalizeId(currentSortColumnId, column.id));
179183
const disableSort = !column.sortable || disabled;
180184
const nativeSortIconLeft = column.sortable && !sortIcon && !column.right;
181185
const nativeSortIconRight = column.sortable && !sortIcon && column.right;

src/DataTable/defaultProps.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import ExpanderCollapsedIcon from '../icons/ExpanderCollapsedIcon';
77
import ExpanderExpandedIcon from '../icons/ExpanderExpandedIcon';
88
import { noop } from './util';
99
import { Alignment, Direction } from './constants';
10+
import { SortOrder } from './types';
1011

1112
export const defaultProps = {
1213
columns: [],
@@ -108,4 +109,6 @@ export const defaultProps = {
108109
onColumnOrderChange: noop,
109110
keepExpandableFirst: false,
110111
footer: null,
112+
currentSortColumnId: null,
113+
currentSortDirection: 'asc' as SortOrder,
111114
};

src/DataTable/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export type TableProps<T> = {
118118
* */
119119
title?: string | React.ReactNode;
120120
footer?: React.ReactNode;
121+
currentSortColumnId: string | number | null;
122+
currentSortDirection: SortOrder;
121123
};
122124

123125
export type TableColumnBase = {

0 commit comments

Comments
 (0)