@trackunit/react-table 0.0.175 → 0.0.176
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/index.cjs.js +69 -0
- package/index.esm.js +71 -3
- package/package.json +1 -1
- package/src/index.d.ts +1 -0
- package/src/useTableSelection.d.ts +67 -0
package/index.cjs.js
CHANGED
|
@@ -686,6 +686,74 @@ const useTable = (_a) => {
|
|
|
686
686
|
return memoizedTable;
|
|
687
687
|
};
|
|
688
688
|
|
|
689
|
+
/**
|
|
690
|
+
* `useTableSelection` is a custom hook that provides functionality for row selection in a table.
|
|
691
|
+
* It returns an object containing methods and values related to row selection.
|
|
692
|
+
*
|
|
693
|
+
* @template TData - The type of data in the table. It must extend `selectableTableData`, which means it should at least have an `id` field.
|
|
694
|
+
* @returns {TableSelectionReturn<TData>} An object containing:
|
|
695
|
+
* - `selectionColumn`: A `ColumnDef` object for the selection column, which includes a checkbox in each cell and header.
|
|
696
|
+
* - `rowSelection`: The current state of row selection.
|
|
697
|
+
* - `setRowSelection`: A function to update the row selection state.
|
|
698
|
+
* @example
|
|
699
|
+
* const {
|
|
700
|
+
* selectionColumn,
|
|
701
|
+
* rowSelection,
|
|
702
|
+
* setRowSelection,
|
|
703
|
+
* } = useTableSelection<MyDataType>();
|
|
704
|
+
*
|
|
705
|
+
* const columns = [selectionColumn, ...otherColumns]
|
|
706
|
+
*
|
|
707
|
+
* const tableProps = useTable({
|
|
708
|
+
* ...selectionTableProps,
|
|
709
|
+
* state: {
|
|
710
|
+
* ...selectionTableState,
|
|
711
|
+
* },
|
|
712
|
+
* data,
|
|
713
|
+
* columns
|
|
714
|
+
* });
|
|
715
|
+
*/
|
|
716
|
+
const useTableSelection = ({ data, }) => {
|
|
717
|
+
const [rowSelection, setRowSelection] = React.useState({});
|
|
718
|
+
const selectedIds = React.useMemo(() => Object.entries(rowSelection)
|
|
719
|
+
.filter(([_, selected]) => selected)
|
|
720
|
+
.map(([id]) => id), [rowSelection]);
|
|
721
|
+
const columnHelper = React.useMemo(() => reactTable.createColumnHelper(), []);
|
|
722
|
+
const selectionColumn = React.useMemo(() => columnHelper.accessor(row => row.id, {
|
|
723
|
+
cell: ({ row }) => (jsxRuntime.jsx(reactFormComponents.Checkbox, { className: "ml-1",
|
|
724
|
+
checked: row.getIsSelected(),
|
|
725
|
+
disabled: !row.getCanSelect(),
|
|
726
|
+
indeterminate: row.getIsSomeSelected(),
|
|
727
|
+
onChange: row.getToggleSelectedHandler() })),
|
|
728
|
+
header: ({ table }) => (jsxRuntime.jsx(reactFormComponents.Checkbox, { checked: table.getIsAllRowsSelected(),
|
|
729
|
+
indeterminate: table.getIsSomeRowsSelected(),
|
|
730
|
+
onChange: table.getToggleAllRowsSelectedHandler() })),
|
|
731
|
+
id: "selection",
|
|
732
|
+
enableSorting: false,
|
|
733
|
+
enableResizing: false,
|
|
734
|
+
size: 55,
|
|
735
|
+
meta: {
|
|
736
|
+
required: true,
|
|
737
|
+
alignment: "left",
|
|
738
|
+
},
|
|
739
|
+
}), [columnHelper]);
|
|
740
|
+
const selectionTableState = React.useMemo(() => ({
|
|
741
|
+
rowSelection,
|
|
742
|
+
}), [rowSelection]);
|
|
743
|
+
const selectionTableProps = React.useMemo(() => ({
|
|
744
|
+
onRowSelectionChange: setRowSelection,
|
|
745
|
+
getRowId: row => row.id,
|
|
746
|
+
enableRowSelection: true,
|
|
747
|
+
}), []);
|
|
748
|
+
return {
|
|
749
|
+
selectionColumn,
|
|
750
|
+
selectedIds,
|
|
751
|
+
selectionTableState,
|
|
752
|
+
selectionTableProps,
|
|
753
|
+
setRowSelection,
|
|
754
|
+
};
|
|
755
|
+
};
|
|
756
|
+
|
|
689
757
|
//TODO: find a more appropriate place for this file
|
|
690
758
|
/**
|
|
691
759
|
* Convert a sorting state from the TUSort format to the TanStack format.
|
|
@@ -752,3 +820,4 @@ exports.fromTanStackToTUSort = fromTanStackToTUSort;
|
|
|
752
820
|
exports.fromTanStackToTUSortSite = fromTanStackToTUSortSite;
|
|
753
821
|
exports.useRelayPagination = useRelayPagination;
|
|
754
822
|
exports.useTable = useTable;
|
|
823
|
+
exports.useTableSelection = useTableSelection;
|
package/index.esm.js
CHANGED
|
@@ -5,11 +5,11 @@ import * as React from 'react';
|
|
|
5
5
|
import { useMemo, Children, cloneElement, useCallback, useEffect, useRef, useState } from 'react';
|
|
6
6
|
import { cvaMerge } from '@trackunit/css-class-variance-utilities';
|
|
7
7
|
import { Link } from 'react-router-dom';
|
|
8
|
-
import { flexRender, useReactTable, getSortedRowModel, getCoreRowModel } from '@tanstack/react-table';
|
|
8
|
+
import { flexRender, useReactTable, getSortedRowModel, getCoreRowModel, createColumnHelper } from '@tanstack/react-table';
|
|
9
9
|
export { createColumnHelper } from '@tanstack/react-table';
|
|
10
10
|
import { TableRoot, Thead, Tr, Th, SortIndicator, ResizeHandle, Tbody, Td } from '@trackunit/react-table-base-components';
|
|
11
11
|
import { useVirtualizer } from '@tanstack/react-virtual';
|
|
12
|
-
import { Toggle, RadioGroup, RadioItem } from '@trackunit/react-form-components';
|
|
12
|
+
import { Toggle, RadioGroup, RadioItem, Checkbox } from '@trackunit/react-form-components';
|
|
13
13
|
import update from 'immutability-helper';
|
|
14
14
|
import { DndProvider, useDrop, useDrag } from 'react-dnd';
|
|
15
15
|
import { HTML5Backend } from 'react-dnd-html5-backend';
|
|
@@ -661,6 +661,74 @@ const useTable = (_a) => {
|
|
|
661
661
|
return memoizedTable;
|
|
662
662
|
};
|
|
663
663
|
|
|
664
|
+
/**
|
|
665
|
+
* `useTableSelection` is a custom hook that provides functionality for row selection in a table.
|
|
666
|
+
* It returns an object containing methods and values related to row selection.
|
|
667
|
+
*
|
|
668
|
+
* @template TData - The type of data in the table. It must extend `selectableTableData`, which means it should at least have an `id` field.
|
|
669
|
+
* @returns {TableSelectionReturn<TData>} An object containing:
|
|
670
|
+
* - `selectionColumn`: A `ColumnDef` object for the selection column, which includes a checkbox in each cell and header.
|
|
671
|
+
* - `rowSelection`: The current state of row selection.
|
|
672
|
+
* - `setRowSelection`: A function to update the row selection state.
|
|
673
|
+
* @example
|
|
674
|
+
* const {
|
|
675
|
+
* selectionColumn,
|
|
676
|
+
* rowSelection,
|
|
677
|
+
* setRowSelection,
|
|
678
|
+
* } = useTableSelection<MyDataType>();
|
|
679
|
+
*
|
|
680
|
+
* const columns = [selectionColumn, ...otherColumns]
|
|
681
|
+
*
|
|
682
|
+
* const tableProps = useTable({
|
|
683
|
+
* ...selectionTableProps,
|
|
684
|
+
* state: {
|
|
685
|
+
* ...selectionTableState,
|
|
686
|
+
* },
|
|
687
|
+
* data,
|
|
688
|
+
* columns
|
|
689
|
+
* });
|
|
690
|
+
*/
|
|
691
|
+
const useTableSelection = ({ data, }) => {
|
|
692
|
+
const [rowSelection, setRowSelection] = useState({});
|
|
693
|
+
const selectedIds = useMemo(() => Object.entries(rowSelection)
|
|
694
|
+
.filter(([_, selected]) => selected)
|
|
695
|
+
.map(([id]) => id), [rowSelection]);
|
|
696
|
+
const columnHelper = useMemo(() => createColumnHelper(), []);
|
|
697
|
+
const selectionColumn = useMemo(() => columnHelper.accessor(row => row.id, {
|
|
698
|
+
cell: ({ row }) => (jsx(Checkbox, { className: "ml-1",
|
|
699
|
+
checked: row.getIsSelected(),
|
|
700
|
+
disabled: !row.getCanSelect(),
|
|
701
|
+
indeterminate: row.getIsSomeSelected(),
|
|
702
|
+
onChange: row.getToggleSelectedHandler() })),
|
|
703
|
+
header: ({ table }) => (jsx(Checkbox, { checked: table.getIsAllRowsSelected(),
|
|
704
|
+
indeterminate: table.getIsSomeRowsSelected(),
|
|
705
|
+
onChange: table.getToggleAllRowsSelectedHandler() })),
|
|
706
|
+
id: "selection",
|
|
707
|
+
enableSorting: false,
|
|
708
|
+
enableResizing: false,
|
|
709
|
+
size: 55,
|
|
710
|
+
meta: {
|
|
711
|
+
required: true,
|
|
712
|
+
alignment: "left",
|
|
713
|
+
},
|
|
714
|
+
}), [columnHelper]);
|
|
715
|
+
const selectionTableState = useMemo(() => ({
|
|
716
|
+
rowSelection,
|
|
717
|
+
}), [rowSelection]);
|
|
718
|
+
const selectionTableProps = useMemo(() => ({
|
|
719
|
+
onRowSelectionChange: setRowSelection,
|
|
720
|
+
getRowId: row => row.id,
|
|
721
|
+
enableRowSelection: true,
|
|
722
|
+
}), []);
|
|
723
|
+
return {
|
|
724
|
+
selectionColumn,
|
|
725
|
+
selectedIds,
|
|
726
|
+
selectionTableState,
|
|
727
|
+
selectionTableProps,
|
|
728
|
+
setRowSelection,
|
|
729
|
+
};
|
|
730
|
+
};
|
|
731
|
+
|
|
664
732
|
//TODO: find a more appropriate place for this file
|
|
665
733
|
/**
|
|
666
734
|
* Convert a sorting state from the TUSort format to the TanStack format.
|
|
@@ -712,4 +780,4 @@ const fromTanStackToTUSortSite = (input) => {
|
|
|
712
780
|
*/
|
|
713
781
|
setupLibraryTranslations();
|
|
714
782
|
|
|
715
|
-
export { ActionSheet, ColumnFilter, RowSpacing, Sorting, Table, fromTUSortToTanStack, fromTUSortToTanStackSite, fromTanStackToTUSort, fromTanStackToTUSortSite, useRelayPagination, useTable };
|
|
783
|
+
export { ActionSheet, ColumnFilter, RowSpacing, Sorting, Table, fromTUSortToTanStack, fromTUSortToTanStackSite, fromTanStackToTUSort, fromTanStackToTUSortSite, useRelayPagination, useTable, useTableSelection };
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ColumnDef, RowSelectionState } from "@tanstack/react-table";
|
|
2
|
+
import { Dispatch, SetStateAction } from "react";
|
|
3
|
+
type selectableTableData = {
|
|
4
|
+
id: string;
|
|
5
|
+
} & object;
|
|
6
|
+
export interface TableSelectionReturn<TData extends selectableTableData> {
|
|
7
|
+
/**
|
|
8
|
+
* An array of the ids of the currently selected rows.
|
|
9
|
+
*/
|
|
10
|
+
selectedIds: TData["id"][];
|
|
11
|
+
/**
|
|
12
|
+
* The current state of row selection.
|
|
13
|
+
* Pass this to the `state` prop of the `useTable` Hook.
|
|
14
|
+
*/
|
|
15
|
+
selectionTableState: {
|
|
16
|
+
rowSelection?: RowSelectionState | undefined;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Props to pass to the `useTable` Hook.
|
|
20
|
+
*/
|
|
21
|
+
selectionTableProps: {
|
|
22
|
+
onRowSelectionChange: Dispatch<SetStateAction<RowSelectionState>>;
|
|
23
|
+
getRowId: (row: TData) => TData["id"];
|
|
24
|
+
enableRowSelection: true;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* A function to update the row selection state.
|
|
28
|
+
* Pass this to the `onRowSelectionChange` prop of the `useTable` Hook.
|
|
29
|
+
*/
|
|
30
|
+
setRowSelection: Dispatch<SetStateAction<RowSelectionState>>;
|
|
31
|
+
/**
|
|
32
|
+
* A `ColumnDef` object for the selection column, which includes a checkbox in each cell and header.
|
|
33
|
+
*/
|
|
34
|
+
selectionColumn: ColumnDef<TData, string>;
|
|
35
|
+
}
|
|
36
|
+
export interface TableSelectionProps<TData extends selectableTableData> {
|
|
37
|
+
data: TData[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* `useTableSelection` is a custom hook that provides functionality for row selection in a table.
|
|
41
|
+
* It returns an object containing methods and values related to row selection.
|
|
42
|
+
*
|
|
43
|
+
* @template TData - The type of data in the table. It must extend `selectableTableData`, which means it should at least have an `id` field.
|
|
44
|
+
* @returns {TableSelectionReturn<TData>} An object containing:
|
|
45
|
+
* - `selectionColumn`: A `ColumnDef` object for the selection column, which includes a checkbox in each cell and header.
|
|
46
|
+
* - `rowSelection`: The current state of row selection.
|
|
47
|
+
* - `setRowSelection`: A function to update the row selection state.
|
|
48
|
+
* @example
|
|
49
|
+
* const {
|
|
50
|
+
* selectionColumn,
|
|
51
|
+
* rowSelection,
|
|
52
|
+
* setRowSelection,
|
|
53
|
+
* } = useTableSelection<MyDataType>();
|
|
54
|
+
*
|
|
55
|
+
* const columns = [selectionColumn, ...otherColumns]
|
|
56
|
+
*
|
|
57
|
+
* const tableProps = useTable({
|
|
58
|
+
* ...selectionTableProps,
|
|
59
|
+
* state: {
|
|
60
|
+
* ...selectionTableState,
|
|
61
|
+
* },
|
|
62
|
+
* data,
|
|
63
|
+
* columns
|
|
64
|
+
* });
|
|
65
|
+
*/
|
|
66
|
+
export declare const useTableSelection: <TData extends selectableTableData>({ data, }: TableSelectionProps<TData>) => TableSelectionReturn<TData>;
|
|
67
|
+
export {};
|