From 738b983f02d31df84210f7b35998fea3f8d166c1 Mon Sep 17 00:00:00 2001 From: Nathan Kluth Date: Wed, 16 Oct 2024 11:26:01 -0600 Subject: [PATCH] fix: passing categories array of strings (#2919) --- .changeset/spotty-moons-enjoy.md | 5 ++ packages/victory-core/src/exports.test.ts | 2 +- .../src/victory-util/wrapper.test.tsx | 55 +++++++++++++++++++ .../victory-core/src/victory-util/wrapper.tsx | 33 ++++------- 4 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 .changeset/spotty-moons-enjoy.md diff --git a/.changeset/spotty-moons-enjoy.md b/.changeset/spotty-moons-enjoy.md new file mode 100644 index 000000000..67ffee6d8 --- /dev/null +++ b/.changeset/spotty-moons-enjoy.md @@ -0,0 +1,5 @@ +--- +"victory-core": patch +--- + +fix categories array of strings diff --git a/packages/victory-core/src/exports.test.ts b/packages/victory-core/src/exports.test.ts index 4937f167c..a50f5c7a0 100644 --- a/packages/victory-core/src/exports.test.ts +++ b/packages/victory-core/src/exports.test.ts @@ -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], diff --git a/packages/victory-core/src/victory-util/wrapper.test.tsx b/packages/victory-core/src/victory-util/wrapper.test.tsx index 1f6b2035e..c9c8d57ff 100644 --- a/packages/victory-core/src/victory-util/wrapper.test.tsx +++ b/packages/victory-core/src/victory-util/wrapper.test.tsx @@ -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( + [], + "x", + ), + ).toEqual(["one", "two", "three"]); + expect( + Wrapper.getStringsFromChildrenCategories( + [], + "y", + ), + ).toEqual(["one", "two", "three"]); + expect( + Wrapper.getStringsFromChildrenCategories( + [ + , + ], + "x", + ), + ).toEqual(["one", "two", "three"]); + expect( + Wrapper.getStringsFromChildrenCategories( + [ + , + , + ], + "x", + ), + ).toEqual(["one", "two", "three", "four", "five", "six"]); + expect( + Wrapper.getStringsFromChildrenCategories( + [ + , + ], + "x", + ), + ).toEqual([]); + }); + }); }); diff --git a/packages/victory-core/src/victory-util/wrapper.tsx b/packages/victory-core/src/victory-util/wrapper.tsx index 790af7abe..79520fae4 100644 --- a/packages/victory-core/src/victory-util/wrapper.tsx +++ b/packages/victory-core/src/victory-util/wrapper.tsx @@ -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); } @@ -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()); } @@ -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