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

Remove server side tag column sorting #1735

Closed
wants to merge 1 commit into from
Closed
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
65 changes: 40 additions & 25 deletions src/components/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type TableColumnInternal<T> = TableBaseColumn<T> & TableColumnState;
const getTagTableColumn = <T extends {}>(
columnState: TagTableColumnState,
tagField: Extract<keyof T, string>,
pagination?: Pagination,
) => {
const column: TableColumnInternal<T> = {
...columnState,
Expand All @@ -171,32 +172,34 @@ const getTagTableColumn = <T extends {}>(
field: `${tagField}.${columnState.tagKey}` as Extract<keyof T, string>,
cellAttributes: tagCellAttributes,
};
column.sortable = (a: T, b: T) => {
const tagKey = column.tagKey;
if (!tagKey) {
return 0;
}
column.sortable = pagination?.serverSide
? false
: (a: T, b: T) => {
const tagKey = column.tagKey;
if (!tagKey) {
return 0;
}

const item1tag = findTagOfTaggedResource(a, tagField, tagKey);
const item2tag = findTagOfTaggedResource(b, tagField, tagKey);
const item1tag = findTagOfTaggedResource(a, tagField, tagKey);
const item2tag = findTagOfTaggedResource(b, tagField, tagKey);

// first compare the objects
// so that we differentiate not having a value
// with not having the tag at all
if (!item1tag && !item2tag) {
return 0;
}
// first compare the objects
// so that we differentiate not having a value
// with not having the tag at all
if (!item1tag && !item2tag) {
return 0;
}

if (!item1tag) {
return 1;
}
if (!item1tag) {
return 1;
}

if (!item2tag) {
return -1;
}
if (!item2tag) {
return -1;
}

return (item1tag.value || '').localeCompare(item2tag.value || '');
};
return (item1tag.value || '').localeCompare(item2tag.value || '');
};
column.render = (_value: any, data: T) => {
const tagKey = column.tagKey;
if (!tagKey) {
Expand Down Expand Up @@ -323,6 +326,7 @@ const applyColumnPreferences = <T extends {}>(
loadedColumns: TableColumnState[] | undefined,
tagField: Extract<keyof T, string> | undefined,
enableCustomColumns?: boolean,
pagination?: Pagination,
): Array<TableColumnInternal<T>> => {
if (!loadedColumns?.length) {
return columns;
Expand Down Expand Up @@ -352,7 +356,7 @@ const applyColumnPreferences = <T extends {}>(
if (tagField) {
const loadedTagColumns = filter(loadedColumns, isCustomTagColumn);
const tagColumns = map(loadedTagColumns, (c) =>
getTagTableColumn<T>(c, tagField),
getTagTableColumn<T>(c, tagField, pagination),
);
insertTagColumns(columns, tagColumns);
}
Expand All @@ -365,8 +369,13 @@ const applyColumnPreferences = <T extends {}>(
};

const addCustomColumns = <T extends {}>(props: TableProps<T>) => {
const { columns, tagField, enableCustomColumns, columnStateRestorationKey } =
props;
const {
columns,
tagField,
enableCustomColumns,
columnStateRestorationKey,
pagination,
} = props;
let allColumns = columns.map((column) =>
normalizeTableColumn(column, enableCustomColumns),
);
Expand All @@ -386,6 +395,7 @@ const addCustomColumns = <T extends {}>(props: TableProps<T>) => {
loadedColumns,
tagField,
enableCustomColumns,
pagination,
);
return allColumns;
};
Expand Down Expand Up @@ -640,7 +650,11 @@ export class Table<T extends {}> extends React.Component<
const newColumnState = getNewTagTableColumnState(tagKey);

const columns = allColumns.slice();
const newTagColumn = getTagTableColumn(newColumnState, this.props.tagField);
const newTagColumn = getTagTableColumn(
newColumnState,
this.props.tagField,
this.props.pagination,
);
this.setTagTableColumnHeader(
{ tagKeys, selectedTagColumnKeys },
newTagColumn,
Expand Down Expand Up @@ -680,6 +694,7 @@ export class Table<T extends {}> extends React.Component<
const newColumn = getTagTableColumn(
getNewTagTableColumnState(tagKey),
this.props.tagField,
this.props.pagination,
);
const columns = this.state.allColumns.slice();
columns.splice(indexOfColumn, 1, newColumn);
Expand Down
Loading