@chumsinc/sortable-tables 2.2.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +69 -52
- package/README.md +94 -4
- package/dist/DataTable.d.ts +6 -6
- package/dist/DataTableCell.d.ts +299 -295
- package/dist/DataTableCols.d.ts +5 -5
- package/dist/DataTableContext.d.ts +10 -0
- package/dist/DataTableProvider.d.ts +12 -0
- package/dist/DataTableTH.d.ts +6 -6
- package/dist/SortableTable.d.ts +6 -6
- package/dist/SortableTableHead.d.ts +6 -6
- package/dist/SortableTableHeadWrapper.d.ts +9 -0
- package/dist/SortableTableTH.d.ts +6 -6
- package/dist/StandaloneDataTable.d.ts +6 -0
- package/dist/StandaloneSortHelper.d.ts +5 -0
- package/dist/StandaloneSortableTable.d.ts +6 -0
- package/dist/Table.d.ts +6 -5
- package/dist/index.cjs.js +30 -34
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +20 -16
- package/dist/index.es.js +521 -471
- package/dist/index.es.js.map +1 -1
- package/dist/types.d.ts +104 -103
- package/dist/useField.d.ts +6 -0
- package/dist/useTableContext.d.ts +2 -0
- package/dist/useTableFields.d.ts +9 -0
- package/dist/useTableSort.d.ts +9 -0
- package/package.json +3 -3
- package/src/DataTable.tsx +34 -11
- package/src/DataTableCell.tsx +33 -28
- package/src/DataTableCols.tsx +11 -18
- package/src/DataTableContext.ts +13 -0
- package/src/DataTableHead.tsx +6 -7
- package/src/DataTableProvider.tsx +62 -0
- package/src/DataTableRow.tsx +8 -7
- package/src/DataTableTBody.tsx +1 -3
- package/src/DataTableTH.tsx +4 -3
- package/src/RowsPerPage.tsx +2 -4
- package/src/SortableTable.tsx +36 -12
- package/src/SortableTableHead.tsx +9 -9
- package/src/SortableTableHeadWrapper.tsx +20 -0
- package/src/SortableTableTH.tsx +4 -4
- package/src/StandaloneDataTable.tsx +16 -0
- package/src/StandaloneSortHelper.tsx +15 -0
- package/src/StandaloneSortableTable.tsx +21 -0
- package/src/Table.tsx +49 -44
- package/src/TablePagination.tsx +1 -3
- package/src/index.tsx +21 -15
- package/src/types.ts +127 -126
- package/src/useField.ts +19 -0
- package/src/useTableContext.ts +10 -0
- package/src/useTableFields.ts +20 -0
- package/src/useTableSort.ts +20 -0
- package/test/Main.tsx +37 -0
- package/test/TableColumnsHandler.tsx +9 -9
- package/test/TestTable.tsx +51 -46
- package/test/data.ts +232 -232
- package/test/index.tsx +11 -11
- package/test/tableFields.tsx +11 -3
- package/test/utils.ts +19 -0
- package/tsconfig.json +1 -0
- package/dist/DataTableWithContext.d.ts +0 -2
- package/dist/SortableTableWithContext.d.ts +0 -2
- package/dist/TableProvider.d.ts +0 -17
- package/src/DataTableWithContext.tsx +0 -41
- package/src/SortableTableWithContext.tsx +0 -44
- package/src/TableProvider.tsx +0 -77
package/src/DataTableCols.tsx
CHANGED
|
@@ -1,25 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import clsx from "clsx";
|
|
3
|
-
import {useTableFields} from "./TableProvider";
|
|
1
|
+
import {useTableFields} from "./useTableFields";
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
visibility: collapse;
|
|
8
|
-
}
|
|
9
|
-
`
|
|
10
|
-
|
|
11
|
-
function DataTableCols() {
|
|
12
|
-
const fields = useTableFields()
|
|
3
|
+
export default function DataTableCols<T = unknown>() {
|
|
4
|
+
const [fields] = useTableFields<T>()
|
|
13
5
|
return (
|
|
14
6
|
<colgroup>
|
|
15
|
-
{fields
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
{fields
|
|
8
|
+
.filter(field => field.visible !== false)
|
|
9
|
+
.map((field, index) => (
|
|
10
|
+
<col key={index}
|
|
11
|
+
className={field.colClassName}
|
|
12
|
+
span={field.colSpan ?? 1}/>
|
|
13
|
+
))}
|
|
20
14
|
</colgroup>
|
|
21
15
|
)
|
|
22
16
|
}
|
|
23
|
-
|
|
24
17
|
DataTableCols.displayName = 'DataTableCols';
|
|
25
|
-
|
|
18
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {createContext} from "react";
|
|
2
|
+
import type {DataTableField, SortProps} from "./types";
|
|
3
|
+
|
|
4
|
+
export interface TableContextData<T = unknown> {
|
|
5
|
+
fields: DataTableField<T>[];
|
|
6
|
+
setFields: (next: DataTableField<T>[] | ((prev: DataTableField<T>[]) => DataTableField<T>[])) => void;
|
|
7
|
+
sort: SortProps<T>|null;
|
|
8
|
+
setSort: (next: SortProps<T> | null | ((prev:SortProps<T>|null) => SortProps<T>|null )) => void;
|
|
9
|
+
getField: (key: string | number) => DataTableField<T> | undefined;
|
|
10
|
+
updateField: (key: string | number, arg: Partial<DataTableField<T>>) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const DataTableContext = createContext<TableContextData<unknown> | null>(null)
|
package/src/DataTableHead.tsx
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import React, {type TableHTMLAttributes} from "react";
|
|
2
2
|
import DataTableTH from "./DataTableTH";
|
|
3
3
|
import clsx from "clsx";
|
|
4
|
-
import {useTableFields} from "./
|
|
4
|
+
import {useTableFields} from "./useTableFields";
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
function DataTableHead<T = unknown>({...rest}: TableHTMLAttributes<HTMLTableSectionElement>) {
|
|
8
|
-
const fields = useTableFields<T>()
|
|
7
|
+
export default function DataTableHead<T = unknown>({...rest}: TableHTMLAttributes<HTMLTableSectionElement>) {
|
|
8
|
+
const [fields] = useTableFields<T>()
|
|
9
9
|
return (
|
|
10
10
|
<thead {...rest}>
|
|
11
11
|
<tr>
|
|
12
|
-
{fields
|
|
13
|
-
|
|
12
|
+
{fields
|
|
13
|
+
.map((field, index) => (
|
|
14
|
+
<DataTableTH key={String(field.id ?? index)}
|
|
14
15
|
{...field.thProps}
|
|
15
16
|
field={field}
|
|
16
17
|
className={clsx(
|
|
@@ -23,6 +24,4 @@ function DataTableHead<T = unknown>({...rest}: TableHTMLAttributes<HTMLTableSect
|
|
|
23
24
|
</thead>
|
|
24
25
|
)
|
|
25
26
|
}
|
|
26
|
-
|
|
27
27
|
DataTableHead.displayName = 'DataTableHead';
|
|
28
|
-
export default DataTableHead;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {type ReactNode, useCallback, useMemo, useState} from "react";
|
|
2
|
+
import type {DataTableField, SortProps} from "./types";
|
|
3
|
+
import {DataTableContext, type TableContextData} from "./DataTableContext";
|
|
4
|
+
|
|
5
|
+
export interface TableProviderProps<T = unknown> {
|
|
6
|
+
initialFields: DataTableField<T>[];
|
|
7
|
+
initialSort?: SortProps<T>|null;
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default function DataTableProvider<T = unknown>({
|
|
12
|
+
children,
|
|
13
|
+
initialFields = [],
|
|
14
|
+
initialSort = null,
|
|
15
|
+
}: TableProviderProps<T>) {
|
|
16
|
+
const [fields, setFieldsState] = useState<DataTableField<T>[]>(initialFields);
|
|
17
|
+
const [sort, setProviderSort] = useState<SortProps<T> | null>(initialSort);
|
|
18
|
+
|
|
19
|
+
const setFields = useCallback(
|
|
20
|
+
(fields: DataTableField<T>[] | ((prev: DataTableField<T>[]) => DataTableField<T>[])) => {
|
|
21
|
+
setFieldsState(fields)
|
|
22
|
+
}, []);
|
|
23
|
+
|
|
24
|
+
const setSort = useCallback(
|
|
25
|
+
(sort: SortProps<T> | null | ((sort:SortProps<T>|null) => SortProps<T> | null)) => {
|
|
26
|
+
setProviderSort(sort)
|
|
27
|
+
}, [])
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
const updateField = useCallback((key: string | number, arg: Partial<DataTableField<T>>) => {
|
|
31
|
+
const nextState = fields.map(field => {
|
|
32
|
+
if (field.id === key) {
|
|
33
|
+
return {...field, ...arg}
|
|
34
|
+
}
|
|
35
|
+
return field;
|
|
36
|
+
});
|
|
37
|
+
setFieldsState(nextState);
|
|
38
|
+
}, [fields])
|
|
39
|
+
|
|
40
|
+
const getField = useCallback((key: string | number) => fields.find(field => field.id === key), [fields]);
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
const value = useMemo<TableContextData<T>>(
|
|
44
|
+
() => ({
|
|
45
|
+
fields,
|
|
46
|
+
setFields,
|
|
47
|
+
sort,
|
|
48
|
+
setSort,
|
|
49
|
+
getField,
|
|
50
|
+
updateField
|
|
51
|
+
}),
|
|
52
|
+
[fields, setFields, sort, setSort, updateField, getField]
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<DataTableContext.Provider value={value as unknown as TableContextData<unknown>}>
|
|
57
|
+
{children}
|
|
58
|
+
</DataTableContext.Provider>
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
DataTableProvider.displayName = 'DataTableProvider';
|
|
62
|
+
|
package/src/DataTableRow.tsx
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import React, {MouseEvent} from 'react';
|
|
1
|
+
import React, {type MouseEvent} from 'react';
|
|
2
2
|
import type {DataTableRowProps} from "./types";
|
|
3
3
|
import DataTableCell from "./DataTableCell";
|
|
4
4
|
import clsx from "clsx";
|
|
5
|
-
import {useTableFields} from "./
|
|
5
|
+
import {useTableFields} from "./useTableFields";
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
function DataTableRow<T = unknown>({
|
|
8
|
+
export default function DataTableRow<T = unknown>({
|
|
9
9
|
className,
|
|
10
10
|
rowClassName,
|
|
11
11
|
selected,
|
|
@@ -14,7 +14,7 @@ function DataTableRow<T = unknown>({
|
|
|
14
14
|
onClick,
|
|
15
15
|
...rest
|
|
16
16
|
}: DataTableRowProps<T>) {
|
|
17
|
-
const fields = useTableFields<T>()
|
|
17
|
+
const [fields] = useTableFields<T>()
|
|
18
18
|
const clickHandler = (ev: MouseEvent<HTMLTableRowElement>) => {
|
|
19
19
|
onClick?.(row, ev)
|
|
20
20
|
}
|
|
@@ -29,10 +29,11 @@ function DataTableRow<T = unknown>({
|
|
|
29
29
|
className={clsx({'table-active': selected}, className, _className)}
|
|
30
30
|
onClick={clickHandler}
|
|
31
31
|
{...rest}>
|
|
32
|
-
{fields
|
|
32
|
+
{fields
|
|
33
|
+
.map((field, index) => (
|
|
34
|
+
<DataTableCell key={String(field?.id ?? index)} field={field} row={row}/>
|
|
35
|
+
))}
|
|
33
36
|
</tr>
|
|
34
37
|
)
|
|
35
38
|
}
|
|
36
|
-
|
|
37
39
|
DataTableRow.displayName = 'DataTableRow';
|
|
38
|
-
export default DataTableRow;
|
package/src/DataTableTBody.tsx
CHANGED
|
@@ -3,7 +3,7 @@ import DataTableRow from "./DataTableRow";
|
|
|
3
3
|
import type {DataTableTBodyProps} from "./types";
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
function DataTableTBody<T = unknown>({
|
|
6
|
+
export default function DataTableTBody<T = unknown>({
|
|
7
7
|
data,
|
|
8
8
|
keyField,
|
|
9
9
|
rowClassName,
|
|
@@ -31,6 +31,4 @@ function DataTableTBody<T = unknown>({
|
|
|
31
31
|
</tbody>
|
|
32
32
|
)
|
|
33
33
|
}
|
|
34
|
-
|
|
35
34
|
DataTableTBody.displayName = 'DataTableTBody';
|
|
36
|
-
export default DataTableTBody
|
package/src/DataTableTH.tsx
CHANGED
|
@@ -3,12 +3,15 @@ import type {DataTableTHProps} from "./types";
|
|
|
3
3
|
import clsx from "clsx";
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
function DataTableTH<T = unknown>({
|
|
6
|
+
export default function DataTableTH<T = unknown>({
|
|
7
7
|
field,
|
|
8
8
|
className,
|
|
9
9
|
children,
|
|
10
10
|
...rest
|
|
11
11
|
}: DataTableTHProps<T>) {
|
|
12
|
+
if (field.visible === false) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
12
15
|
const thClassName = clsx({[`text-${field.align}`]: !!field.align}, className);
|
|
13
16
|
return (
|
|
14
17
|
<th className={thClassName} scope="col" {...rest}>
|
|
@@ -16,6 +19,4 @@ function DataTableTH<T = unknown>({
|
|
|
16
19
|
</th>
|
|
17
20
|
)
|
|
18
21
|
}
|
|
19
|
-
|
|
20
22
|
DataTableTH.displayName = 'DataTableTH';
|
|
21
|
-
export default DataTableTH;
|
package/src/RowsPerPage.tsx
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import React, {ChangeEvent, useId} from 'react';
|
|
1
|
+
import React, {type ChangeEvent, useId} from 'react';
|
|
2
2
|
import type {RowsPerPageProps} from "./types";
|
|
3
3
|
import clsx from "clsx";
|
|
4
4
|
|
|
5
5
|
const defaultRowsPerPageValues: number[] = [10, 25, 50, 100, 250, 500, 1000];
|
|
6
6
|
|
|
7
|
-
function RowsPerPage({
|
|
7
|
+
export default function RowsPerPage({
|
|
8
8
|
value,
|
|
9
9
|
pageValues = defaultRowsPerPageValues,
|
|
10
10
|
size,
|
|
@@ -32,6 +32,4 @@ function RowsPerPage({
|
|
|
32
32
|
</div>
|
|
33
33
|
)
|
|
34
34
|
}
|
|
35
|
-
|
|
36
35
|
RowsPerPage.displayName = 'RowsPerPage';
|
|
37
|
-
export default RowsPerPage;
|
package/src/SortableTable.tsx
CHANGED
|
@@ -1,19 +1,43 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
1
|
import type {SortableTableProps} from "./types";
|
|
3
|
-
import
|
|
4
|
-
import
|
|
2
|
+
import clsx from "clsx";
|
|
3
|
+
import Table from "./Table";
|
|
4
|
+
import {DataTableCols, DataTableTBody} from "./index";
|
|
5
|
+
import React from "react";
|
|
6
|
+
import SortableTableHeadWrapper from "./SortableTableHeadWrapper";
|
|
5
7
|
|
|
8
|
+
export default function SortableTable<T = unknown>({
|
|
9
|
+
className,
|
|
10
|
+
size,
|
|
11
|
+
responsive,
|
|
12
|
+
sticky,
|
|
13
|
+
data,
|
|
14
|
+
keyField,
|
|
15
|
+
rowClassName,
|
|
16
|
+
renderRow,
|
|
17
|
+
onSelectRow,
|
|
18
|
+
selected,
|
|
19
|
+
tableHeadProps,
|
|
20
|
+
children,
|
|
21
|
+
tfoot,
|
|
22
|
+
onChangeSort,
|
|
23
|
+
...rest
|
|
24
|
+
}: Omit<SortableTableProps<T>, 'fields' | 'currentSort'>) {
|
|
25
|
+
const tableClassName = clsx('table', className, {
|
|
26
|
+
[`table-${size}`]: !!size,
|
|
27
|
+
})
|
|
6
28
|
|
|
7
|
-
function SortableTable<T = unknown>({
|
|
8
|
-
fields,
|
|
9
|
-
...rest
|
|
10
|
-
}: SortableTableProps<T>) {
|
|
11
29
|
return (
|
|
12
|
-
<
|
|
13
|
-
<
|
|
14
|
-
|
|
30
|
+
<Table className={tableClassName} responsive={responsive} sticky={sticky} {...rest}>
|
|
31
|
+
<DataTableCols/>
|
|
32
|
+
<SortableTableHeadWrapper onChangeSort={onChangeSort} {...tableHeadProps}/>
|
|
33
|
+
{!!data.length && (
|
|
34
|
+
<DataTableTBody data={data} keyField={keyField} rowClassName={rowClassName}
|
|
35
|
+
renderRow={renderRow}
|
|
36
|
+
onSelectRow={onSelectRow} selected={selected}/>
|
|
37
|
+
)}
|
|
38
|
+
{children}
|
|
39
|
+
{tfoot}
|
|
40
|
+
</Table>
|
|
15
41
|
)
|
|
16
42
|
}
|
|
17
|
-
|
|
18
43
|
SortableTable.displayName = 'SortableTable';
|
|
19
|
-
export default SortableTable;
|
|
@@ -2,21 +2,21 @@ import React from "react";
|
|
|
2
2
|
import SortableTableTH from "./SortableTableTH";
|
|
3
3
|
import type {SortableTableHeadProps} from "./types";
|
|
4
4
|
import clsx from "clsx";
|
|
5
|
-
import {useTableFields} from "./
|
|
5
|
+
import {useTableFields} from "./useTableFields";
|
|
6
|
+
import {useTableSort} from "./useTableSort";
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
function SortableTableHead<T = unknown>({
|
|
9
|
-
currentSort,
|
|
8
|
+
export default function SortableTableHead<T = unknown>({
|
|
10
9
|
onChangeSort,
|
|
11
10
|
}: SortableTableHeadProps<T>) {
|
|
12
|
-
const fields = useTableFields<T>()
|
|
13
|
-
const
|
|
11
|
+
const [fields] = useTableFields<T>()
|
|
12
|
+
const [sort] = useTableSort<T>();
|
|
14
13
|
return (
|
|
15
14
|
<thead>
|
|
16
15
|
<tr>
|
|
17
|
-
{fields
|
|
16
|
+
{fields
|
|
17
|
+
.map((tableField, index) => (
|
|
18
18
|
<SortableTableTH<T> key={index} field={tableField}
|
|
19
|
-
sorted={field === tableField.field} ascending={ascending}
|
|
19
|
+
sorted={sort?.field === tableField.field} ascending={sort?.ascending}
|
|
20
20
|
className={clsx(
|
|
21
21
|
typeof tableField.className === 'function'
|
|
22
22
|
? {[`text-${tableField.align}`]: !!tableField.align}
|
|
@@ -29,4 +29,4 @@ function SortableTableHead<T = unknown>({
|
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
SortableTableHead.displayName = 'SortableTableHead';
|
|
32
|
-
|
|
32
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {useTableFields} from "./useTableFields";
|
|
2
|
+
import {useTableSort} from "./useTableSort";
|
|
3
|
+
import SortableTableHead from "./SortableTableHead";
|
|
4
|
+
import type {SortProps} from "./types";
|
|
5
|
+
|
|
6
|
+
export interface SortableTableHeadWrapperProps<T = unknown> {
|
|
7
|
+
onChangeSort: (sort: SortProps<T>) => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default function SortableTableHeadWrapper<T = unknown>({
|
|
11
|
+
onChangeSort,
|
|
12
|
+
}: SortableTableHeadWrapperProps<T>) {
|
|
13
|
+
const [fields] = useTableFields<T>()
|
|
14
|
+
const [sort] = useTableSort<T>();
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<SortableTableHead fields={fields} currentSort={sort} onChangeSort={onChangeSort} />
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
SortableTableHeadWrapper.displayName = 'SortableTableHeadWrapper';
|
package/src/SortableTableTH.tsx
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import classNames from "classnames";
|
|
3
2
|
import DataTableTH from "./DataTableTH";
|
|
4
3
|
import type {SortableTableTHProps, UIFlexAlign} from "./types";
|
|
5
4
|
import styled from '@emotion/styled';
|
|
@@ -39,13 +38,16 @@ const FieldTitle = styled.div<FieldTitleProps>`
|
|
|
39
38
|
}
|
|
40
39
|
`
|
|
41
40
|
|
|
42
|
-
function SortableTableTH<T = unknown>({
|
|
41
|
+
export default function SortableTableTH<T = unknown>({
|
|
43
42
|
field,
|
|
44
43
|
sorted,
|
|
45
44
|
ascending,
|
|
46
45
|
className,
|
|
47
46
|
onClick
|
|
48
47
|
}: SortableTableTHProps<T>) {
|
|
48
|
+
if (field.visible === false) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
49
51
|
if (!field.sortable) {
|
|
50
52
|
return (<DataTableTH field={field} className={className}/>)
|
|
51
53
|
}
|
|
@@ -75,6 +77,4 @@ function SortableTableTH<T = unknown>({
|
|
|
75
77
|
</th>
|
|
76
78
|
)
|
|
77
79
|
}
|
|
78
|
-
|
|
79
80
|
SortableTableTH.displayName = 'SortableTableTH';
|
|
80
|
-
export default SortableTableTH;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type {DataTableProps} from "./types";
|
|
2
|
+
import DataTableProvider from "./DataTableProvider";
|
|
3
|
+
import DataTable from "./DataTable";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export default function StandaloneDataTable<T = unknown>({
|
|
7
|
+
fields,
|
|
8
|
+
...rest
|
|
9
|
+
}: DataTableProps<T>) {
|
|
10
|
+
return (
|
|
11
|
+
<DataTableProvider initialFields={fields}>
|
|
12
|
+
<DataTable {...rest}/>
|
|
13
|
+
</DataTableProvider>
|
|
14
|
+
)
|
|
15
|
+
}
|
|
16
|
+
StandaloneDataTable.displayName = 'StandaloneDataTable';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type {SortProps} from "./types";
|
|
2
|
+
import {useTableSort} from "./useTableSort";
|
|
3
|
+
import {useEffect} from "react";
|
|
4
|
+
|
|
5
|
+
export interface StandaloneSortHelperProps<T = unknown> {
|
|
6
|
+
nextSort: SortProps<T>
|
|
7
|
+
}
|
|
8
|
+
export function StandaloneSortHelper<T = unknown>({nextSort}:StandaloneSortHelperProps<T>) {
|
|
9
|
+
const [, setNextSort] = useTableSort<T>();
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
console.log('setNextSort', nextSort);
|
|
12
|
+
setNextSort(nextSort);
|
|
13
|
+
}, [nextSort, setNextSort]);
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type {SortableTableProps} from "./types";
|
|
3
|
+
import DataTableProvider from "./DataTableProvider";
|
|
4
|
+
import SortableTable from "./SortableTable";
|
|
5
|
+
import {StandaloneSortHelper} from "./StandaloneSortHelper";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export default function StandaloneSortableTable<T = unknown>({
|
|
9
|
+
fields,
|
|
10
|
+
currentSort,
|
|
11
|
+
...rest
|
|
12
|
+
}: SortableTableProps<T>) {
|
|
13
|
+
return (
|
|
14
|
+
<DataTableProvider initialFields={fields} initialSort={currentSort}>
|
|
15
|
+
<StandaloneSortHelper nextSort={currentSort} />
|
|
16
|
+
<SortableTable {...rest}/>
|
|
17
|
+
</DataTableProvider>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
StandaloneSortableTable.displayName = 'StandaloneSortableTable';
|
package/src/Table.tsx
CHANGED
|
@@ -1,44 +1,49 @@
|
|
|
1
|
-
import React, {TableHTMLAttributes} from 'react';
|
|
2
|
-
import styled from "@emotion/styled";
|
|
3
|
-
import type {DataTableProps} from "./types";
|
|
4
|
-
import clsx from "clsx";
|
|
5
|
-
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
1
|
+
import React, {type RefObject, type TableHTMLAttributes} from 'react';
|
|
2
|
+
import styled from "@emotion/styled";
|
|
3
|
+
import type {DataTableProps} from "./types";
|
|
4
|
+
import clsx from "clsx";
|
|
5
|
+
|
|
6
|
+
export interface StyledTableProps extends TableHTMLAttributes<HTMLTableElement>, Pick<DataTableProps, 'sticky' | 'responsive'> {
|
|
7
|
+
ref?: RefObject<HTMLTableElement>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const StyledTable = styled.table<StyledTableProps>`
|
|
11
|
+
--table-sticky-top: ${props => props.sticky ? '0' : undefined};
|
|
12
|
+
|
|
13
|
+
thead {
|
|
14
|
+
tr:nth-of-type(1) td,
|
|
15
|
+
tr:nth-of-type(1) th {
|
|
16
|
+
top: var(--table-sticky-top, unset);
|
|
17
|
+
position: ${props => props.sticky ? "sticky" : "unset"};
|
|
18
|
+
z-index: ${props => props.sticky ? 10 : "unset"};
|
|
19
|
+
background: ${props => props.sticky ? "linear-gradient(var(--bs-table-bg) 75%, rgba(var(--bs-secondary-bg-rgb), 0.9))" : "unset"};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
`
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export default function Table({
|
|
26
|
+
sticky,
|
|
27
|
+
responsive,
|
|
28
|
+
children,
|
|
29
|
+
className,
|
|
30
|
+
ref,
|
|
31
|
+
...rest
|
|
32
|
+
}: StyledTableProps) {
|
|
33
|
+
if (responsive) {
|
|
34
|
+
const _className = clsx(className, {
|
|
35
|
+
'table-responsive': responsive === true,
|
|
36
|
+
[`table-responsive-${responsive}`]: responsive !== true,
|
|
37
|
+
})
|
|
38
|
+
return (
|
|
39
|
+
<div className={_className}>
|
|
40
|
+
<StyledTable ref={ref} {...rest}>{children}</StyledTable>
|
|
41
|
+
</div>
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return (
|
|
46
|
+
<StyledTable className={className} sticky={sticky} ref={ref} {...rest}>{children}</StyledTable>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
package/src/TablePagination.tsx
CHANGED
|
@@ -3,7 +3,7 @@ import RowsPerPage from "./RowsPerPage";
|
|
|
3
3
|
import type {TablePaginationProps} from "./types";
|
|
4
4
|
import clsx from "clsx";
|
|
5
5
|
|
|
6
|
-
function TablePagination({
|
|
6
|
+
export default function TablePagination({
|
|
7
7
|
page,
|
|
8
8
|
rowsPerPage,
|
|
9
9
|
onChangePage,
|
|
@@ -67,6 +67,4 @@ function TablePagination({
|
|
|
67
67
|
</div>
|
|
68
68
|
)
|
|
69
69
|
}
|
|
70
|
-
|
|
71
70
|
TablePagination.displayname = 'TablePagination';
|
|
72
|
-
export default TablePagination
|
package/src/index.tsx
CHANGED
|
@@ -1,37 +1,43 @@
|
|
|
1
1
|
/* eslint-disable react-refresh/only-export-components */
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
export {default as DataTable} from './StandaloneDataTable';
|
|
4
5
|
export {default as DataTableRow} from './DataTableRow';
|
|
5
6
|
export {default as DataTableTBody} from './DataTableTBody';
|
|
6
7
|
export {default as DataTableTH} from './DataTableTH';
|
|
7
|
-
export {default as
|
|
8
|
+
export {default as StandaloneSortableTable} from './StandaloneSortableTable';
|
|
8
9
|
export {default as SortableTableHead} from './SortableTableHead';
|
|
9
10
|
export {default as SortableTableTH} from './SortableTableTH';
|
|
10
11
|
export {default as RowsPerPage} from './RowsPerPage';
|
|
11
12
|
export {default as TablePagination} from './TablePagination';
|
|
12
13
|
export {default as DataTableCols} from './DataTableCols'
|
|
13
|
-
export {default as DataTableWithContext} from './
|
|
14
|
-
export {default as
|
|
15
|
-
export {default as DataTableProvider} from './
|
|
16
|
-
export {
|
|
17
|
-
export
|
|
14
|
+
export {default as DataTableWithContext} from './DataTable'
|
|
15
|
+
export {default as SortableTable} from './SortableTable'
|
|
16
|
+
export {default as DataTableProvider} from './DataTableProvider'
|
|
17
|
+
export {useField} from './useField'
|
|
18
|
+
export {useTableFields} from './useTableFields'
|
|
19
|
+
export {useTableSort} from './useTableSort'
|
|
20
|
+
export {useTableContext} from './useTableContext'
|
|
21
|
+
export {DataTableContext, type TableContextData} from './DataTableContext'
|
|
22
|
+
export type {TableProviderProps} from './DataTableProvider'
|
|
18
23
|
export type {
|
|
24
|
+
SortProps,
|
|
19
25
|
DataTableField,
|
|
20
|
-
DataTableTHProps,
|
|
21
|
-
DataTableClassNames,
|
|
22
|
-
DataTableProps,
|
|
23
26
|
SortableTableField,
|
|
27
|
+
DataTableColProps,
|
|
28
|
+
DataTableCellProps,
|
|
24
29
|
DataTableRowProps,
|
|
25
30
|
DataTableTBodyProps,
|
|
31
|
+
DataTableClassNames,
|
|
32
|
+
DataTableProps,
|
|
33
|
+
DataTableTHProps,
|
|
34
|
+
RowsPerPageProps,
|
|
26
35
|
SortableTableTHProps,
|
|
27
36
|
SortableTableHeadProps,
|
|
28
|
-
SortProps,
|
|
29
37
|
SortableTableProps,
|
|
30
|
-
UISize,
|
|
31
|
-
UIFlexAlign,
|
|
32
38
|
TablePaginationProps,
|
|
33
|
-
RowsPerPageProps,
|
|
34
39
|
UITableSize,
|
|
35
|
-
|
|
40
|
+
UISize,
|
|
41
|
+
UIFlexAlign
|
|
36
42
|
} from './types';
|
|
37
43
|
|