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

feat(api): support node with callback children #5594

Merged
merged 1 commit into from
Sep 25, 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
27 changes: 27 additions & 0 deletions __tests__/integration/api-chart-options-callback-children.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { chartOptionsCallbackChildren as render } from '../plots/api/chart-options-callback-children';
import { createNodeGCanvas } from './utils/createNodeGCanvas';
import { sleep } from './utils/sleep';
import { kebabCase } from './utils/kebabCase';
import './utils/useSnapshotMatchers';
import './utils/useCustomFetch';

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

it('chart.options should return node with callback children.', async () => {
const { finished, ...rest } = render({
canvas,
container: document.createElement('div'),
});
chart = rest.chart;
await finished;
await sleep(20);
await expect(canvas).toMatchCanvasSnapshot(dir, 'step0');
});

afterAll(() => {
canvas?.destroy();
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions __tests__/plots/api/chart-options-callback-children.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Chart } from '../../../src';

export function chartOptionsCallbackChildren(context) {
const { container, canvas } = context;

const chart = new Chart({
container,
canvas,
});

chart.options({
type: 'repeatMatrix',
width: 480,
height: 480,
paddingLeft: 50,
paddingBottom: 50,
data: {
type: 'fetch',
value: 'data/penguins.csv',
},
encode: {
position: ['culmen_length_mm', 'culmen_depth_mm'],
color: 'species',
},
children: () => ({
type: 'point',
encode: { color: 'species' },
}),
});

const finished = chart.render();

return { chart, finished };
}
1 change: 1 addition & 0 deletions __tests__/plots/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ export { chartRender3dLinePlot } from './chart-render-3d-line-plot';
export { chartRender3dLinePlotPerspective } from './chart-render-3d-line-plot-perspective';
export { chartOnBrushHighlightTooltip } from './chart-on-brush-highlight-tooltip';
export { chartChangeSizeCustomShape } from './chart-change-size-custom-shape';
export { chartOptionsCallbackChildren } from './chart-options-callback-children';
28 changes: 22 additions & 6 deletions src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export const VIEW_KEYS = [

export const REMOVE_FLAG = '__remove__';

export const CALLBACK_NODE = '__callback__';

export function normalizeContainer(
container: string | HTMLElement,
): HTMLElement {
Expand Down Expand Up @@ -84,12 +86,16 @@ export function optionsOf(node: Node): Record<string, any> {
const value = nodeValue.get(node);
const { children = [] } = node;
for (const child of children) {
const childValue = valueOf(child);
const { children = [] } = value;
children.push(childValue);
discovered.push(child);
nodeValue.set(child, childValue);
value.children = children;
if (child.type === CALLBACK_NODE) {
value.children = child.value;
} else {
const childValue = valueOf(child);
const { children = [] } = value;
children.push(childValue);
discovered.push(child);
nodeValue.set(child, childValue);
value.children = children;
}
}
}
return nodeValue.get(root);
Expand Down Expand Up @@ -142,6 +148,12 @@ function createNode(
mark: Record<string, new () => Node>,
composition: Record<string, new () => Node>,
): Node {
if (typeof options === 'function') {
const node = new Node();
node.value = options;
node.type = CALLBACK_NODE;
return node;
}
const { type, children, ...value } = options;
const Ctor = typeCtor(type, mark, composition);
const node = new Ctor();
Expand Down Expand Up @@ -182,6 +194,8 @@ function appendNode(
for (const child of children) {
discovered.push([node, child]);
}
} else if (typeof children === 'function') {
discovered.push([node, children]);
}
}
}
Expand Down Expand Up @@ -216,6 +230,8 @@ export function updateRoot(
const oldChild = oldChildren[i];
discovered.push([oldNode, oldChild, newChild]);
}
} else if (typeof newChildren === 'function') {
discovered.push([oldNode, null, newChildren]);
}
}
}
Expand Down