Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onRowMouseEnter and onRowMouseLeave event handlers #1027

Merged
merged 5 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
"editor.defaultFormatter": "vscode.typescript-language-features"
jbetancur marked this conversation as resolved.
Show resolved Hide resolved
},
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-data-table-component",
"version": "7.4.7",
"version": "7.4.8",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new feature so it should be 7.5.0

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last comment and this should be GTG

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"description": "A simple to use declarative react based data table",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
Expand Down
8 changes: 8 additions & 0 deletions src/DataTable/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
expandableRows = defaultProps.expandableRows,
onRowClicked = defaultProps.onRowClicked,
onRowDoubleClicked = defaultProps.onRowDoubleClicked,
onRowMouseEnter = defaultProps.onRowMouseEnter,
onRowMouseLeave = defaultProps.onRowMouseLeave,
sortIcon = defaultProps.sortIcon,
onSort = defaultProps.onSort,
sortFunction = defaultProps.sortFunction,
Expand Down Expand Up @@ -204,6 +206,10 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {

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

const handleRowMouseEnter = React.useCallback((row, e) => onRowMouseEnter(row, e), [onRowMouseEnter]);

const handleRowMouseLeave = React.useCallback((row, e) => onRowMouseLeave(row, e), [onRowMouseLeave]);

const handleChangePage = React.useCallback(
(page: number) =>
dispatch({
Expand Down Expand Up @@ -454,6 +460,8 @@ function DataTable<T>(props: TableProps<T>): JSX.Element {
onRowExpandToggled={onRowExpandToggled}
onRowClicked={handleRowClicked}
onRowDoubleClicked={handleRowDoubleClicked}
onRowMouseEnter={handleRowMouseEnter}
onRowMouseLeave={handleRowMouseLeave}
onSelectedRow={handleSelectedRow}
draggingColumnId={draggingColumnId}
onDragStart={handleDragStart}
Expand Down
20 changes: 20 additions & 0 deletions src/DataTable/TableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type DProps<T> = Pick<
| 'keyField'
| 'onRowClicked'
| 'onRowDoubleClicked'
| 'onRowMouseEnter'
| 'onRowMouseLeave'
| 'onRowExpandToggled'
| 'pointerOnHover'
| 'selectableRowDisabled'
Expand Down Expand Up @@ -109,6 +111,8 @@ function Row<T>({
keyField,
onRowClicked = noop,
onRowDoubleClicked = noop,
onRowMouseEnter = noop,
onRowMouseLeave = noop,
onRowExpandToggled = noop,
onSelectedRow = noop,
pointerOnHover = false,
Expand Down Expand Up @@ -169,6 +173,20 @@ function Row<T>({
[defaultExpanderDisabled, expandOnRowDoubleClicked, expandableRows, handleExpanded, onRowDoubleClicked, row],
);

const handleRowMouseEnter = React.useCallback(
e => {
onRowMouseEnter(row, e);
},
[onRowMouseEnter, row],
);

const handleRowMouseLeave = React.useCallback(
e => {
onRowMouseLeave(row, e);
},
[onRowMouseLeave, row],
);

const rowKeyField = prop(row as TableRow, keyField);
const { style, classNames } = getConditionalStyle(row, conditionalRowStyles, ['rdt_TableRow']);
const highlightSelected = selectableRowsHighlight && selected;
Expand All @@ -186,6 +204,8 @@ function Row<T>({
dense={dense}
onClick={handleRowClick}
onDoubleClick={handleRowDoubleClick}
onMouseEnter={handleRowMouseEnter}
onMouseLeave={handleRowMouseLeave}
className={classNames}
selected={highlightSelected}
style={style}
Expand Down
21 changes: 21 additions & 0 deletions src/DataTable/__tests__/DataTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,27 @@ describe('DataTable:RowClicks', () => {
});
});

describe('DataTable:RowMouseEnterAndLeave', () => {
test('should call onRowMouseEnter and onRowMouseLeave callbacks when row is entered/left', () => {
const onRowMouseEnterMock = jest.fn();
const onRowMouseLeaveMock = jest.fn();
const mock = dataMock({});
const { container } = render(
<DataTable
data={mock.data}
columns={mock.columns}
onRowMouseEnter={onRowMouseEnterMock}
onRowMouseLeave={onRowMouseLeaveMock}
/>,
);

fireEvent.mouseEnter(container.querySelector('div[id="cell-1-1"]') as HTMLElement);
expect(onRowMouseEnterMock).toHaveBeenCalled();
fireEvent.mouseLeave(container.querySelector('div[id="cell-1-1"]') as HTMLElement);
expect(onRowMouseLeaveMock).toHaveBeenCalled();
});
});

describe('DataTable::progress/nodata', () => {
test('should render correctly when progressPending is true', () => {
const mock = dataMock();
Expand Down
2 changes: 2 additions & 0 deletions src/DataTable/defaultProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export const defaultProps = {
onChangeRowsPerPage: noop,
onRowClicked: noop,
onRowDoubleClicked: noop,
onRowMouseEnter: noop,
onRowMouseLeave: noop,
onRowExpandToggled: noop,
onSelectedRowsChange: noop,
onSort: noop,
Expand Down
2 changes: 2 additions & 0 deletions src/DataTable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export type TableProps<T> = {
onChangeRowsPerPage?: PaginationChangeRowsPerPage;
onRowClicked?: (row: T, e: React.MouseEvent) => void;
onRowDoubleClicked?: (row: T, e: React.MouseEvent) => void;
onRowMouseEnter?: (row: T, e: React.MouseEvent) => void;
onRowMouseLeave?: (row: T, e: React.MouseEvent) => void;
onRowExpandToggled?: ExpandRowToggled<T>;
onSelectedRowsChange?: (selected: { allSelected: boolean; selectedCount: number; selectedRows: T[] }) => void;
onSort?: (selectedColumn: TableColumn<T>, sortDirection: SortOrder) => void;
Expand Down
2 changes: 2 additions & 0 deletions stories/props.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { Meta } from '@storybook/addon-docs';
| 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 |
| 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. |
| 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. |
| onRowMouseEnter | `(row, event) => {}` | no | | Callback to access the row, event on the mouse entering the row.
| onRowMouseLeave | `(row, event) => {}` | no | | Callback to access the row, event on the mouse leaving the row.

# Columns

Expand Down