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: 解决移动端下,树形结构展开、收起交互失效的问题 #3037

Closed
wants to merge 5 commits into from
Closed
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
35 changes: 26 additions & 9 deletions packages/s2-core/src/cell/corner-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getResizeAreaAttrs,
shouldAddResizeArea,
} from '../utils/interaction/resize';
import { isMobile } from '../utils/is-mobile';
import { HeaderCell } from './header-cell';

export class CornerCell extends HeaderCell<CornerHeaderConfig> {
Expand Down Expand Up @@ -55,6 +56,19 @@ export class CornerCell extends HeaderCell<CornerHeaderConfig> {
this.update();
}

private onTreeIconClick(isCollapsed: boolean) {
if (isMobile()) {
return;
}

this.emitCollapseEvent(isCollapsed);
}

private emitCollapseEvent(isCollapsed: boolean) {
this.spreadsheet.facet.resetScrollY();
this.spreadsheet.emit(S2Event.ROW_CELL_ALL_COLLAPSED__PRIVATE, isCollapsed);
}

/**
* 绘制折叠展开的 icon
*/
Expand All @@ -73,23 +87,26 @@ export class CornerCell extends HeaderCell<CornerHeaderConfig> {
values(collapseFields!).filter(Boolean).length === rootRowNodes.length ||
rootRowNodes.every((node) => node.isCollapsed);
const isCollapsed = isAllCollapsed;
const { offsetXY, offsetWH } = this.getActionIconOffset(size);

this.treeIcon = renderTreeIcon({
group: this,
iconCfg: {
x: area.x,
y: this.getIconPosition().y,
width: size,
height: size,
x: area.x - offsetXY,
y: this.getIconPosition().y - offsetXY,
width: size + offsetWH,
height: size + offsetWH,
fill,
},
isCollapsed,
onClick: () => {
this.spreadsheet.facet.resetScrollY();
this.spreadsheet.emit(
S2Event.ROW_CELL_ALL_COLLAPSED__PRIVATE,
isCollapsed,
);
this.onTreeIconClick(isCollapsed);
},
onTouchEnd: () => {
/**
* 移动端时,绑定展开、收起事件
*/
this.emitCollapseEvent(isCollapsed);
},
});
}
Expand Down
17 changes: 17 additions & 0 deletions packages/s2-core/src/cell/header-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
} from '../utils/cell/header-cell';
import { findFieldCondition } from '../utils/condition/condition';
import { renderIcon } from '../utils/g-renders';
import { isMobile } from '../utils/is-mobile';
import { getSortTypeIcon } from '../utils/sort-action';

export abstract class HeaderCell<
Expand Down Expand Up @@ -519,4 +520,20 @@ export abstract class HeaderCell<
public getActionIcons() {
return this.actionIcons || [];
}

protected getActionIconOffset(iconSize: number) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
protected getActionIconOffset(iconSize: number) {
protected getActionIconOffset(iconSize: number) {
// 移动端的时候,将icon变大,从而增加触发交互的面积
if (isMobile()) {
return {
offsetXY: iconSize / 4;
offsetWH: iconSize / 2;
}
}
return {
offsetXY: 0,
offsetWH: 0,
};
}

let offsetXY = 0;
let offsetWH = 0;

// 移动端的时候,将icon变大,从而增加触发交互的面积
if (isMobile()) {
offsetXY = iconSize / 4;
offsetWH = iconSize / 2;
}

return {
offsetXY,
offsetWH,
};
}
}
25 changes: 16 additions & 9 deletions packages/s2-core/src/cell/row-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,35 @@ export class RowCell extends HeaderCell<RowHeaderConfig> {

const iconX = x + contentIndent;
const iconY = this.getIconPosition().y;
const { offsetXY, offsetWH } = this.getActionIconOffset(size);

this.treeIcon = renderTreeIcon({
group: this,
iconCfg: {
x: iconX,
y: iconY,
width: size!,
height: size!,
x: iconX - offsetXY,
y: iconY - offsetXY,
width: size + offsetWH,
height: size + offsetWH,
fill,
},
isCollapsed,
onClick: () => {
this.onTreeIconClick();
},
onTouchEnd: () => {
/**
* 移动端时,绑定展开、收起事件
*/
this.emitCollapseEvent();
},
});

// 移动端, 点击热区为整个单元格
if (isMobile()) {
this.addEventListener('click', () => {
this.emitCollapseEvent();
});
}
// if (isMobile()) {
Copy link
Member

Choose a reason for hiding this comment

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

这一块直接删掉吧

// this.addEventListener('click', () => {
// this.emitCollapseEvent();
// });
// }
}

protected drawTreeLeafNodeAlignDot() {
Expand Down
7 changes: 6 additions & 1 deletion packages/s2-core/src/utils/g-renders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ export function renderTreeIcon(options: {
isCollapsed?: boolean | null;
iconCfg: Omit<GuiIconCfg, 'name'>;
onClick?: () => void;
onTouchEnd?: () => void;
}) {
const { group, iconCfg, isCollapsed, onClick } = options;
const { group, iconCfg, isCollapsed, onClick, onTouchEnd } = options;
const iconShape = renderIcon(group, {
...iconCfg,
name: isCollapsed ? 'Plus' : 'Minus',
Expand All @@ -142,5 +143,9 @@ export function renderTreeIcon(options: {
iconShape.addEventListener('click', onClick);
}

if (isFunction(onTouchEnd)) {
iconShape.addEventListener('touchend', onTouchEnd);
}

return iconShape;
}
Loading