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

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: () => {
Copy link
Member

Choose a reason for hiding this comment

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

角头这里还是热区绑定单元格即可, 和 row-cell 一致

if (isMobile()) {
  this.addEventListener('touched', () => {
    this.emitCollapseEvent();
  });
}

/**
* 移动端时,绑定展开、收起事件
*/
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) {
let offsetXY = 0;
let offsetWH = 0;

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

return {
offsetXY,
offsetWH,
};
}
}
22 changes: 11 additions & 11 deletions packages/s2-core/src/cell/row-cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,28 +155,28 @@ 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,
Copy link
Member

Choose a reason for hiding this comment

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

不建议增大 icon 的尺寸, 和 pc 表现不一致有一点奇怪

y: iconY,
width: size!,
height: size!,
x: iconX - offsetXY,
y: iconY - offsetXY,
width: size + offsetWH,
height: size + offsetWH,
fill,
},
isCollapsed,
onClick: () => {
this.onTreeIconClick();
},
});

// 移动端, 点击热区为整个单元格
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.

移动端热区还是不变, 把 click 改成 touched 就能解决这个问题

if (isMobile()) {
  this.addEventListener('touched', () => {
    this.emitCollapseEvent();
  });
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这个方案不妥,如果把整个单元格做为热区的话,单元格上的Tooltip就无法触发。所以我才考虑把icon放大

Copy link
Contributor Author

Choose a reason for hiding this comment

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

之前也考虑过这个方案

this.addEventListener('click', () => {
onTouchEnd: () => {
/**
* 移动端时,绑定展开、收起事件
*/
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