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

Improve performance for addEventKeys #1208

Merged
merged 1 commit into from
Dec 20, 2018
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
13 changes: 10 additions & 3 deletions packages/victory-core/src/victory-util/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,17 @@ function getEventKey(key) {

// Returns data with an eventKey prop added to each datum
function addEventKeys(props, data) {
const hasEventKeyAccessor = !!props.eventKey;
const eventKeyAccessor = getEventKey(props.eventKey);
return data.map((datum, index) => {
const eventKey = datum.eventKey || eventKeyAccessor(datum) || index;
return assign({ eventKey }, datum);
return data.map((datum) => {
if (datum.eventKey) {
return datum;
} else if (hasEventKeyAccessor) {
const eventKey = eventKeyAccessor(datum);
return eventKey ? assign({ eventKey }, datum) : datum;
} else {
return datum;
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/victory-scatter/src/helper-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const getBaseProps = (props, fallbackProps) => {
} };

return data.reduce((childProps, datum, index) => {
const eventKey = datum.eventKey;
const eventKey = datum.eventKey || index;
const { x, y } = Helpers.scalePoint(props, datum);
const dataProps = {
x, y, datum, data, index, scale, polar, origin,
Expand Down
2 changes: 1 addition & 1 deletion packages/victory-voronoi/src/helper-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const getBaseProps = (props, fallbackProps) => {

return data.reduce((childProps, datum, index) => {
const polygon = without(polygons[index], "data");
const eventKey = datum.eventKey;
const eventKey = datum.eventKey || index;
const { x, y } = Helpers.scalePoint(props, datum);
const dataProps = {
x, y, datum, data, index, scale, polygon, origin,
Expand Down
1 change: 1 addition & 0 deletions test/client/spec/mock-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class MockVictoryComponent extends React.Component {
index,
datum,
data,
eventKey: index,
style: {}
}
}
Expand Down
34 changes: 34 additions & 0 deletions test/client/spec/victory-core/victory-util/add-events.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,40 @@ describe("victory-util/add-events", () => {
expectEventsTriggered(getDataComponents, dataComponentIsAltered, [true, true], wrapper);
});

it("should set up events on data components scoped with an event key", () => {
const wrapper = mount(
<EventedMockVictoryComponent
data={[{ x: 1, y: 2 }, { x: 3, y: 4 }]}
events={[
{
target: "data",
eventKey: "1",
eventHandlers: {
onClick: () => {
return [{
target: "data",
mutation: () => {
return { style: { fill: "tomato" } };
}
}];
}
}
}
]}
/>
);

const dataComponentIsAltered = (dataComponent) => {
return get(dataComponent.props(), "style.fill") === "tomato";
};

expectEventsTriggered(getDataComponents, dataComponentIsAltered, [false, false], wrapper);
getDataComponents(wrapper).at(0).simulate("click");
expectEventsTriggered(getDataComponents, dataComponentIsAltered, [false, false], wrapper);
getDataComponents(wrapper).at(1).simulate("click");
expectEventsTriggered(getDataComponents, dataComponentIsAltered, [false, true], wrapper);
});

it("should set up events on data components to target labels", () => {
const wrapper = mount(
<EventedMockVictoryComponent
Expand Down
36 changes: 18 additions & 18 deletions test/client/spec/victory-core/victory-util/data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe("victory-util/data", () => {
const props = { data: createData(dataset) };
const formatted = Data.formatData(dataset, props);
expect(formatted).to.be.an.array;
expect(formatted[0]).to.have.keys(["_x", "_y", "x", "y", "eventKey"]);
expect(formatted[0]).to.have.keys(["_x", "_y", "x", "y"]);
});
});
});
Expand All @@ -141,8 +141,8 @@ describe("victory-util/data", () => {
const data = createData([{ x: "kittens", y: 3 }, { x: "cats", y: 5 }]);
const props = { data, x: "x", y: "y" };
const expectedReturnWithEventKeys = [
{ _x: 1, x: "kittens", xName: "kittens", _y: 3, y: 3, eventKey: 0 },
{ _x: 2, x: "cats", xName: "cats", _y: 5, y: 5, eventKey: 1 }
{ _x: 1, x: "kittens", xName: "kittens", _y: 3, y: 3 },
{ _x: 2, x: "cats", xName: "cats", _y: 5, y: 5 }
];
const returnData = Data.getData(props);
expect(returnData).to.eql(expectedReturnWithEventKeys);
Expand Down Expand Up @@ -186,9 +186,9 @@ describe("victory-util/data", () => {
const returnData = Data.getData({ data });

expect(returnData).to.eql([
{ _x: 2, x: 2, _y: 2, y: 2, eventKey: 0 },
{ _x: 1, x: 1, _y: 3, y: 3, eventKey: 1 },
{ _x: 3, x: 3, _y: 1, y: 1, eventKey: 2 }
{ _x: 2, x: 2, _y: 2, y: 2 },
{ _x: 1, x: 1, _y: 3, y: 3 },
{ _x: 3, x: 3, _y: 1, y: 1 }
]);
});

Expand All @@ -202,9 +202,9 @@ describe("victory-util/data", () => {
const returnData = Data.getData({ data, sortKey: "order" });

expect(returnData).to.eql([
{ _x: 3, x: 3, _y: 3, y: 3, order: 1, eventKey: 0 },
{ _x: 1, x: 1, _y: 1, y: 1, order: 2, eventKey: 1 },
{ _x: 2, x: 2, _y: 2, y: 2, order: 3, eventKey: 2 }
{ _x: 3, x: 3, _y: 3, y: 3, order: 1 },
{ _x: 1, x: 1, _y: 1, y: 1, order: 2 },
{ _x: 2, x: 2, _y: 2, y: 2, order: 3 }
]);
});

Expand All @@ -218,9 +218,9 @@ describe("victory-util/data", () => {
const returnData = Data.getData({ data, sortKey: "order", sortOrder: "descending" });

expect(returnData).to.eql([
{ _x: 2, x: 2, _y: 2, y: 2, order: 3, eventKey: 0 },
{ _x: 1, x: 1, _y: 1, y: 1, order: 2, eventKey: 1 },
{ _x: 3, x: 3, _y: 3, y: 3, order: 1, eventKey: 2 }
{ _x: 2, x: 2, _y: 2, y: 2, order: 3 },
{ _x: 1, x: 1, _y: 1, y: 1, order: 2 },
{ _x: 3, x: 3, _y: 3, y: 3, order: 1 }

]);
});
Expand All @@ -236,16 +236,16 @@ describe("victory-util/data", () => {
const returnDataX = Data.getData({ data, sortKey: "x" });

expect(returnDataX).to.eql([
{ _x: 1, x: 20, _y: 3, y: 20, eventKey: 0 },
{ _x: 2, x: 10, _y: 2, y: 10, eventKey: 1 },
{ _x: 3, x: 30, _y: 1, y: 30, eventKey: 2 }
{ _x: 1, x: 20, _y: 3, y: 20 },
{ _x: 2, x: 10, _y: 2, y: 10 },
{ _x: 3, x: 30, _y: 1, y: 30 }
]);

const returnDataY = Data.getData({ data, sortKey: "y" });
expect(returnDataY).to.eql([
{ _x: 3, x: 30, _y: 1, y: 30, eventKey: 0 },
{ _x: 2, x: 10, _y: 2, y: 10, eventKey: 1 },
{ _x: 1, x: 20, _y: 3, y: 20, eventKey: 2 }
{ _x: 3, x: 30, _y: 1, y: 30 },
{ _x: 2, x: 10, _y: 2, y: 10 },
{ _x: 1, x: 20, _y: 3, y: 20 }
]);
});
});
Expand Down