From 53248c967e32bba6cb5f4451f9ee5bf00d963163 Mon Sep 17 00:00:00 2001 From: Dmitrii Selianin Date: Wed, 4 Oct 2023 15:24:07 +0200 Subject: [PATCH] feat(withTableSorting): add control the use of data sorting (#1032) --- .../Table/hoc/withTableSorting/withTableSorting.tsx | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/Table/hoc/withTableSorting/withTableSorting.tsx b/src/components/Table/hoc/withTableSorting/withTableSorting.tsx index 8a287bd97f..994fd13b09 100644 --- a/src/components/Table/hoc/withTableSorting/withTableSorting.tsx +++ b/src/components/Table/hoc/withTableSorting/withTableSorting.tsx @@ -28,6 +28,7 @@ export interface WithTableSortingProps { defaultSortState?: SortState; sortState?: SortState; onSortStateChange?: (sortState: SortState) => void; + disableDataSorting?: boolean; } interface WithTableSortingState { @@ -73,10 +74,10 @@ export function withTableSorting( } private getSortedData() { - const {data, columns} = this.props; + const {data, columns, disableDataSorting = this.isControlledState()} = this.props; const sortState = this.getSortState(); - if (this.isControlledState() || sortState.length === 0) { + if (disableDataSorting || sortState.length === 0) { return data; } @@ -195,11 +196,13 @@ export function withTableSorting( private handleSortStateChange(newSortState: SortState) { const {onSortStateChange} = this.props; - if (this.isControlledState()) { - onSortStateChange!(newSortState); - } else { + if (!this.isControlledState()) { this.setState({sort: newSortState}); } + + if (onSortStateChange) { + onSortStateChange(newSortState); + } } private isControlledState() {