Skip to content

Commit

Permalink
feat: table default sorting prop
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Dec 6, 2020
1 parent 2962c3c commit e5451f3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ui/components/src/Table/Table.stories.tsx
Expand Up @@ -74,7 +74,17 @@ export const sortable: Example = () => {
const data = useMemo(mockData, []);
return (
<ThemeProvider>
<Table sorting={true} columns={columns} data={data} />
<Table
sorting={true}
columns={columns}
data={data}
sortBy={[
{
id: 'age',
desc: true,
},
]}
/>
</ThemeProvider>
);
};
Expand Down
12 changes: 12 additions & 0 deletions ui/components/src/Table/Table.tsx
Expand Up @@ -25,6 +25,8 @@ import {
UseGroupByRowProps,
UseExpandedState,
UseRowSelectState,
UseSortByState,
SortingRule,
UseGroupByState,
TableState,
} from 'react-table';
Expand Down Expand Up @@ -103,6 +105,11 @@ interface TableOwnProps {
* callback to render a SubComponent row
*/
renderRowSubComponent?: (props: { row: Row }) => ReactNode;

/**
* initial sorting
*/
sortBy?: Array<SortingRule<any>>;
}

export type TableProps = TableOwnProps & BoxProps;
Expand All @@ -126,6 +133,7 @@ export const Table: FC<TableProps> = ({
initialSelected = {},
onSelectRowsChange,
rowSelect,
sortBy,
...rest
}) => {
const plugins = [
Expand All @@ -143,13 +151,17 @@ export const Table: FC<TableProps> = ({
const initialState: Partial<TableState<Record<string, unknown>>> &
Partial<UseExpandedState<Record<string, unknown>>> &
Partial<UseGroupByState<Record<string, unknown>>> &
Partial<UseSortByState<Record<string, unknown>>> &
Partial<UseRowSelectState<Record<string, unknown>>> = {};
if (Array.isArray(groupBy)) {
initialState.groupBy = groupBy;
initialState.hiddenColumns = hiddenColumns || groupBy;
} else if (hiddenColumns !== undefined) {
initialState.hiddenColumns = hiddenColumns;
}
if (Array.isArray(sortBy)) {
initialState.sortBy = sortBy;
}
if (typeof expanded === 'object') {
initialState.expanded = expanded;
}
Expand Down

0 comments on commit e5451f3

Please sign in to comment.