@redis-ui/table 2.18.0 → 2.22.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.
Files changed (77) hide show
  1. package/dist/Table/Table.cjs +28 -4
  2. package/dist/Table/Table.context.d.ts +1 -0
  3. package/dist/Table/Table.d.ts +25 -20
  4. package/dist/Table/Table.js +28 -4
  5. package/dist/Table/Table.style.cjs +33 -8
  6. package/dist/Table/Table.style.d.ts +4 -4
  7. package/dist/Table/Table.style.js +33 -8
  8. package/dist/Table/Table.types.d.ts +3 -1
  9. package/dist/Table/components/Compose/Compose.cjs +4 -1
  10. package/dist/Table/components/Compose/Compose.d.ts +1 -1
  11. package/dist/Table/components/Compose/Compose.js +4 -1
  12. package/dist/Table/components/ExpandRowButton/ExpandAllRowsButton.cjs +20 -0
  13. package/dist/Table/components/ExpandRowButton/ExpandAllRowsButton.d.ts +8 -0
  14. package/dist/Table/components/ExpandRowButton/ExpandAllRowsButton.js +20 -0
  15. package/dist/Table/components/ExpandRowButton/ExpandAllRowsButton.test.d.ts +1 -0
  16. package/dist/Table/components/ExpandRowButton/useExpanderColumn.cjs +19 -0
  17. package/dist/Table/components/ExpandRowButton/useExpanderColumn.d.ts +6 -0
  18. package/dist/Table/components/ExpandRowButton/useExpanderColumn.js +19 -0
  19. package/dist/Table/components/ExpandRowButton/useExpanderColumn.test.d.ts +1 -0
  20. package/dist/Table/components/PluggableTable/compositionComponents.d.ts +5 -5
  21. package/dist/Table/components/RowSelection/HeaderMultiRowSelectionButton.cjs +1 -0
  22. package/dist/Table/components/RowSelection/HeaderMultiRowSelectionButton.d.ts +1 -1
  23. package/dist/Table/components/RowSelection/HeaderMultiRowSelectionButton.js +1 -0
  24. package/dist/Table/components/RowSelection/useRowSelectionColumn.cjs +26 -0
  25. package/dist/Table/components/RowSelection/useRowSelectionColumn.d.ts +8 -0
  26. package/dist/Table/components/RowSelection/useRowSelectionColumn.js +26 -0
  27. package/dist/Table/components/RowSelection/useRowSelectionColumn.test.d.ts +1 -0
  28. package/dist/Table/components/ShowOnRowHover/ShowOnRowHover.style.cjs +3 -1
  29. package/dist/Table/components/ShowOnRowHover/ShowOnRowHover.style.js +3 -1
  30. package/dist/Table/components/TableBody/TableBody.d.ts +1 -1
  31. package/dist/Table/components/TableBody/components/Compose/Compose.cjs +1 -3
  32. package/dist/Table/components/TableBody/components/Compose/Compose.js +1 -3
  33. package/dist/Table/components/TableBodyCell/TableBodyCell.cjs +1 -5
  34. package/dist/Table/components/TableBodyCell/TableBodyCell.d.ts +4 -7
  35. package/dist/Table/components/TableBodyCell/TableBodyCell.js +1 -5
  36. package/dist/Table/components/TableBodyCell/components/Compose/Compose.cjs +21 -1
  37. package/dist/Table/components/TableBodyCell/components/Compose/Compose.d.ts +8 -2
  38. package/dist/Table/components/TableBodyCell/components/Compose/Compose.js +21 -1
  39. package/dist/Table/components/TableBodyCell/components/Compose/Compose.test.d.ts +1 -0
  40. package/dist/Table/components/TableBodyRow/TableBodyRow.cjs +12 -2
  41. package/dist/Table/components/TableBodyRow/TableBodyRow.d.ts +3 -3
  42. package/dist/Table/components/TableBodyRow/TableBodyRow.js +12 -2
  43. package/dist/Table/components/TableBodyRow/components/Compose/Compose.d.ts +2 -1
  44. package/dist/Table/components/TableHeader/TableHeader.d.ts +2 -2
  45. package/dist/Table/components/TableHeaderCell/SRSortingNotification.cjs +4 -5
  46. package/dist/Table/components/TableHeaderCell/SRSortingNotification.d.ts +1 -1
  47. package/dist/Table/components/TableHeaderCell/SRSortingNotification.js +4 -5
  48. package/dist/Table/components/TableHeaderCell/TableHeaderCell.cjs +2 -6
  49. package/dist/Table/components/TableHeaderCell/TableHeaderCell.d.ts +2 -2
  50. package/dist/Table/components/TableHeaderCell/TableHeaderCell.js +2 -6
  51. package/dist/Table/components/TableHeaderCell/components/Compose/Compose.cjs +12 -4
  52. package/dist/Table/components/TableHeaderCell/components/Compose/Compose.js +12 -4
  53. package/dist/Table/components/TableHeaderCell/components/Compose/Compose.test.d.ts +1 -0
  54. package/dist/Table/components/TableHeaderCell/components/Heading/Heading.cjs +6 -5
  55. package/dist/Table/components/TableHeaderCell/components/Heading/Heading.js +7 -6
  56. package/dist/Table/components/TableHeaderCell/components/SortableHeading/SortableHeading.d.ts +1 -1
  57. package/dist/Table/components/TableHeaderCell/components/SortableHeading/components/Compose/Compose.cjs +1 -0
  58. package/dist/Table/components/TableHeaderCell/components/SortableHeading/components/Compose/Compose.js +1 -0
  59. package/dist/Table/components/TableRoot/TableRoot.cjs +9 -1
  60. package/dist/Table/components/TableRoot/TableRoot.d.ts +2 -1
  61. package/dist/Table/components/TableRoot/TableRoot.js +9 -1
  62. package/dist/Table/plugins/ExpandableRowPlugin.cjs +11 -5
  63. package/dist/Table/plugins/ExpandableRowPlugin.d.ts +7 -1
  64. package/dist/Table/plugins/ExpandableRowPlugin.js +11 -5
  65. package/dist/Table/plugins/RowNavigationPlugin.cjs +139 -0
  66. package/dist/Table/plugins/RowNavigationPlugin.d.ts +44 -0
  67. package/dist/Table/plugins/RowNavigationPlugin.js +139 -0
  68. package/dist/Table/plugins/SortingPlugin.cjs +1 -1
  69. package/dist/Table/plugins/SortingPlugin.d.ts +1 -1
  70. package/dist/Table/plugins/SortingPlugin.js +1 -1
  71. package/dist/Table/tanStackExtendedTypes.d.ts +9 -0
  72. package/dist/Table/utils/plugin.utils.d.ts +1 -1
  73. package/dist/Table/utils/table.utils.cjs +13 -0
  74. package/dist/Table/utils/table.utils.d.ts +2 -0
  75. package/dist/Table/utils/table.utils.js +13 -0
  76. package/dist/Table/utils/table.utils.test.d.ts +1 -0
  77. package/package.json +4 -4
@@ -4,30 +4,38 @@ import { VisuallyHidden } from "../../../../../node_modules/@radix-ui/react-visu
4
4
  import { useMemo } from "react";
5
5
  import { normalizeCellSpan, getCurrentAriaSort } from "../../TableHeaderCell.utils.js";
6
6
  import { TableHeaderCellContextProvider } from "../../TableHeaderCell.context.js";
7
+ import { getWidth } from "../../../../utils/table.utils.js";
7
8
  import { TableTh } from "../../../../Table.style.js";
8
9
  const TableHeaderCellCompose = forwardRefWithGenerics(({
9
10
  header,
10
11
  children,
11
12
  ...restProps
12
13
  }, ref) => {
14
+ var _a, _b;
13
15
  const colSpan = normalizeCellSpan(header.colSpan);
14
16
  const rowSpan = normalizeCellSpan(header.rowSpan);
17
+ const {
18
+ column
19
+ } = header;
20
+ const columnCellProps = (_b = (_a = column.columnDef).getHeaderCellProps) == null ? void 0 : _b.call(_a, header);
15
21
  return jsxRuntimeExports.jsx(TableHeaderCellContextProvider, {
16
22
  value: useMemo(() => ({
17
23
  header
18
24
  }), [header]),
19
25
  children: jsxRuntimeExports.jsx(TableTh, {
20
26
  ...restProps,
27
+ ...columnCellProps,
21
28
  ref,
22
29
  colSpan,
23
30
  rowSpan,
24
31
  style: {
25
- width: header.column.getSize(),
26
- ...restProps.style
32
+ width: getWidth(column),
33
+ ...restProps.style,
34
+ ...columnCellProps == null ? void 0 : columnCellProps.style
27
35
  },
28
36
  "aria-hidden": header.isPlaceholder,
29
- onClick: header.column.getCanSort() ? header.column.getToggleSortingHandler() : void 0,
30
- "aria-sort": header.column.getCanSort() ? getCurrentAriaSort(header) : void 0,
37
+ onClick: column.getCanSort() ? column.getToggleSortingHandler() : void 0,
38
+ "aria-sort": column.getCanSort() ? getCurrentAriaSort(header) : void 0,
31
39
  scope: "col",
32
40
  children: header.isPlaceholder ? jsxRuntimeExports.jsx(VisuallyHidden, {
33
41
  children: "Placeholder"
@@ -8,15 +8,16 @@ const TableHeaderCell_context = require("../../TableHeaderCell.context.cjs");
8
8
  const Table_style = require("../../../../Table.style.cjs");
9
9
  const DEFAULT_HIDDEN_HEADER = "Actions";
10
10
  const TableHeaderCellHeading = (props) => {
11
- var _a;
12
11
  const {
13
12
  header
14
13
  } = TableHeaderCell_context.useTableHeaderCellContext();
15
14
  const content = index.flexRender(header.column.columnDef.header, header.getContext());
16
- if (header.column.columnDef.isHeaderCustom) return jsxRuntime.jsxRuntimeExports.jsx(jsxRuntime.jsxRuntimeExports.Fragment, {
17
- children: content
18
- });
19
- const text = (_a = redisUiComponents.childrenToString(content)) == null ? void 0 : _a.trim();
15
+ if (header.column.columnDef.isHeaderCustom) {
16
+ return jsxRuntime.jsxRuntimeExports.jsx(jsxRuntime.jsxRuntimeExports.Fragment, {
17
+ children: content
18
+ });
19
+ }
20
+ const text = typeof content === "string" ? content.trim() : content;
20
21
  if (!text || header.column.columnDef.isHeaderHidden) {
21
22
  return jsxRuntime.jsxRuntimeExports.jsx(index$1.VisuallyHidden, {
22
23
  children: text || DEFAULT_HIDDEN_HEADER
@@ -1,20 +1,21 @@
1
1
  import { j as jsxRuntimeExports } from "../../../../../node_modules/react/jsx-runtime.js";
2
2
  import { flexRender } from "../../../../../node_modules/@tanstack/react-table/build/lib/index.js";
3
- import { childrenToString, isTextualNode } from "@redislabsdev/redis-ui-components";
3
+ import { isTextualNode } from "@redislabsdev/redis-ui-components";
4
4
  import { VisuallyHidden } from "../../../../../node_modules/@radix-ui/react-visually-hidden/dist/index.js";
5
5
  import { useTableHeaderCellContext } from "../../TableHeaderCell.context.js";
6
6
  import { HeaderTitleWrapper } from "../../../../Table.style.js";
7
7
  const DEFAULT_HIDDEN_HEADER = "Actions";
8
8
  const TableHeaderCellHeading = (props) => {
9
- var _a;
10
9
  const {
11
10
  header
12
11
  } = useTableHeaderCellContext();
13
12
  const content = flexRender(header.column.columnDef.header, header.getContext());
14
- if (header.column.columnDef.isHeaderCustom) return jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, {
15
- children: content
16
- });
17
- const text = (_a = childrenToString(content)) == null ? void 0 : _a.trim();
13
+ if (header.column.columnDef.isHeaderCustom) {
14
+ return jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, {
15
+ children: content
16
+ });
17
+ }
18
+ const text = typeof content === "string" ? content.trim() : content;
18
19
  if (!text || header.column.columnDef.isHeaderHidden) {
19
20
  return jsxRuntimeExports.jsx(VisuallyHidden, {
20
21
  children: text || DEFAULT_HIDDEN_HEADER
@@ -1,7 +1,7 @@
1
1
  /// <reference types="react" />
2
2
  import { ComposeElementProps } from '@redislabsdev/redis-ui-components';
3
3
  export declare const SortableHeading: (({ children, ...restProps }: ComposeElementProps) => import("react/jsx-runtime").JSX.Element) & {
4
- Compose: ((props: ComposeElementProps<HTMLDivElement>) => import("react/jsx-runtime").JSX.Element) & {
4
+ Compose: ((props: ComposeElementProps<HTMLElement>) => import("react/jsx-runtime").JSX.Element) & {
5
5
  SortIndicator: () => import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>>;
6
6
  };
7
7
  SortIndicator: () => import("react").ReactElement<any, string | import("react").JSXElementConstructor<any>>;
@@ -14,6 +14,7 @@ const SortableHeadingCompose = Object.assign((props) => {
14
14
  {
15
15
  "aria-description": TableHeaderCell_utils.getColumnSortLabel(header, TableHeaderCell_utils.formatColumnSortNotification),
16
16
  ...props,
17
+ "$custom": header.column.columnDef.isHeaderCustom || false,
17
18
  as: header.column.getCanSort() ? void 0 : "div"
18
19
  }
19
20
  );
@@ -12,6 +12,7 @@ const SortableHeadingCompose = Object.assign((props) => {
12
12
  {
13
13
  "aria-description": getColumnSortLabel(header, formatColumnSortNotification),
14
14
  ...props,
15
+ "$custom": header.column.columnDef.isHeaderCustom || false,
15
16
  as: header.column.getCanSort() ? void 0 : "div"
16
17
  }
17
18
  );
@@ -1,5 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const jsxRuntime = require("../../../node_modules/react/jsx-runtime.cjs");
3
4
  const Table_style = require("../../Table.style.cjs");
4
- const TableRoot = Table_style.Table;
5
+ const RowNavigationPlugin = require("../../plugins/RowNavigationPlugin.cjs");
6
+ const TableRoot = (props) => {
7
+ const navigationProps = RowNavigationPlugin.useHandleRowNavigationOnTable(props);
8
+ return jsxRuntime.jsxRuntimeExports.jsx(Table_style.Table, {
9
+ ...props,
10
+ ...navigationProps
11
+ });
12
+ };
5
13
  exports.TableRoot = TableRoot;
@@ -1 +1,2 @@
1
- export declare const TableRoot: import("styled-components").StyledComponent<"table", any, {}, never>;
1
+ import { HTMLAttributes } from 'react';
2
+ export declare const TableRoot: (props: HTMLAttributes<HTMLTableElement>) => import("react/jsx-runtime").JSX.Element;
@@ -1,5 +1,13 @@
1
+ import { j as jsxRuntimeExports } from "../../../node_modules/react/jsx-runtime.js";
1
2
  import { Table } from "../../Table.style.js";
2
- const TableRoot = Table;
3
+ import { useHandleRowNavigationOnTable } from "../../plugins/RowNavigationPlugin.js";
4
+ const TableRoot = (props) => {
5
+ const navigationProps = useHandleRowNavigationOnTable(props);
6
+ return jsxRuntimeExports.jsx(Table, {
7
+ ...props,
8
+ ...navigationProps
9
+ });
10
+ };
3
11
  export {
4
12
  TableRoot
5
13
  };
@@ -7,21 +7,27 @@ const index = require("../../node_modules/@tanstack/table-core/build/lib/index.c
7
7
  const getTrue = () => true;
8
8
  const TableExpandableRowContextKey = "expandableRow";
9
9
  const useExpandableRowPlugin = ({
10
+ defaultExpanded,
10
11
  getRowCanExpand,
11
12
  renderExpandedRow,
12
13
  expandRowOnClick,
13
- expandedRowComponent
14
+ expandedRowComponent,
15
+ getSubRows
14
16
  }) => {
15
17
  const clickablePluginData = ClickableRowPlugin.useClickableRowPlugin({
16
18
  getCanRowClick: (row) => !!(expandRowOnClick && row.getCanExpand()),
17
19
  onRowClick: (row) => row.toggleExpanded(),
18
20
  shouldClickOnEnter: true
19
21
  });
20
- const isExpandable = !!renderExpandedRow;
21
- return isExpandable ? {
22
+ if (!renderExpandedRow && !getSubRows) return void 0;
23
+ return {
22
24
  tableOptions: {
23
25
  getExpandedRowModel: index.getExpandedRowModel(),
24
- getRowCanExpand: getRowCanExpand || getTrue
26
+ getRowCanExpand: getRowCanExpand || (getSubRows ? void 0 : getTrue),
27
+ getSubRows,
28
+ initialState: defaultExpanded ? {
29
+ expanded: defaultExpanded
30
+ } : void 0
25
31
  },
26
32
  context: {
27
33
  ...plugin_utils.buildPluginContext(TableExpandableRowContextKey, {
@@ -30,7 +36,7 @@ const useExpandableRowPlugin = ({
30
36
  }),
31
37
  ...expandRowOnClick ? clickablePluginData == null ? void 0 : clickablePluginData.context : null
32
38
  }
33
- } : void 0;
39
+ };
34
40
  };
35
41
  const useExpandableRowContext = () => Table_context.usePluginContext(TableExpandableRowContextKey);
36
42
  exports.TableExpandableRowContextKey = TableExpandableRowContextKey;
@@ -1,5 +1,6 @@
1
1
  import { ComponentType, ReactNode } from 'react';
2
2
  import { type Row } from '@tanstack/react-table';
3
+ import { ExpandedState } from '@tanstack/table-core/src/features/RowExpanding';
3
4
  import { TablePluginData } from '../utils/plugin.utils';
4
5
  import type { TableExpandedRowProps } from '../components/TableExpandedRow';
5
6
  export declare const TableExpandableRowContextKey = "expandableRow";
@@ -17,6 +18,11 @@ export interface TableExpandableRowParams<T extends object> extends TableExpanda
17
18
  expandRowOnClick?: boolean;
18
19
  /** Allows individual rows to be expandable */
19
20
  getRowCanExpand?: (row: Row<T>) => boolean;
21
+ /**
22
+ * Enables tree-list (hierarchical) data.<br/>
23
+ * When this prop is set, the `renderExpandedRow` and `expandedRowComponent` parameters are not used. */
24
+ getSubRows?: (rowData: T, index: number) => T[] | undefined;
25
+ defaultExpanded?: ExpandedState;
20
26
  }
21
- export declare const useExpandableRowPlugin: <T extends object>({ getRowCanExpand, renderExpandedRow, expandRowOnClick, expandedRowComponent }: TableExpandableRowParams<T>) => TablePluginData<T>;
27
+ export declare const useExpandableRowPlugin: <T extends object>({ defaultExpanded, getRowCanExpand, renderExpandedRow, expandRowOnClick, expandedRowComponent, getSubRows }: TableExpandableRowParams<T>) => TablePluginData<T>;
22
28
  export declare const useExpandableRowContext: <T extends object>() => TableExpandableRowContext<T> | undefined;
@@ -5,21 +5,27 @@ import { getExpandedRowModel } from "../../node_modules/@tanstack/table-core/bui
5
5
  const getTrue = () => true;
6
6
  const TableExpandableRowContextKey = "expandableRow";
7
7
  const useExpandableRowPlugin = ({
8
+ defaultExpanded,
8
9
  getRowCanExpand,
9
10
  renderExpandedRow,
10
11
  expandRowOnClick,
11
- expandedRowComponent
12
+ expandedRowComponent,
13
+ getSubRows
12
14
  }) => {
13
15
  const clickablePluginData = useClickableRowPlugin({
14
16
  getCanRowClick: (row) => !!(expandRowOnClick && row.getCanExpand()),
15
17
  onRowClick: (row) => row.toggleExpanded(),
16
18
  shouldClickOnEnter: true
17
19
  });
18
- const isExpandable = !!renderExpandedRow;
19
- return isExpandable ? {
20
+ if (!renderExpandedRow && !getSubRows) return void 0;
21
+ return {
20
22
  tableOptions: {
21
23
  getExpandedRowModel: getExpandedRowModel(),
22
- getRowCanExpand: getRowCanExpand || getTrue
24
+ getRowCanExpand: getRowCanExpand || (getSubRows ? void 0 : getTrue),
25
+ getSubRows,
26
+ initialState: defaultExpanded ? {
27
+ expanded: defaultExpanded
28
+ } : void 0
23
29
  },
24
30
  context: {
25
31
  ...buildPluginContext(TableExpandableRowContextKey, {
@@ -28,7 +34,7 @@ const useExpandableRowPlugin = ({
28
34
  }),
29
35
  ...expandRowOnClick ? clickablePluginData == null ? void 0 : clickablePluginData.context : null
30
36
  }
31
- } : void 0;
37
+ };
32
38
  };
33
39
  const useExpandableRowContext = () => usePluginContext(TableExpandableRowContextKey);
34
40
  export {
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const React = require("react");
4
+ const redisUiComponents = require("@redislabsdev/redis-ui-components");
5
+ const index = require("../../node_modules/@radix-ui/react-compose-refs/dist/index.cjs");
6
+ const plugin_utils = require("../utils/plugin.utils.cjs");
7
+ const Table_context = require("../Table.context.cjs");
8
+ const TableRowNavigationContextKey = "RowNavigation";
9
+ const useRowNavigationPlugin = ({
10
+ enableRowNavigation = true
11
+ }) => {
12
+ const [activeRowId, setActiveRowId] = React.useState();
13
+ const rowRefs = React.useRef(/* @__PURE__ */ new Map());
14
+ const unregisterRow = React.useCallback((rowId) => {
15
+ rowRefs.current.delete(rowId);
16
+ }, []);
17
+ const registerRow = React.useCallback((rowId, rowEl) => {
18
+ if (rowEl) rowRefs.current.set(rowId, rowEl);
19
+ else unregisterRow(rowId);
20
+ }, [unregisterRow]);
21
+ const goToRowId = (rowId, noScroll) => {
22
+ var _a;
23
+ return rowId && ((_a = rowRefs.current.get(rowId)) == null ? void 0 : _a.focus({
24
+ preventScroll: noScroll
25
+ }));
26
+ };
27
+ const getGoToRow = (getTargetRow) => (table) => {
28
+ const {
29
+ rows
30
+ } = table.getRowModel();
31
+ if (!rows.length) return false;
32
+ const targetRow = getTargetRow(rows);
33
+ if (!targetRow) return false;
34
+ setActiveRowId(targetRow.id);
35
+ goToRowId(targetRow.id);
36
+ return true;
37
+ };
38
+ const findActiveIndex = (rows) => rows.findIndex((row) => row.id === activeRowId);
39
+ const goToNextRow = getGoToRow((rows) => activeRowId && rows[findActiveIndex(rows) + 1] || rows.at(0));
40
+ const goToPrevRow = getGoToRow((rows) => activeRowId && rows[findActiveIndex(rows) - 1] || rows.at(-1));
41
+ const goToFirstRow = getGoToRow((rows) => rows.at(0));
42
+ const goToLastRow = getGoToRow((rows) => rows.at(-1));
43
+ return enableRowNavigation ? {
44
+ tableOptions: {},
45
+ context: plugin_utils.buildPluginContext(TableRowNavigationContextKey, {
46
+ registerRow,
47
+ unregisterRow,
48
+ activeRowId,
49
+ setActiveRowId,
50
+ goToNextRow,
51
+ goToPrevRow,
52
+ goToFirstRow,
53
+ goToLastRow,
54
+ goToRowId
55
+ })
56
+ } : void 0;
57
+ };
58
+ const useRowNavigationContext = () => Table_context.usePluginContext(TableRowNavigationContextKey);
59
+ const useHandleRowNavigationOnRow = ({
60
+ row,
61
+ onFocus,
62
+ ref
63
+ }) => {
64
+ const {
65
+ registerRow,
66
+ setActiveRowId,
67
+ activeRowId
68
+ } = useRowNavigationContext() || {};
69
+ const innerRowRef = registerRow && ((rowEl) => registerRow(row.id, rowEl));
70
+ const composedRowRef = index.useComposedRefs(ref, innerRowRef);
71
+ const handleFocus = redisUiComponents.combineHandlers(setActiveRowId && (() => setActiveRowId(row.id)), onFocus);
72
+ return {
73
+ ref: composedRowRef,
74
+ onFocus: handleFocus,
75
+ ...setActiveRowId && {
76
+ tabIndex: activeRowId === row.id ? 0 : -1
77
+ }
78
+ };
79
+ };
80
+ const useHandleRowNavigationOnTable = ({
81
+ onKeyDown,
82
+ onFocus
83
+ }) => {
84
+ const {
85
+ table
86
+ } = Table_context.useTableContext();
87
+ const nav = useRowNavigationContext();
88
+ if (!nav) {
89
+ return {
90
+ ...onKeyDown && {
91
+ onKeyDown
92
+ },
93
+ ...onFocus && {
94
+ onFocus
95
+ }
96
+ };
97
+ }
98
+ const {
99
+ goToNextRow,
100
+ goToPrevRow,
101
+ goToFirstRow,
102
+ goToLastRow,
103
+ goToRowId,
104
+ activeRowId
105
+ } = nav;
106
+ const handleKeyDown = redisUiComponents.combineHandlers((e) => {
107
+ if (e.key === "ArrowDown") {
108
+ e.preventDefault();
109
+ goToNextRow(table);
110
+ }
111
+ if (e.key === "ArrowUp") {
112
+ e.preventDefault();
113
+ goToPrevRow(table);
114
+ }
115
+ if (e.key === "Home") {
116
+ e.preventDefault();
117
+ goToFirstRow(table);
118
+ }
119
+ if (e.key === "End") {
120
+ e.preventDefault();
121
+ goToLastRow(table);
122
+ }
123
+ }, onKeyDown);
124
+ const handleFocus = redisUiComponents.combineHandlers((e) => {
125
+ if (e.target === e.currentTarget) {
126
+ goToRowId(activeRowId, !e.target.matches(":focus-visible"));
127
+ }
128
+ }, onFocus);
129
+ return {
130
+ onKeyDown: handleKeyDown,
131
+ onFocus: handleFocus,
132
+ tabIndex: 0
133
+ };
134
+ };
135
+ exports.TableRowNavigationContextKey = TableRowNavigationContextKey;
136
+ exports.useHandleRowNavigationOnRow = useHandleRowNavigationOnRow;
137
+ exports.useHandleRowNavigationOnTable = useHandleRowNavigationOnTable;
138
+ exports.useRowNavigationContext = useRowNavigationContext;
139
+ exports.useRowNavigationPlugin = useRowNavigationPlugin;
@@ -0,0 +1,44 @@
1
+ import { FocusEventHandler, KeyboardEventHandler, Ref } from 'react';
2
+ import { Row, Table } from '@tanstack/react-table';
3
+ import { TablePluginData } from '../utils/plugin.utils';
4
+ export declare const TableRowNavigationContextKey = "RowNavigation";
5
+ export type TableRowNavigationContext = {
6
+ registerRow: (rowId: string, rowEl: HTMLTableRowElement | null) => void;
7
+ unregisterRow: (rowId: string) => void;
8
+ activeRowId: string | undefined;
9
+ setActiveRowId: (rowId: string) => void;
10
+ goToNextRow: (table: Table<object>) => void;
11
+ goToPrevRow: (table: Table<object>) => void;
12
+ goToFirstRow: (table: Table<object>) => void;
13
+ goToLastRow: (table: Table<object>) => void;
14
+ goToRowId: (rowId: string | undefined, noScroll?: boolean) => void;
15
+ };
16
+ export type TableRowNavigationParams = {
17
+ enableRowNavigation?: boolean;
18
+ };
19
+ export declare const useRowNavigationPlugin: <T extends object>({ enableRowNavigation }: TableRowNavigationParams) => TablePluginData<T>;
20
+ export declare const useRowNavigationContext: () => TableRowNavigationContext | undefined;
21
+ type UseHandleRowNavigationOnRowParams<T extends object> = {
22
+ row: Row<T>;
23
+ onFocus?: FocusEventHandler<HTMLTableRowElement>;
24
+ ref?: Ref<HTMLTableRowElement>;
25
+ };
26
+ export declare const useHandleRowNavigationOnRow: <T extends object>({ row, onFocus, ref }: UseHandleRowNavigationOnRowParams<T>) => {
27
+ tabIndex?: number | undefined;
28
+ ref: (node: HTMLTableRowElement) => void;
29
+ onFocus: FocusEventHandler<HTMLTableRowElement> | undefined;
30
+ };
31
+ type UseHandleRowNavigationOnTableParams = {
32
+ onKeyDown?: KeyboardEventHandler<HTMLTableElement>;
33
+ onFocus?: FocusEventHandler<HTMLTableElement>;
34
+ };
35
+ export declare const useHandleRowNavigationOnTable: ({ onKeyDown, onFocus }: UseHandleRowNavigationOnTableParams) => {
36
+ onFocus?: FocusEventHandler<HTMLTableElement> | undefined;
37
+ onKeyDown?: KeyboardEventHandler<HTMLTableElement> | undefined;
38
+ tabIndex?: undefined;
39
+ } | {
40
+ onKeyDown: KeyboardEventHandler<HTMLTableElement> | undefined;
41
+ onFocus: FocusEventHandler<HTMLTableElement> | undefined;
42
+ tabIndex: number;
43
+ };
44
+ export {};
@@ -0,0 +1,139 @@
1
+ import { useState, useRef, useCallback } from "react";
2
+ import { combineHandlers } from "@redislabsdev/redis-ui-components";
3
+ import { useComposedRefs } from "../../node_modules/@radix-ui/react-compose-refs/dist/index.js";
4
+ import { buildPluginContext } from "../utils/plugin.utils.js";
5
+ import { usePluginContext, useTableContext } from "../Table.context.js";
6
+ const TableRowNavigationContextKey = "RowNavigation";
7
+ const useRowNavigationPlugin = ({
8
+ enableRowNavigation = true
9
+ }) => {
10
+ const [activeRowId, setActiveRowId] = useState();
11
+ const rowRefs = useRef(/* @__PURE__ */ new Map());
12
+ const unregisterRow = useCallback((rowId) => {
13
+ rowRefs.current.delete(rowId);
14
+ }, []);
15
+ const registerRow = useCallback((rowId, rowEl) => {
16
+ if (rowEl) rowRefs.current.set(rowId, rowEl);
17
+ else unregisterRow(rowId);
18
+ }, [unregisterRow]);
19
+ const goToRowId = (rowId, noScroll) => {
20
+ var _a;
21
+ return rowId && ((_a = rowRefs.current.get(rowId)) == null ? void 0 : _a.focus({
22
+ preventScroll: noScroll
23
+ }));
24
+ };
25
+ const getGoToRow = (getTargetRow) => (table) => {
26
+ const {
27
+ rows
28
+ } = table.getRowModel();
29
+ if (!rows.length) return false;
30
+ const targetRow = getTargetRow(rows);
31
+ if (!targetRow) return false;
32
+ setActiveRowId(targetRow.id);
33
+ goToRowId(targetRow.id);
34
+ return true;
35
+ };
36
+ const findActiveIndex = (rows) => rows.findIndex((row) => row.id === activeRowId);
37
+ const goToNextRow = getGoToRow((rows) => activeRowId && rows[findActiveIndex(rows) + 1] || rows.at(0));
38
+ const goToPrevRow = getGoToRow((rows) => activeRowId && rows[findActiveIndex(rows) - 1] || rows.at(-1));
39
+ const goToFirstRow = getGoToRow((rows) => rows.at(0));
40
+ const goToLastRow = getGoToRow((rows) => rows.at(-1));
41
+ return enableRowNavigation ? {
42
+ tableOptions: {},
43
+ context: buildPluginContext(TableRowNavigationContextKey, {
44
+ registerRow,
45
+ unregisterRow,
46
+ activeRowId,
47
+ setActiveRowId,
48
+ goToNextRow,
49
+ goToPrevRow,
50
+ goToFirstRow,
51
+ goToLastRow,
52
+ goToRowId
53
+ })
54
+ } : void 0;
55
+ };
56
+ const useRowNavigationContext = () => usePluginContext(TableRowNavigationContextKey);
57
+ const useHandleRowNavigationOnRow = ({
58
+ row,
59
+ onFocus,
60
+ ref
61
+ }) => {
62
+ const {
63
+ registerRow,
64
+ setActiveRowId,
65
+ activeRowId
66
+ } = useRowNavigationContext() || {};
67
+ const innerRowRef = registerRow && ((rowEl) => registerRow(row.id, rowEl));
68
+ const composedRowRef = useComposedRefs(ref, innerRowRef);
69
+ const handleFocus = combineHandlers(setActiveRowId && (() => setActiveRowId(row.id)), onFocus);
70
+ return {
71
+ ref: composedRowRef,
72
+ onFocus: handleFocus,
73
+ ...setActiveRowId && {
74
+ tabIndex: activeRowId === row.id ? 0 : -1
75
+ }
76
+ };
77
+ };
78
+ const useHandleRowNavigationOnTable = ({
79
+ onKeyDown,
80
+ onFocus
81
+ }) => {
82
+ const {
83
+ table
84
+ } = useTableContext();
85
+ const nav = useRowNavigationContext();
86
+ if (!nav) {
87
+ return {
88
+ ...onKeyDown && {
89
+ onKeyDown
90
+ },
91
+ ...onFocus && {
92
+ onFocus
93
+ }
94
+ };
95
+ }
96
+ const {
97
+ goToNextRow,
98
+ goToPrevRow,
99
+ goToFirstRow,
100
+ goToLastRow,
101
+ goToRowId,
102
+ activeRowId
103
+ } = nav;
104
+ const handleKeyDown = combineHandlers((e) => {
105
+ if (e.key === "ArrowDown") {
106
+ e.preventDefault();
107
+ goToNextRow(table);
108
+ }
109
+ if (e.key === "ArrowUp") {
110
+ e.preventDefault();
111
+ goToPrevRow(table);
112
+ }
113
+ if (e.key === "Home") {
114
+ e.preventDefault();
115
+ goToFirstRow(table);
116
+ }
117
+ if (e.key === "End") {
118
+ e.preventDefault();
119
+ goToLastRow(table);
120
+ }
121
+ }, onKeyDown);
122
+ const handleFocus = combineHandlers((e) => {
123
+ if (e.target === e.currentTarget) {
124
+ goToRowId(activeRowId, !e.target.matches(":focus-visible"));
125
+ }
126
+ }, onFocus);
127
+ return {
128
+ onKeyDown: handleKeyDown,
129
+ onFocus: handleFocus,
130
+ tabIndex: 0
131
+ };
132
+ };
133
+ export {
134
+ TableRowNavigationContextKey,
135
+ useHandleRowNavigationOnRow,
136
+ useHandleRowNavigationOnTable,
137
+ useRowNavigationContext,
138
+ useRowNavigationPlugin
139
+ };
@@ -13,7 +13,7 @@ const useSortingPlugin = ({
13
13
  manualSorting = false,
14
14
  enableSorting = true
15
15
  // added plugin is enabled by default
16
- }) => {
16
+ } = {}) => {
17
17
  const [sortedByUser, setSortedByUser] = React.useState(false);
18
18
  const [sortingState, setSortingState] = index.useControllableState({
19
19
  prop: sorting,
@@ -14,5 +14,5 @@ export interface TableSortingParams {
14
14
  /** Set false to disable sorting */
15
15
  enableSorting?: boolean;
16
16
  }
17
- export declare const useSortingPlugin: <T extends object>({ defaultSorting, onSortingChange, sorting, manualSorting, enableSorting }: TableSortingParams) => TablePluginData<T>;
17
+ export declare const useSortingPlugin: <T extends object>({ defaultSorting, onSortingChange, sorting, manualSorting, enableSorting }?: TableSortingParams) => TablePluginData<T>;
18
18
  export declare const useSortingContext: () => TableSortingContext | undefined;
@@ -11,7 +11,7 @@ const useSortingPlugin = ({
11
11
  manualSorting = false,
12
12
  enableSorting = true
13
13
  // added plugin is enabled by default
14
- }) => {
14
+ } = {}) => {
15
15
  const [sortedByUser, setSortedByUser] = useState(false);
16
16
  const [sortingState, setSortingState] = useControllableState({
17
17
  prop: sorting,
@@ -1,11 +1,20 @@
1
1
  import '@tanstack/table-core';
2
+ import { Cell, Header } from '@tanstack/react-table';
3
+ import { HTMLAttributes } from 'react';
2
4
  declare module '@tanstack/table-core' {
5
+ type CellSizingUnits = 'fraction' | 'px' | 'rem';
3
6
  interface StringHeaderIdentifier {
4
7
  isHeaderHidden?: boolean;
5
8
  isHeaderCustom?: boolean;
9
+ sizeUnit?: CellSizingUnits;
10
+ getHeaderCellProps?: VoidFunction;
11
+ getCellProps?: VoidFunction;
6
12
  }
7
13
  interface IdIdentifier<TData, TValue> {
8
14
  isHeaderHidden?: boolean;
9
15
  isHeaderCustom?: boolean;
16
+ sizeUnit?: CellSizingUnits;
17
+ getHeaderCellProps?: (header: Header<TData, unknown>) => HTMLAttributes<HTMLTableCellElement> & Record<string, unknown>;
18
+ getCellProps?: (data: TData, cell: Cell<TData, unknown>) => HTMLAttributes<HTMLTableCellElement> & Record<string, unknown>;
10
19
  }
11
20
  }
@@ -1,6 +1,6 @@
1
1
  import { TableOptions } from '@tanstack/table-core';
2
2
  import { RowData } from '@tanstack/react-table';
3
- export type PartialTableOptions<T extends RowData> = Partial<Omit<TableOptions<T>, 'columns' | 'data' | 'getRowId' | 'getSubRows' | 'onStateChange' | 'getCoreRowModel' | 'defaultColumn' | 'debugTable' | 'debugRows' | 'debugHeaders' | 'debugColumns' | 'debugCells' | 'debugAll' | 'autoResetAll' | 'meta'>>;
3
+ export type PartialTableOptions<T extends RowData> = Partial<Omit<TableOptions<T>, 'columns' | 'data' | 'getRowId' | 'onStateChange' | 'getCoreRowModel' | 'defaultColumn' | 'debugTable' | 'debugRows' | 'debugHeaders' | 'debugColumns' | 'debugCells' | 'debugAll' | 'autoResetAll' | 'meta'>>;
4
4
  export type TablePluginData<T extends RowData> = {
5
5
  /** Partial table options that will be smartly merged with other table options */
6
6
  tableOptions: PartialTableOptions<T>;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const getWidth = (column) => {
4
+ const size = column.getSize();
5
+ if (!size) return 0;
6
+ if (column.getCanResize()) return size;
7
+ const {
8
+ sizeUnit
9
+ } = column.columnDef;
10
+ const unit = !sizeUnit || sizeUnit === "fraction" ? "%" : sizeUnit;
11
+ return `${size}${unit}`;
12
+ };
13
+ exports.getWidth = getWidth;