Skip to content

Commit

Permalink
Merge pull request #1 from gb853940223/mobile-tap
Browse files Browse the repository at this point in the history
fix: 解决移动端下,树形结构展开、收起交互失效的问题
  • Loading branch information
gb853940223 authored Dec 12, 2024
2 parents 0a7977c + 2074881 commit a91b554
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
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) {
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()) {
// 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;
}

0 comments on commit a91b554

Please sign in to comment.