19231: Add smaller page sizes (10 and 20 items) to load faster
[arvados-workbench2.git] / src / components / data-table / data-column.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from "react";
6 import { DataTableFilters } from "../data-table-filters/data-table-filters-tree";
7 import { createTree } from 'models/tree';
8
9 export interface DataColumn<T> {
10     key?: React.Key;
11     name: string;
12     selected: boolean;
13     configurable: boolean;
14
15     /**
16      * If set to true, filters on this column will be displayed in a
17      * radio group and only one filter can be selected at a time.
18      */
19     mutuallyExclusiveFilters?: boolean;
20     sortDirection?: SortDirection;
21     filters: DataTableFilters;
22     render: (item: T) => React.ReactElement<any>;
23     renderHeader?: () => React.ReactElement<any>;
24 }
25
26 export enum SortDirection {
27     ASC = "asc",
28     DESC = "desc",
29     NONE = "none"
30 }
31
32 export const toggleSortDirection = <T>(column: DataColumn<T>): DataColumn<T> => {
33     return column.sortDirection
34         ? column.sortDirection === SortDirection.ASC
35             ? { ...column, sortDirection: SortDirection.DESC }
36             : { ...column, sortDirection: SortDirection.ASC }
37         : column;
38 };
39
40 export const resetSortDirection = <T>(column: DataColumn<T>): DataColumn<T> => {
41     return column.sortDirection ? { ...column, sortDirection: SortDirection.NONE } : column;
42 };
43
44 export const createDataColumn = <T>(dataColumn: Partial<DataColumn<T>>): DataColumn<T> => ({
45     key: '',
46     name: '',
47     selected: true,
48     configurable: true,
49     sortDirection: SortDirection.NONE,
50     filters: createTree(),
51     render: () => React.createElement('span'),
52     ...dataColumn,
53 });