Skip to content

Commit a1aae13

Browse files
authored
feat: add rowHoverable to disable hover interaction (#1068)
* feat: Table add disabledHover * test: add disabledHover case * docs: add disabledHover * feat: add demo * feat: rename disabledHover to rowHoverable * fix: demo
1 parent 9e83836 commit a1aae13

File tree

7 files changed

+83
-3
lines changed

7 files changed

+83
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ React.render(<Table columns={columns} data={data} />, mountNode);
117117
| components | Object | | Override table elements, see [#171](https://github.com/react-component/table/pull/171) for more details |
118118
| sticky | boolean \| {offsetHeader?: number, offsetScroll?: number, getContainer?: () => Window \| HTMLElement } | false | stick header and scroll bar |
119119
| summary | (data: readonly RecordType[]) => React.ReactNode | - | `summary` attribute in `table` component is used to define the summary row. |
120+
| rowHoverable | boolean | true | Table hover interaction |
120121

121122
## Column Props
122123

docs/demo/row-hoverable.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: row-hoverable
3+
nav:
4+
title: Demo
5+
path: /demo
6+
---
7+
8+
<code src="../examples/row-hoverable.tsx"></code>

docs/examples/row-hoverable.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { TableProps } from 'rc-table';
2+
import Table from 'rc-table';
3+
import React from 'react';
4+
import '../../assets/index.less';
5+
6+
interface RecordType {
7+
a?: string;
8+
}
9+
10+
const data = [{ a: 'A' }, { a: 'B' }, { a: 'C' }];
11+
12+
const Demo = () => {
13+
const columns: TableProps<any>['columns'] = [
14+
{
15+
title: 'title',
16+
dataIndex: 'a',
17+
},
18+
];
19+
20+
return <Table<RecordType> columns={columns} data={data} rowHoverable={false} />;
21+
};
22+
23+
export default Demo;

src/Cell/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,10 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
118118
} = props;
119119

120120
const cellPrefixCls = `${prefixCls}-cell`;
121-
const { supportSticky, allColumnsFixedLeft } = useContext(TableContext, [
121+
const { supportSticky, allColumnsFixedLeft, rowHoverable } = useContext(TableContext, [
122122
'supportSticky',
123123
'allColumnsFixedLeft',
124+
'rowHoverable',
124125
]);
125126

126127
// ====================== Value =======================
@@ -244,8 +245,8 @@ function Cell<RecordType>(props: CellProps<RecordType>) {
244245
title={title}
245246
scope={scope}
246247
// Hover
247-
onMouseEnter={onMouseEnter}
248-
onMouseLeave={onMouseLeave}
248+
onMouseEnter={rowHoverable ? onMouseEnter : undefined}
249+
onMouseLeave={rowHoverable ? onMouseLeave : undefined}
249250
//Span
250251
colSpan={mergedColSpan !== 1 ? mergedColSpan : null}
251252
rowSpan={mergedRowSpan !== 1 ? mergedRowSpan : null}

src/Table.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ export interface TableProps<RecordType = unknown>
121121

122122
sticky?: boolean | TableSticky;
123123

124+
rowHoverable?: boolean;
125+
124126
// Events
125127
onScroll?: React.UIEventHandler<HTMLDivElement>;
126128

@@ -218,6 +220,7 @@ function Table<RecordType extends DefaultRecordType>(
218220
getContainerWidth,
219221

220222
sticky,
223+
rowHoverable = true,
221224
} = props;
222225

223226
const mergedData = data || EMPTY_DATA;
@@ -832,6 +835,8 @@ function Table<RecordType extends DefaultRecordType>(
832835
getRowKey,
833836
expandedKeys: mergedExpandedKeys,
834837
childrenColumnName: mergedChildrenColumnName,
838+
839+
rowHoverable,
835840
}),
836841
[
837842
// Scroll
@@ -879,6 +884,8 @@ function Table<RecordType extends DefaultRecordType>(
879884
getRowKey,
880885
mergedExpandedKeys,
881886
mergedChildrenColumnName,
887+
888+
rowHoverable,
882889
],
883890
);
884891

src/context/TableContext.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export interface TableContextProps<RecordType = any> {
6666
expandedKeys: Set<React.Key>;
6767
getRowKey: GetRowKey<RecordType>;
6868
childrenColumnName: string;
69+
70+
rowHoverable?: boolean;
6971
}
7072

7173
const TableContext = createContext<TableContextProps>();

tests/Table.spec.jsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,44 @@ describe('Table.Basic', () => {
11421142
expect(wrapper.find('td.rc-table-cell-row-hover')).toHaveLength(1);
11431143
});
11441144
});
1145+
1146+
it('when rowHoverable is false', () => {
1147+
const tColumns = [
1148+
{
1149+
title: 'Key',
1150+
dataIndex: 'key',
1151+
},
1152+
];
1153+
1154+
const tData = [
1155+
{ key: 'row0', children: [{ key: 'row0-0' }, { key: 'row0-1' }] },
1156+
{ key: 'row1', children: [{ key: 'row1-0' }, { key: 'row1-1' }] },
1157+
];
1158+
const wrapper = mount(
1159+
<Table
1160+
columns={tColumns}
1161+
expandable={{ defaultExpandAllRows: true }}
1162+
data={tData}
1163+
rowHoverable={false}
1164+
/>,
1165+
);
1166+
1167+
const trs = wrapper.find('tr.rc-table-row');
1168+
1169+
trs.forEach((tr, index) => {
1170+
tr.find('td.rc-table-cell').at(0).simulate('mouseEnter');
1171+
const currentClassName = wrapper
1172+
.find('tr.rc-table-row')
1173+
.at(index)
1174+
.find('td.rc-table-cell')
1175+
.at(0)
1176+
.getElement().props.className;
1177+
1178+
expect(currentClassName.includes('rc-table-cell-row-hover')).toEqual(false);
1179+
expect(wrapper.find('td.rc-table-cell-row-hover')).toHaveLength(0);
1180+
});
1181+
});
1182+
11451183
it('should get scrollbar size', () => {
11461184
const tColumns = [{ title: 'Name', dataIndex: 'name', key: 'name', width: 100 }];
11471185
const wrapper = mount(

0 commit comments

Comments
 (0)