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

fix: 修复自定义选中交互主题对行列叶子节点不生效 #3064

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 39 additions & 1 deletion packages/s2-core/__tests__/unit/interaction/root-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,33 @@ describe('RootInteraction Tests', () => {
});

test('should selected header cell', () => {
const highlightNodesSpy = jest
.spyOn(rootInteraction, 'highlightNodes')
.mockImplementationOnce(() => {});

rootInteraction.selectCell(mockCell);

expect(rootInteraction.getState()).toMatchSnapshot();
expect(rootInteraction.hasIntercepts([InterceptType.HOVER])).toBeTruthy();
expect(highlightNodesSpy).toHaveBeenCalledWith(
[],
InteractionStateName.SELECTED,
);
});

test('should highlight header cell', () => {
const highlightNodesSpy = jest
.spyOn(rootInteraction, 'highlightNodes')
.mockImplementationOnce(() => {});

rootInteraction.highlightCell(mockCell);

expect(rootInteraction.getState()).toMatchSnapshot();
expect(rootInteraction.hasIntercepts([InterceptType.HOVER])).toBeTruthy();
expect(highlightNodesSpy).toHaveBeenCalledWith(
[],
InteractionStateName.HOVER,
);
});

// https://github.com/antvis/S2/issues/1243
Expand Down Expand Up @@ -444,7 +460,7 @@ describe('RootInteraction Tests', () => {
expect(rootInteraction.getActiveCells()).toEqual([]);
});

test('should set selected status after highlight nodes', () => {
test('should set hover status after highlight nodes', () => {
const belongsCell = createMockCellInfo('test-A').mockCell;

const mockNodeA = new Node({
Expand All @@ -471,6 +487,28 @@ describe('RootInteraction Tests', () => {
});
});

// https://github.com/antvis/S2/discussions/3062
test('should set selected status after highlight nodes', () => {
const belongsCell = createMockCellInfo('test-A').mockCell;

const mockNodeA = new Node({
id: 'test',
field: 'test',
value: '1',
belongsCell,
});

rootInteraction.highlightNodes(
[mockNodeA],
InteractionStateName.SELECTED,
);

expect(mockNodeA.belongsCell?.updateByState).toHaveBeenCalledWith(
InteractionStateName.SELECTED,
belongsCell,
);
});

test.each`
stateName | handler
${InteractionStateName.SELECTED} | ${'isSelectedState'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class CornerCellClick extends BaseEvent implements BaseEventImplement {
cells,
stateName: InteractionStateName.SELECTED,
});
interaction.highlightNodes(nodes);
interaction.highlightNodes(nodes, InteractionStateName.SELECTED);
cornerCell?.updateByState(InteractionStateName.SELECTED);

this.showTooltip(event);
Expand Down
12 changes: 6 additions & 6 deletions packages/s2-core/src/interaction/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ export class RootInteraction {

// 平铺模式或者是树状模式下的列头单元格, 高亮子节点
if (!isHierarchyTree || isColCell) {
this.highlightNodes(childrenNodes);
this.highlightNodes(childrenNodes, stateName);
}

// 如果不在可视范围, 自动滚动
Expand All @@ -684,12 +684,12 @@ export class RootInteraction {
* 高亮节点对应的单元格
* @example s2.interaction.highlightNodes([node])
*/
public highlightNodes = (nodes: Node[] = []) => {
public highlightNodes = (
nodes: Node[] = [],
stateName: `${InteractionStateName}` = InteractionStateName.HOVER,
) => {
nodes.forEach((node) => {
node?.belongsCell?.updateByState(
InteractionStateName.HOVER,
node.belongsCell,
);
node?.belongsCell?.updateByState(stateName, node.belongsCell);
});
};

Expand Down
2 changes: 1 addition & 1 deletion s2-site/docs/api/basic-class/interaction.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ s2.interaction.reset()
| addIntercepts | 新增交互拦截 | (interceptTypes: [InterceptType](#intercepttype)[]) => void |
| hasIntercepts | 是否有指定拦截的交互 | (interceptTypes: [InterceptType](#intercepttype)[]) => boolean |
| removeIntercepts | 移除指定交互拦截 | (interceptTypes: [InterceptType](#intercepttype)[]) => void |
| highlightNodes | 高亮节点对应的单元格 | (nodes: [Node](/api/basic-class/node)[]) => void |
| highlightNodes | 高亮节点对应的单元格 | (nodes: [Node](/api/basic-class/node)[], stateName: [InteractionStateName](#interactionstatename)) => void |
| scrollTo | 滚动至指定位置 | (offsetConfig: [ScrollOffsetConfig](#offsetconfig)) => void | |
| scrollToNode | 滚动至指定单元格节点 | (node: [Node](/api/basic-class/node), options?: [CellScrollToOptions](#cellscrolltooptions)) => void | |
| scrollToCell | 滚动至指定单元格 | (cell: [S2CellType](#s2celltype), options?: [CellScrollToOptions](#cellscrolltooptions)) => void | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ s2.interaction.getCurrentStateName() // "hover"
const targetNodes = s2.facet.getRowNodes()

s2.interaction.highlightNodes(targetNodes)
s2.interaction.highlightNodes(targetNodes, 'hover')
s2.interaction.highlightNodes(targetNodes, 'selected')
```

## 选中单元格
Expand Down
Loading