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

EPMRPP-92222 || Update sorting for the table component #22

Merged
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/components/table/table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const Default: Story = {
<Table
{...args}
data={tableData}
sortableColumns={[primaryColumn.key]}
onChangeSorting={(sortConfigParam = sortConfig) => {
let { direction } = sortConfigParam;
const { key } = sortConfigParam;
Expand Down
4 changes: 4 additions & 0 deletions src/components/table/table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import classNames from 'classnames/bind';
import { ArrowDownIcon, ArrowUpIcon } from '@components/icons';
import { FixedColumn, PrimaryColumn, RowData, TableComponentProps, SortDirection } from './types';
import { Checkbox } from '@components/checkbox';
import { getColumnsKeys } from './utils';

const cx = classNames.bind(styles);

Expand All @@ -17,6 +18,7 @@ export const Table: FC<TableComponentProps> = ({
selectedRowIds = [],
sortingDirection = SortDirection.ASC,
sortingColumn = primaryColumn,
sortableColumns = getColumnsKeys([primaryColumn, ...fixedColumns]),
onChangeSorting = () => {},
onToggleRowSelection = () => {},
onToggleAllRowsSelection = () => {},
Expand All @@ -29,6 +31,7 @@ export const Table: FC<TableComponentProps> = ({
}, [primaryColumn, fixedColumns]);

const handleSort = (key: string) => {
if (!sortableColumns.includes(key)) return;
onChangeSorting({ key, direction: sortingDirection });
};

Expand Down Expand Up @@ -72,6 +75,7 @@ export const Table: FC<TableComponentProps> = ({
};

const getSortIcon = (columnKey: string) => {
if (!sortableColumns.includes(columnKey)) return;
if (sortingColumn?.key === columnKey) {
return sortingDirection === SortDirection.ASC ? <ArrowUpIcon /> : <ArrowDownIcon />;
}
Expand Down
1 change: 1 addition & 0 deletions src/components/table/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
}
export interface RowData {
id: string | number;
[key: string]: DetailedCellData | RowConfigs | string | number | any;

Check warning on line 23 in src/components/table/types.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
rowConfigs?: RowConfigs;
}
export enum SortDirection {
Expand All @@ -41,6 +41,7 @@
selectedRowIds?: (string | number)[];
sortingDirection?: SortDirection;
sortingColumn?: Column;
sortableColumns?: string[];
onChangeSorting?: (sortConfig?: SortConfig) => void;
onToggleRowSelection?: (id: string | number) => void;
onToggleAllRowsSelection?: () => void;
Expand Down
6 changes: 5 additions & 1 deletion src/components/table/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RowData, SortConfig, SortDirection } from './types';
import { Column, RowData, SortConfig, SortDirection } from './types';

export const sortTableData = (tableData: RowData[], sortConfig?: SortConfig): RowData[] => {
if (sortConfig) {
Expand All @@ -17,3 +17,7 @@ export const sortTableData = (tableData: RowData[], sortConfig?: SortConfig): Ro
}
return tableData;
};

export const getColumnsKeys = (columns: Column[]): string[] => {
return columns.map((column) => column.key);
};
Loading