Skip to content

Commit

Permalink
fix: passing categories array of strings (#2919)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlkluth authored Oct 16, 2024
1 parent 7afa44a commit 738b983
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-moons-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"victory-core": patch
---

fix categories array of strings
2 changes: 1 addition & 1 deletion packages/victory-core/src/exports.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ describe("victory-core", () => {
"getDomain": [Function],
"getDomainFromChildren": [Function],
"getScale": [Function],
"getStringsFromCategories": [Function],
"getStringsFromChildren": [Function],
"getStringsFromChildrenCategories": [Function],
"getStringsFromData": [Function],
"getStyle": [Function],
"getWidth": [Function],
Expand Down
55 changes: 55 additions & 0 deletions packages/victory-core/src/victory-util/wrapper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,59 @@ describe("helpers/wrapper", () => {
expect(Wrapper.getStringsFromData([])).toEqual({ x: [], y: [] });
});
});

describe("getStringsFromCategories", () => {
it("gets string from all options", () => {
expect(Wrapper.getStringsFromChildrenCategories([], "x")).toEqual([]);
expect(
Wrapper.getStringsFromChildrenCategories(
[<MockVictoryLine categories={["one", "two", "three"]} key="1" />],
"x",
),
).toEqual(["one", "two", "three"]);
expect(
Wrapper.getStringsFromChildrenCategories(
[<MockVictoryLine categories={["one", "two", "three"]} key="1" />],
"y",
),
).toEqual(["one", "two", "three"]);
expect(
Wrapper.getStringsFromChildrenCategories(
[
<MockVictoryLine
categories={{ x: ["one", "two", "three"] }}
key="1"
/>,
],
"x",
),
).toEqual(["one", "two", "three"]);
expect(
Wrapper.getStringsFromChildrenCategories(
[
<MockVictoryLine
categories={{ x: ["one", "two", "three"] }}
key="1"
/>,
<MockVictoryLine
categories={{ x: ["four", "five", "six"] }}
key="2"
/>,
],
"x",
),
).toEqual(["one", "two", "three", "four", "five", "six"]);
expect(
Wrapper.getStringsFromChildrenCategories(
[
<MockVictoryLine
categories={{ y: ["one", "two", "three"] }}
key="1"
/>,
],
"x",
),
).toEqual([]);
});
});
});
33 changes: 10 additions & 23 deletions packages/victory-core/src/victory-util/wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,13 @@ export function getChildStyle(child, index, calculatedProps, theme) {
};
}

export function getStringsFromCategories(childComponents, axis) {
export function getStringsFromChildrenCategories(childComponents, axis) {
const iteratee = (child) => {
const childProps = child.props || {};
if (!Domain.isDomainComponent(child) || !childProps.categories) {
if (!Domain.isDomainComponent(child)) {
return null;
}
const categories =
childProps.categories && !Array.isArray(childProps.categories)
? childProps.categories[axis]
: childProps.props.categories;
const categoryStrings =
categories && categories.filter((val) => typeof val === "string");
return categoryStrings ? Collection.removeUndefined(categoryStrings) : [];
const childProps = child.props || {};
return Data.getStringsFromCategories(childProps, axis);
};
return Helpers.reduceChildren(childComponents.slice(0), iteratee);
}
Expand Down Expand Up @@ -417,15 +411,14 @@ export function getCategoryAndAxisStringsFromChildren(
axis,
childComponents,
) {
const categories = isPlainObject(props.categories)
? props.categories[axis]
: props.categories;
const categories = Data.getStringsFromCategories(props, axis);
const axisComponent = Axis.getAxisComponent(childComponents, axis);
const axisStrings = axisComponent
? Data.getStringsFromAxes(axisComponent.props, axis)
: [];
const categoryStrings =
categories || getStringsFromCategories(childComponents, axis);
const categoryStrings = categories.length
? categories
: getStringsFromChildrenCategories(childComponents, axis);
return uniq([...categoryStrings, ...axisStrings].flat());
}

Expand All @@ -445,15 +438,9 @@ export function getStringsFromChildren(props, childComponents) {

export function getCategories(props, childComponents, allStrings?) {
const xPropCategories =
props.categories && !Array.isArray(props.categories)
? props.categories.x
: props.categories;

props.categories && Data.getStringsFromCategories(props, "x");
const yPropCategories =
props.categories && !Array.isArray(props.categories)
? props.categories.y
: props.categories;

props.categories && Data.getStringsFromCategories(props, "y");
const fallbackRequired = !xPropCategories || !yPropCategories;

const fallbackProps = fallbackRequired
Expand Down

0 comments on commit 738b983

Please sign in to comment.