Skip to content

Commit 14e2e7f

Browse files
gsimkoGabor Simko
and
Gabor Simko
authored
Add onRowMouseEnter and onRowMouseLeave event handlers (#1027)
* Add onRowMouseEnter and onRowMouseLeave event handlers * format w/ prettier * undo change in settings.json * change version to 7.5.0 Co-authored-by: Gabor Simko <[email protected]>
1 parent c927014 commit 14e2e7f

File tree

7 files changed

+56
-1
lines changed

7 files changed

+56
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-data-table-component",
3-
"version": "7.4.7",
3+
"version": "7.5.0",
44
"description": "A simple to use declarative react based data table",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.es.js",

src/DataTable/DataTable.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
9393
expandableRows = defaultProps.expandableRows,
9494
onRowClicked = defaultProps.onRowClicked,
9595
onRowDoubleClicked = defaultProps.onRowDoubleClicked,
96+
onRowMouseEnter = defaultProps.onRowMouseEnter,
97+
onRowMouseLeave = defaultProps.onRowMouseLeave,
9698
sortIcon = defaultProps.sortIcon,
9799
onSort = defaultProps.onSort,
98100
sortFunction = defaultProps.sortFunction,
@@ -204,6 +206,10 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
204206

205207
const handleRowDoubleClicked = React.useCallback((row, e) => onRowDoubleClicked(row, e), [onRowDoubleClicked]);
206208

209+
const handleRowMouseEnter = React.useCallback((row, e) => onRowMouseEnter(row, e), [onRowMouseEnter]);
210+
211+
const handleRowMouseLeave = React.useCallback((row, e) => onRowMouseLeave(row, e), [onRowMouseLeave]);
212+
207213
const handleChangePage = React.useCallback(
208214
(page: number) =>
209215
dispatch({
@@ -454,6 +460,8 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
454460
onRowExpandToggled={onRowExpandToggled}
455461
onRowClicked={handleRowClicked}
456462
onRowDoubleClicked={handleRowDoubleClicked}
463+
onRowMouseEnter={handleRowMouseEnter}
464+
onRowMouseLeave={handleRowMouseLeave}
457465
onSelectedRow={handleSelectedRow}
458466
draggingColumnId={draggingColumnId}
459467
onDragStart={handleDragStart}

src/DataTable/TableRow.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ type DProps<T> = Pick<
6161
| 'keyField'
6262
| 'onRowClicked'
6363
| 'onRowDoubleClicked'
64+
| 'onRowMouseEnter'
65+
| 'onRowMouseLeave'
6466
| 'onRowExpandToggled'
6567
| 'pointerOnHover'
6668
| 'selectableRowDisabled'
@@ -109,6 +111,8 @@ function Row<T>({
109111
keyField,
110112
onRowClicked = noop,
111113
onRowDoubleClicked = noop,
114+
onRowMouseEnter = noop,
115+
onRowMouseLeave = noop,
112116
onRowExpandToggled = noop,
113117
onSelectedRow = noop,
114118
pointerOnHover = false,
@@ -169,6 +173,20 @@ function Row<T>({
169173
[defaultExpanderDisabled, expandOnRowDoubleClicked, expandableRows, handleExpanded, onRowDoubleClicked, row],
170174
);
171175

176+
const handleRowMouseEnter = React.useCallback(
177+
e => {
178+
onRowMouseEnter(row, e);
179+
},
180+
[onRowMouseEnter, row],
181+
);
182+
183+
const handleRowMouseLeave = React.useCallback(
184+
e => {
185+
onRowMouseLeave(row, e);
186+
},
187+
[onRowMouseLeave, row],
188+
);
189+
172190
const rowKeyField = prop(row as TableRow, keyField);
173191
const { style, classNames } = getConditionalStyle(row, conditionalRowStyles, ['rdt_TableRow']);
174192
const highlightSelected = selectableRowsHighlight && selected;
@@ -186,6 +204,8 @@ function Row<T>({
186204
dense={dense}
187205
onClick={handleRowClick}
188206
onDoubleClick={handleRowDoubleClick}
207+
onMouseEnter={handleRowMouseEnter}
208+
onMouseLeave={handleRowMouseLeave}
189209
className={classNames}
190210
selected={highlightSelected}
191211
style={style}

src/DataTable/__tests__/DataTable.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,27 @@ describe('DataTable:RowClicks', () => {
445445
});
446446
});
447447

448+
describe('DataTable:RowMouseEnterAndLeave', () => {
449+
test('should call onRowMouseEnter and onRowMouseLeave callbacks when row is entered/left', () => {
450+
const onRowMouseEnterMock = jest.fn();
451+
const onRowMouseLeaveMock = jest.fn();
452+
const mock = dataMock({});
453+
const { container } = render(
454+
<DataTable
455+
data={mock.data}
456+
columns={mock.columns}
457+
onRowMouseEnter={onRowMouseEnterMock}
458+
onRowMouseLeave={onRowMouseLeaveMock}
459+
/>,
460+
);
461+
462+
fireEvent.mouseEnter(container.querySelector('div[id="cell-1-1"]') as HTMLElement);
463+
expect(onRowMouseEnterMock).toHaveBeenCalled();
464+
fireEvent.mouseLeave(container.querySelector('div[id="cell-1-1"]') as HTMLElement);
465+
expect(onRowMouseLeaveMock).toHaveBeenCalled();
466+
});
467+
});
468+
448469
describe('DataTable::progress/nodata', () => {
449470
test('should render correctly when progressPending is true', () => {
450471
const mock = dataMock();

src/DataTable/defaultProps.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ export const defaultProps = {
9696
onChangeRowsPerPage: noop,
9797
onRowClicked: noop,
9898
onRowDoubleClicked: noop,
99+
onRowMouseEnter: noop,
100+
onRowMouseLeave: noop,
99101
onRowExpandToggled: noop,
100102
onSelectedRowsChange: noop,
101103
onSort: noop,

src/DataTable/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export type TableProps<T> = {
6666
onChangeRowsPerPage?: PaginationChangeRowsPerPage;
6767
onRowClicked?: (row: T, e: React.MouseEvent) => void;
6868
onRowDoubleClicked?: (row: T, e: React.MouseEvent) => void;
69+
onRowMouseEnter?: (row: T, e: React.MouseEvent) => void;
70+
onRowMouseLeave?: (row: T, e: React.MouseEvent) => void;
6971
onRowExpandToggled?: ExpandRowToggled<T>;
7072
onSelectedRowsChange?: (selected: { allSelected: boolean; selectedCount: number; selectedRows: T[] }) => void;
7173
onSort?: (selectedColumn: TableColumn<T>, sortDirection: SortOrder) => void;

stories/props.stories.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import { Meta } from '@storybook/addon-docs';
2222
| direction | string | no | auto | Accepts: `ltr`, `rtl`, or `auto`. When set to `auto` (default), RDT will attempt to detect direction by checking the HTML and DIV tags. For cases where you need to force rtl, or ltr just set this option manually (i.e. SSR).<br /><br />If you are using Typescript or prefer enums you can `import { Direction } from 'react-data-table-component';` and use `Direction.AUTO, Direction.LTR, or Direction.RTL |
2323
| onRowClicked | `(row, event) => {}` | no | | Callback to access the row, event on row click.<br /> <br /><br />**Note:** If you are using custom cells (`column[].cell`), you will need to apply the `data-tag="allowRowEvents"` to the innermost element or on the elements you want to propagate the `onRowClicked` event to. |
2424
| onRowDoubleClicked | `(row, event) => {}` | no | | Callback to access the row, event on row double click.<br /><br />**Note:** If you are using custom cells (`column[].cell`), you will need to apply the `data-tag="allowRowEvents"` to the innermost element or on the elements you want to propagate the `onRowDoubleClicked` event to. |
25+
| onRowMouseEnter | `(row, event) => {}` | no | | Callback to access the row, event on the mouse entering the row.
26+
| onRowMouseLeave | `(row, event) => {}` | no | | Callback to access the row, event on the mouse leaving the row.
2527

2628
# Columns
2729

0 commit comments

Comments
 (0)