Skip to content

Commit 015ff2d

Browse files
authored
Add onRowClick/EnableRowClick and enable pkg.pr.new (#72)
Add onRowClick/EnableRowClick and enable pkg.pr.new
1 parent 019d83c commit 015ff2d

5 files changed

Lines changed: 69 additions & 16 deletions

File tree

.github/workflows/pkg.pr.new.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: PKG PR New
2+
on:
3+
workflow_dispatch:
4+
pull_request:
5+
types: [opened, synchronize, ready_for_review]
6+
push:
7+
branches:
8+
- main
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- uses: actions/setup-node@v4
19+
with:
20+
node-version: 20
21+
22+
- run: yarn --frozen-lockfile
23+
24+
- run: npx pkg-pr-new publish

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- property `onRowClick` in `ReactDataTable` in order to define a custom function to execute the row is clicked
13+
- property `enableRowClick` in `ReactDataTable` in order to define if a row should be clickable or not
14+
1015
## [5.6.0] - 2024-10-02
1116

1217
### Added

src/lib/ReactDataTable/ReactDataTable.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ const ReactDataTable = <TData, TFilter extends FilterModel = Record<string, neve
4141
withoutHeaderFilters = false,
4242
dragAndDropOptions,
4343
noEntriesMessage,
44+
onRowClick,
45+
enableRowClick,
4446
} = props;
4547

4648
const { pagination } = table.getState();
@@ -69,9 +71,11 @@ const ReactDataTable = <TData, TFilter extends FilterModel = Record<string, neve
6971
return enableDragAndDrop ? (
7072
<SortableContext items={table.getRowModel().rows.map((row) => row.id)} strategy={verticalListSortingStrategy}>
7173
{table.getRowModel().rows.map((row, index) => (
72-
<DraggableRow<TData>
74+
<DraggableRow<TData, TFilter>
7375
key={index}
7476
row={row}
77+
enableRowClick={enableRowClick as ReactDataTableProps<TData, TFilter>["enableRowClick"]}
78+
onRowClick={onRowClick as ReactDataTableProps<TData, TFilter>["onRowClick"]}
7579
enableRowSelection={enableRowSelection as boolean | ((row: Row<TData>) => boolean)}
7680
enableExpanding={enableExpanding as boolean | ((row: Row<TData>) => boolean)}
7781
rowStyle={rowStyle && rowStyle(row.original)}
@@ -82,9 +86,11 @@ const ReactDataTable = <TData, TFilter extends FilterModel = Record<string, neve
8286
) : (
8387
<>
8488
{table.getRowModel().rows.map((row, index) => (
85-
<InternalTableRow<TData>
89+
<InternalTableRow<TData, TFilter>
8690
key={index}
8791
row={row}
92+
enableRowClick={enableRowClick as ReactDataTableProps<TData, TFilter>["enableRowClick"]}
93+
onRowClick={onRowClick as ReactDataTableProps<TData, TFilter>["onRowClick"]}
8894
enableRowSelection={enableRowSelection as boolean | ((row: Row<TData>) => boolean)}
8995
enableExpanding={enableExpanding as boolean | ((row: Row<TData>) => boolean)}
9096
rowStyle={rowStyle && rowStyle(row.original)}

src/lib/ReactDataTable/ReactDataTableProps.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Table } from "@tanstack/react-table";
1+
import { Row, Table } from "@tanstack/react-table";
22
import { CSSProperties } from "react";
33
import { FilterModel } from "../types/TableState";
44
import { DragAndDropOptions } from "./DragAndDropOptions";
@@ -53,6 +53,20 @@ export interface ReactDataTableProps<TData, TFilter extends FilterModel> {
5353
*/
5454
rowStyle?: (row: TData) => CSSProperties;
5555

56+
/**
57+
* boolean indicating whether to enable click for specific row
58+
* Notice that this is ignored in case of rowSelection feature is enabled and fullRowSelectable is not provided as false
59+
* @param row the row model
60+
*/
61+
enableRowClick?: boolean | ((row: Row<TData>) => boolean);
62+
63+
/**
64+
* callback which gets triggered when the row is clicked
65+
* Notice that this is ignored in case of rowSelection feature is enabled and fullRowSelectable is not provided as false
66+
* @param row the row model
67+
*/
68+
onRowClick?: (row: Row<TData>) => void | Promise<void>;
69+
5670
/**
5771
* boolean flag to indicate if the paging should be displayed
5872
*/

src/lib/ReactDataTable/TableRows.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import { Row, flexRender } from "@tanstack/react-table";
33
import { CSSProperties } from "react";
44
import { CSS } from "@dnd-kit/utilities";
5+
import { FilterModel } from "../types/TableState";
6+
import { ReactDataTableProps } from "./ReactDataTableProps";
57

6-
interface TableRowProps<TData> {
8+
interface TableRowProps<TData, TFilter extends FilterModel = Record<string, never>>
9+
extends Pick<ReactDataTableProps<TData, TFilter>, "onRowClick" | "enableRowClick"> {
710
row: Row<TData>;
811
enableRowSelection?: boolean | ((row: Row<TData>) => boolean);
912
fullRowSelectable?: boolean;
@@ -12,21 +15,25 @@ interface TableRowProps<TData> {
1215
setNodeRef?: (node: HTMLElement | null) => void;
1316
}
1417

15-
const InternalTableRow = <TData,>(props: TableRowProps<TData>) => {
16-
const { row, rowStyle, setNodeRef, enableRowSelection = false, fullRowSelectable = true } = props;
18+
const InternalTableRow = <TData, TFilter extends FilterModel = Record<string, never>>(props: TableRowProps<TData, TFilter>) => {
19+
const { row, rowStyle, setNodeRef, enableRowSelection = false, fullRowSelectable = true, onRowClick, enableRowClick } = props;
1720
const isRowSelectionEnabled =
1821
(typeof enableRowSelection === "function" ? enableRowSelection(row) : enableRowSelection) && fullRowSelectable;
19-
22+
const isRowClickable = typeof enableRowClick === "function" ? enableRowClick(row) : enableRowClick;
2023
return (
2124
<tr
2225
key={row.id}
2326
ref={setNodeRef}
24-
onClick={() => {
27+
onClick={async () => {
2528
if (isRowSelectionEnabled) {
2629
row.toggleSelected();
30+
} else if (isRowClickable && onRowClick) {
31+
await onRowClick(row);
32+
} else {
33+
// Nothing to execute
2734
}
2835
}}
29-
className={isRowSelectionEnabled ? "cursor-pointer" : undefined}
36+
className={isRowSelectionEnabled || isRowClickable ? "cursor-pointer" : undefined}
3037
style={rowStyle}
3138
>
3239
{row.getVisibleCells().map((cell) => (
@@ -38,8 +45,8 @@ const InternalTableRow = <TData,>(props: TableRowProps<TData>) => {
3845
);
3946
};
4047

41-
const DraggableRow = <TData,>(props: TableRowProps<TData>) => {
42-
const { row, rowStyle, enableRowSelection, fullRowSelectable, enableExpanding } = props;
48+
const DraggableRow = <TData, TFilter extends FilterModel = Record<string, never>>(props: TableRowProps<TData, TFilter>) => {
49+
const { row, rowStyle } = props;
4350
const { transform, transition, setNodeRef, isDragging } = useSortable({ id: row.id });
4451
const draggableStyle: CSSProperties = {
4552
transform: CSS.Transform.toString(transform),
@@ -49,11 +56,8 @@ const DraggableRow = <TData,>(props: TableRowProps<TData>) => {
4956
position: "relative",
5057
};
5158
return (
52-
<InternalTableRow
53-
row={row}
54-
enableRowSelection={enableRowSelection}
55-
enableExpanding={enableExpanding}
56-
fullRowSelectable={fullRowSelectable}
59+
<InternalTableRow<TData, TFilter>
60+
{...props}
5761
setNodeRef={setNodeRef}
5862
rowStyle={rowStyle ? { ...rowStyle, ...draggableStyle } : draggableStyle}
5963
/>

0 commit comments

Comments
 (0)