Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(withTableSorting): add control the use of data sorting #1032

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/components/Table/hoc/withTableSorting/withTableSorting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface WithTableSortingProps {
defaultSortState?: SortState;
sortState?: SortState;
onSortStateChange?: (sortState: SortState) => void;
disableDataSorting?: boolean;
}

interface WithTableSortingState {
Expand Down Expand Up @@ -73,10 +74,10 @@ export function withTableSorting<I extends TableDataItem, E extends {} = {}>(
}

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;
}

Expand Down Expand Up @@ -195,11 +196,13 @@ export function withTableSorting<I extends TableDataItem, E extends {} = {}>(
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() {
Expand Down
Loading