Skip to content

Commit

Permalink
fix(core): 调用 setOptions 设置 brushSelection (#2257)
Browse files Browse the repository at this point in the history
  • Loading branch information
1wkk authored Jul 13, 2023
1 parent 00cb47d commit f610514
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ describe('Interaction Base Cell Brush Selection Tests', () => {
mockSpreadSheetInstance.foregroundGroup = new Group('');
mockSpreadSheetInstance.showTooltipWithInfo = jest.fn();
mockSpreadSheetInstance.interaction = mockRootInteraction;
mockRootInteraction.getBrushSelection = () => ({
data: true,
row: true,
col: true,
});
mockSpreadSheetInstance.render();
brushSelectionInstance = new DataCellBrushSelection(
mockSpreadSheetInstance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ describe('Interaction Col Cell Brush Selection Tests', () => {
mockRootInteraction.getAllColHeaderCells = () =>
allColHeaderCells as unknown as ColCell[];
mockSpreadSheetInstance.interaction = mockRootInteraction;
mockRootInteraction.getBrushSelection = () => ({
data: true,
row: true,
col: true,
});
mockSpreadSheetInstance.render();
brushSelectionInstance = new ColBrushSelection(mockSpreadSheetInstance);

Expand Down Expand Up @@ -227,4 +232,33 @@ describe('Interaction Col Cell Brush Selection Tests', () => {
expect(selectedFn).toHaveBeenCalledTimes(1);
expect(brushSelectionFn).toHaveBeenCalledTimes(1);
});

test('should not emit brush secletion event', () => {
mockRootInteraction.getBrushSelection = () => ({
data: true,
row: true,
col: false,
});

const brushSelectionFn = jest.fn();

mockSpreadSheetInstance.on(
S2Event.COL_CELL_BRUSH_SELECTION,
brushSelectionFn,
);

// ================== mouse down ==================
emitEvent(S2Event.COL_CELL_MOUSE_DOWN, { x: 200, y: 0 });

// ================== mouse move ==================
emitEvent(S2Event.COL_CELL_MOUSE_MOVE, {
clientX: 600,
clientY: 90,
});

// ================== mouse up ==================
emitEvent(S2Event.GLOBAL_MOUSE_UP, {});
// emit event
expect(brushSelectionFn).toHaveBeenCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ describe('Interaction Data Cell Brush Selection Tests', () => {
currentCol: false,
});
mockSpreadSheetInstance.interaction = mockRootInteraction;
mockRootInteraction.getBrushSelection = () => ({
data: true,
row: true,
col: true,
});
mockSpreadSheetInstance.render();
mockSpreadSheetInstance.facet.layoutResult.colLeafNodes = Array.from(
new Array(10),
Expand Down Expand Up @@ -567,4 +572,33 @@ describe('Interaction Data Cell Brush Selection Tests', () => {
expect(validateXIndex(8)).toBe(null);
expect(validateXIndex(7)).toBe(7);
});

test('should not emit brush secletion event', () => {
mockRootInteraction.getBrushSelection = () => ({
data: false,
row: true,
col: true,
});

const brushSelectionFn = jest.fn();

mockSpreadSheetInstance.on(
S2Event.DATA_CELL_BRUSH_SELECTION,
brushSelectionFn,
);

// ================== mouse down ==================
emitEvent(S2Event.DATA_CELL_MOUSE_DOWN, { x: 10, y: 20 });

// ================== mouse move ==================
emitGlobalEvent(S2Event.GLOBAL_MOUSE_MOVE, {
clientX: 100,
clientY: 200,
});

// ================== mouse up ==================
emitEvent(S2Event.GLOBAL_MOUSE_UP, {});
// emit event
expect(brushSelectionFn).toHaveBeenCalledTimes(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ describe('Interaction Row Cell Brush Selection Tests', () => {
mockSpreadSheetInstance.getCell = jest.fn(() => startBrushRowCell) as any;
mockRootInteraction.getAllRowHeaderCells = () => allRowHeaderCells;
mockSpreadSheetInstance.interaction = mockRootInteraction;
mockRootInteraction.getBrushSelection = () => ({
data: true,
row: true,
col: true,
});
mockSpreadSheetInstance.render();
brushSelectionInstance = new RowBrushSelection(mockSpreadSheetInstance);

Expand Down Expand Up @@ -280,4 +285,31 @@ describe('Interaction Row Cell Brush Selection Tests', () => {
currentRow.length / 2,
);
});

test('should not emit brush secletion event', () => {
mockRootInteraction.getBrushSelection = () => ({
data: true,
row: false,
col: true,
});

const brushSelectionFn = jest.fn();

mockSpreadSheetInstance.on(
S2Event.ROW_CELL_BRUSH_SELECTION,
brushSelectionFn,
);

// ================== mouse down ==================
emitEvent(S2Event.ROW_CELL_MOUSE_DOWN, { x: 10, y: 90 });

mockSpreadSheetInstance.getCell = jest.fn(() => endBrushRowCell) as any;
// ================== mouse move ==================
emitEvent(S2Event.GLOBAL_MOUSE_MOVE, { clientX: 180, clientY: 400 });

// ================== mouse up ==================
emitEvent(S2Event.GLOBAL_MOUSE_UP, {});
// emit event
expect(brushSelectionFn).toHaveBeenCalledTimes(0);
});
});
30 changes: 30 additions & 0 deletions packages/s2-core/__tests__/unit/sheet-type/pivot-sheet-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,36 @@ describe('PivotSheet Tests', () => {
expect(s2.tooltip).toBeInstanceOf(CustomTooltip);
});

test('should refresh brush selection info', () => {
s2.setOptions({
interaction: {
brushSelection: true,
},
});

expect(s2.interaction.getBrushSelection()).toStrictEqual({
data: true,
row: true,
col: true,
});

s2.setOptions({
interaction: {
brushSelection: {
data: true,
row: false,
col: false,
},
},
});

expect(s2.interaction.getBrushSelection()).toStrictEqual({
data: true,
row: false,
col: false,
});
});

test('should render sheet', () => {
const facetRenderSpy = jest
.spyOn(s2, 'buildFacet' as any)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class ColBrushSelection extends BaseBrushSelection {

protected bindMouseDown() {
this.spreadsheet.on(S2Event.COL_CELL_MOUSE_DOWN, (event) => {
if (!this.spreadsheet.interaction.getBrushSelection().col) {
return;
}

super.mouseDown(event);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class DataCellBrushSelection extends BaseBrushSelection {

protected bindMouseDown() {
this.spreadsheet.on(S2Event.DATA_CELL_MOUSE_DOWN, (event) => {
if (!this.spreadsheet.interaction.getBrushSelection().data) {
return;
}

super.mouseDown(event);
this.resetScrollDelta();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { BaseBrushSelection } from './base-brush-selection';
export class RowBrushSelection extends BaseBrushSelection {
protected bindMouseDown() {
this.spreadsheet.on(S2Event.ROW_CELL_MOUSE_DOWN, (event) => {
if (!this.spreadsheet.interaction.getBrushSelection().row) {
return;
}

super.mouseDown(event);
});
}
Expand Down
20 changes: 20 additions & 0 deletions packages/s2-core/src/interaction/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,24 @@ export class RootInteraction {
currentCol,
};
}

public getBrushSelection(): BrushSelection {
const { brushSelection } = this.spreadsheet.options.interaction;

if (isBoolean(brushSelection)) {
return {
data: brushSelection,
row: brushSelection,
col: brushSelection,
};
}

const { data = false, row = false, col = false } = brushSelection ?? {};

return {
data,
row,
col,
};
}
}
3 changes: 3 additions & 0 deletions packages/s2-core/src/sheet-type/spread-sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
get,
includes,
isEmpty,
isEqual,
isFunction,
isString,
memoize,
Expand Down Expand Up @@ -215,6 +216,7 @@ export abstract class SpreadSheet extends EE {
}

private initInteraction() {
this.interaction?.destroy?.();
this.interaction = new RootInteraction(this);
}

Expand Down Expand Up @@ -377,6 +379,7 @@ export abstract class SpreadSheet extends EE {

public setOptions(options: Partial<S2Options>, reset?: boolean) {
this.hideTooltip();

if (reset) {
this.options = getSafetyOptions(options);
} else {
Expand Down
15 changes: 15 additions & 0 deletions packages/s2-react/playground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,21 @@ function MainLayout() {
>
自定义 Tooltip (s2.setOptions)
</Button>

<Button
size="small"
onClick={() => {
s2Ref.current?.setOptions({
interaction: {
brushSelection: false,
},
});
s2Ref.current?.render();
}}
>
禁用刷选 (s2.setOptions)
</Button>

<Popover
placement="bottomRight"
content={
Expand Down

1 comment on commit f610514

@vercel
Copy link

@vercel vercel bot commented on f610514 Jul 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

antvis-s2 – ./

antvis-s2-antv-s2.vercel.app
antvis-s2-git-master-antv-s2.vercel.app
antvis-s2.vercel.app

Please sign in to comment.