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(brush): emit brush remove #5170

Merged
merged 1 commit into from
Jun 8, 2023
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
11 changes: 8 additions & 3 deletions __tests__/integration/api-chart-emit-brush-highlight-x.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { sleep } from './utils/sleep';
import { kebabCase } from './utils/kebabCase';
import './utils/useSnapshotMatchers';

describe('chart.options.autoFit', () => {
describe('chart.emit', () => {
const dir = `${__dirname}/snapshots/api/${kebabCase(render.name)}`;
const canvas = createNodeGCanvas(800, 500);

it('chart({ autoFit: true }) should fit parent container', async () => {
const { highlighted, finished, button } = render({
it('chart.emit(brushX) should emit brushX events', async () => {
const { highlighted, finished, button, button1, removed } = render({
canvas,
container: document.createElement('div'),
});
Expand All @@ -19,6 +19,11 @@ describe('chart.options.autoFit', () => {
await highlighted;
await sleep(20);
await expect(canvas).toMatchCanvasSnapshot(dir, 'step0');

button1.dispatchEvent(new CustomEvent('click'));
await removed;
await sleep(20);
await expect(canvas).toMatchCanvasSnapshot(dir, 'step1');
});

afterAll(() => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 17 additions & 1 deletion __tests__/plots/api/chart-emit-brush-highlight-x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export function chartEmitBrushHighlightX(context) {
button.innerText = 'Highlight';
container.appendChild(button);

const button1 = document.createElement('button');
button1.innerText = 'Remove';
container.appendChild(button1);

// wrapperDiv
const wrapperDiv = document.createElement('div');
container.appendChild(wrapperDiv);
Expand Down Expand Up @@ -55,6 +59,11 @@ export function chartEmitBrushHighlightX(context) {
let resolve;
const highlighted = new Promise((r) => (resolve = r));

chart.on('brush:remove', ({ nativeEvent } = {}) => {
if (!nativeEvent) return;
console.log('remove');
});

button.onclick = () => {
const X = ['2001-01', '2001-03'];
chart.emit('brush:highlight', {
Expand All @@ -63,5 +72,12 @@ export function chartEmitBrushHighlightX(context) {
resolve();
};

return { chart, button, finished, highlighted };
let resolve1;
const removed = new Promise((r) => (resolve1 = r));
button1.onclick = () => {
chart.emit('brush:remove');
resolve1();
};

return { chart, button, finished, highlighted, button1, removed };
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ focus.on('brush:filter', (e) => {
const [[x1, x2]] = selection;
const domainX = scaleX.getOptions().domain;
if (x1 === domainX[0] && x2 === domainX[1]) {
context.emit('brush:remove');
context.emit('brush:remove', {});
} else {
context.emit('brush:highlight', { data: { selection } });
}
Expand Down
9 changes: 6 additions & 3 deletions src/interaction/brushHighlight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ export function brush(
end = [x1, y1];
updateMask([x, y], [x1, y1], emit);
},
remove() {
if (mask) removeMask(false);
remove(emit = true) {
if (mask) removeMask(emit);
},
destroy() {
// Do not emit brush:end event.
Expand Down Expand Up @@ -657,7 +657,10 @@ export function brushHighlight(
emitter.on('brush:highlight', onHighlight);

// Remove brush and reset data.
const onRemove = () => brushHandler.remove();
const onRemove = ({ nativeEvent }: any = {}) => {
if (nativeEvent) return;
brushHandler.remove(false);
};
emitter.on('brush:remove', onRemove);

// Remove event handlers.
Expand Down