Skip to content

Commit

Permalink
Fixed issue where VictoryChart would throw an unhandled exception whe… (
Browse files Browse the repository at this point in the history
#2536)

Co-authored-by: grose <gwyneth.rose@formidable.com>
  • Loading branch information
gwynethrose and grose authored Jan 8, 2024
1 parent d9ece4a commit 8ce3ef6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/green-parents-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"victory-chart": patch
"victory-core": patch
---

Fixed issue where VictoryChart would throw an unhandled exception when passed non-element children (fixes #2391)
15 changes: 7 additions & 8 deletions packages/victory-chart/src/helper-methods.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function getChildren(props, childComponents, calculatedProps) {
const { origin, horizontal } = calculatedProps;
const parentName = props.name || "chart";

return childComponents.map((child, index) => {
return childComponents.filter(React.isValidElement).map((child, index) => {
const role = child.type && child.type.role;
const style = Array.isArray(child.props.style)
? child.props.style
Expand Down Expand Up @@ -161,11 +161,10 @@ export function getChildren(props, childComponents, calculatedProps) {
}

export const getChildComponents = (props, defaultAxes?) => {
const childComponents = React.Children.toArray(props.children);
let newChildComponents = [...childComponents];
let childComponents = React.Children.toArray(props.children);

if (childComponents.length === 0) {
newChildComponents.push(defaultAxes.independent, defaultAxes.dependent);
childComponents.push(defaultAxes.independent, defaultAxes.dependent);
} else {
const axisComponents = {
dependent: Axis.getAxisComponentsWithParent(childComponents, "dependent"),
Expand All @@ -179,18 +178,18 @@ export const getChildComponents = (props, defaultAxes?) => {
axisComponents.dependent.length === 0 &&
axisComponents.independent.length === 0
) {
newChildComponents = props.prependDefaultAxes
childComponents = props.prependDefaultAxes
? [defaultAxes.independent, defaultAxes.dependent].concat(
newChildComponents,
childComponents,
)
: newChildComponents.concat([
: childComponents.concat([
defaultAxes.independent,
defaultAxes.dependent,
]);
}
}

return newChildComponents;
return childComponents;
};

const getDomain = (props, axis, childComponents) => {
Expand Down
8 changes: 5 additions & 3 deletions packages/victory-core/src/victory-util/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-use-before-define */
import React from "react";
import React, { isValidElement } from "react";
import { defaults, isFunction, property, pick, assign, keys } from "lodash";
import { CallbackArgs } from "../types/callbacks";
import { ValueOrAccessor } from "../types/prop-types";
Expand Down Expand Up @@ -301,8 +301,10 @@ export function reduceChildren<
return memo;
}, initialMemo);
};
const childNames = children.map((c, i) => i);
return traverseChildren(children, childNames);

const validChildren = children.filter(isValidElement);
const childNames = validChildren.map((c, i) => i);
return traverseChildren(validChildren, childNames);
}

/**
Expand Down

1 comment on commit 8ce3ef6

@vercel
Copy link

@vercel vercel bot commented on 8ce3ef6 Jan 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.