@chumsinc/sortable-tables 2.2.0 → 3.0.1

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.
Files changed (68) hide show
  1. package/CHANGELOG.md +31 -1
  2. package/README.md +94 -4
  3. package/dist/DataTable.d.ts +1 -1
  4. package/dist/DataTableCell.d.ts +6 -2
  5. package/dist/DataTableCols.d.ts +1 -1
  6. package/dist/DataTableContext.d.ts +10 -0
  7. package/dist/DataTableProvider.d.ts +12 -0
  8. package/dist/DataTableTH.d.ts +1 -1
  9. package/dist/SortableTable.d.ts +1 -1
  10. package/dist/SortableTableHead.d.ts +1 -1
  11. package/dist/SortableTableHeadWrapper.d.ts +9 -0
  12. package/dist/SortableTableTH.d.ts +1 -1
  13. package/dist/StandaloneDataTable.d.ts +6 -0
  14. package/dist/StandaloneDataTableRow.d.ts +9 -0
  15. package/dist/StandaloneSortHelper.d.ts +5 -0
  16. package/dist/StandaloneSortableTable.d.ts +6 -0
  17. package/dist/Table.d.ts +5 -4
  18. package/dist/index.cjs.js +4 -8
  19. package/dist/index.cjs.js.map +1 -1
  20. package/dist/index.d.ts +12 -8
  21. package/dist/index.es.js +239 -198
  22. package/dist/index.es.js.map +1 -1
  23. package/dist/types.d.ts +4 -3
  24. package/dist/useField.d.ts +6 -0
  25. package/dist/useTableContext.d.ts +2 -0
  26. package/dist/useTableFields.d.ts +9 -0
  27. package/dist/useTableSort.d.ts +9 -0
  28. package/package.json +3 -3
  29. package/src/DataTable.tsx +34 -11
  30. package/src/DataTableCell.tsx +33 -28
  31. package/src/DataTableCols.tsx +11 -18
  32. package/src/DataTableContext.ts +13 -0
  33. package/src/DataTableHead.tsx +6 -7
  34. package/src/DataTableProvider.tsx +62 -0
  35. package/src/DataTableRow.tsx +8 -7
  36. package/src/DataTableTBody.tsx +1 -3
  37. package/src/DataTableTH.tsx +4 -3
  38. package/src/RowsPerPage.tsx +2 -4
  39. package/src/SortableTable.tsx +36 -12
  40. package/src/SortableTableHead.tsx +9 -9
  41. package/src/SortableTableHeadWrapper.tsx +20 -0
  42. package/src/SortableTableTH.tsx +4 -4
  43. package/src/StandaloneDataTable.tsx +16 -0
  44. package/src/StandaloneDataTableRow.tsx +42 -0
  45. package/src/StandaloneSortHelper.tsx +15 -0
  46. package/src/StandaloneSortableTable.tsx +21 -0
  47. package/src/Table.tsx +49 -44
  48. package/src/TablePagination.tsx +1 -3
  49. package/src/index.tsx +21 -15
  50. package/src/types.ts +127 -126
  51. package/src/useField.ts +19 -0
  52. package/src/useTableContext.ts +10 -0
  53. package/src/useTableFields.ts +20 -0
  54. package/src/useTableSort.ts +20 -0
  55. package/test/Main.tsx +37 -0
  56. package/test/TableColumnsHandler.tsx +9 -9
  57. package/test/TestTable.tsx +51 -46
  58. package/test/data.ts +232 -232
  59. package/test/index.tsx +11 -11
  60. package/test/tableFields.tsx +11 -3
  61. package/test/utils.ts +19 -0
  62. package/tsconfig.json +1 -0
  63. package/dist/DataTableWithContext.d.ts +0 -2
  64. package/dist/SortableTableWithContext.d.ts +0 -2
  65. package/dist/TableProvider.d.ts +0 -17
  66. package/src/DataTableWithContext.tsx +0 -41
  67. package/src/SortableTableWithContext.tsx +0 -44
  68. package/src/TableProvider.tsx +0 -77
@@ -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;
@@ -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;
@@ -1,19 +1,43 @@
1
- import React from 'react';
2
1
  import type {SortableTableProps} from "./types";
3
- import TableProvider from "./TableProvider";
4
- import SortableTableWithContext from "./SortableTableWithContext";
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
- <TableProvider initialFields={fields}>
13
- <SortableTableWithContext {...rest}/>
14
- </TableProvider>
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 "./TableProvider";
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 {field, ascending} = currentSort;
11
+ const [fields] = useTableFields<T>()
12
+ const [sort] = useTableSort<T>();
14
13
  return (
15
14
  <thead>
16
15
  <tr>
17
- {fields.map((tableField, index) => (
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
- export default SortableTableHead;
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';
@@ -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,42 @@
1
+ import React, {type MouseEvent} from 'react';
2
+ import type {DataTableField, DataTableRowProps} from "./types";
3
+ import DataTableCell from "./DataTableCell";
4
+ import clsx from "clsx";
5
+ import {useTableFields} from "./useTableFields";
6
+
7
+ export interface StandaloneDataTableRowProps<T = unknown> extends DataTableRowProps<T> {
8
+ fields: DataTableField<T>[]
9
+ }
10
+
11
+ export default function StandaloneDataTableRow<T = unknown>({
12
+ fields,
13
+ className,
14
+ rowClassName,
15
+ selected,
16
+ row,
17
+ trRef,
18
+ onClick,
19
+ ...rest
20
+ }: StandaloneDataTableRowProps<T>) {
21
+ const clickHandler = (ev: MouseEvent<HTMLTableRowElement>) => {
22
+ onClick?.(row, ev)
23
+ }
24
+
25
+ const _className = typeof rowClassName === 'function' ? rowClassName(row) : rowClassName;
26
+ if (!row) {
27
+ return null;
28
+ }
29
+
30
+ return (
31
+ <tr ref={trRef}
32
+ className={clsx({'table-active': selected}, className, _className)}
33
+ onClick={clickHandler}
34
+ {...rest}>
35
+ {fields
36
+ .map((field, index) => (
37
+ <DataTableCell key={String(field?.id ?? index)} field={field} row={row}/>
38
+ ))}
39
+ </tr>
40
+ )
41
+ }
42
+ StandaloneDataTableRow.displayName = 'StandaloneDataTableRow';
@@ -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 type StyledTableProps = TableHTMLAttributes<HTMLTableElement> & Pick<DataTableProps, 'sticky' | 'responsive'>
7
-
8
- const StyledTable = styled.table<StyledTableProps>`
9
- --table-sticky-top: ${props => props.sticky ? '0' : undefined};
10
-
11
- thead {
12
- tr:nth-of-type(1) td,
13
- tr:nth-of-type(1) th {
14
- top: var(--table-sticky-top, unset);
15
- position: ${props => props.sticky ? "sticky" : "unset"};
16
- z-index: ${props => props.sticky ? 10 : "unset"};
17
- background: ${props => props.sticky ? "linear-gradient(var(--bs-table-bg) 75%, rgba(var(--bs-secondary-bg-rgb), 0.9))" : "unset"};
18
- }
19
- }
20
- `
21
-
22
- export default React.forwardRef<HTMLTableElement, StyledTableProps>(
23
- function Table({
24
- sticky,
25
- responsive,
26
- children,
27
- className,
28
- ...rest
29
- }, ref) {
30
- if (responsive) {
31
- const _className = clsx(className, {
32
- 'table-responsive': responsive === true,
33
- [`table-responsive-${responsive}`]: responsive !== true,
34
- })
35
- return (
36
- <div className={_className}>
37
- <StyledTable ref={ref} {...rest}>{children}</StyledTable>
38
- </div>
39
- )
40
- }
41
- return (
42
- <StyledTable className={className} sticky={sticky} ref={ref} {...rest}>{children}</StyledTable>
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
+
@@ -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
- export {default as DataTable} from './DataTable';
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 SortableTable} from './SortableTable';
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 './DataTableWithContext'
14
- export {default as SortableTableWithContext} from './SortableTableWithContext'
15
- export {default as DataTableProvider} from './TableProvider'
16
- export {useTableContext, useTableFields, useHasTableFieldsContext, useField} from './TableProvider'
17
- export type {TableContextData, TableProviderProps} from './TableProvider'
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
- DataTableCellProps
40
+ UISize,
41
+ UIFlexAlign
36
42
  } from './types';
37
43