Skip to content

Commit

Permalink
Ensure undefined props do not overwrite defaults (#2852)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot committed Apr 4, 2024
1 parent 2b3fb9e commit 8c4cbe7
Show file tree
Hide file tree
Showing 20 changed files with 59 additions and 19 deletions.
17 changes: 17 additions & 0 deletions .changeset/lucky-roses-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"victory-area": patch
"victory-bar": patch
"victory-candlestick": patch
"victory-chart": patch
"victory-core": patch
"victory-errorbar": patch
"victory-group": patch
"victory-line": patch
"victory-native": patch
"victory-pie": patch
"victory-stack": patch
"victory-tooltip": patch
"victory-voronoi": patch
---

Ensure undefined props do not overwrite defaults
3 changes: 2 additions & 1 deletion packages/victory-area/src/area.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2] }]*/
import React from "react";
import { defaults } from "lodash";
import * as d3Shape from "victory-vendor/d3-shape";
import {
Helpers,
Expand Down Expand Up @@ -100,7 +101,7 @@ const defaultProps = {
* The area primitive used by VictoryArea
*/
export const Area: React.FC<AreaProps> = (initialProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const {
ariaLabel,
role,
Expand Down
3 changes: 2 additions & 1 deletion packages/victory-bar/src/bar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { forwardRef } from "react";
import { defaults } from "lodash";
import {
Helpers,
NumberOrCallback,
Expand Down Expand Up @@ -78,7 +79,7 @@ export const Bar = forwardRef<SVGPathElement, BarProps>(function Bar(
initialProps,
ref,
) {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const { polar, origin, style, barWidth, cornerRadius } = props;

const path = polar
Expand Down
2 changes: 1 addition & 1 deletion packages/victory-candlestick/src/candle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const defaultProps: Partial<CandleProps> = {
};

export const Candle = (props: CandleProps) => {
const modifiedProps = evaluateProps({ ...defaultProps, ...props });
const modifiedProps = evaluateProps(defaults({}, props, defaultProps));
const {
ariaLabel,
events,
Expand Down
2 changes: 1 addition & 1 deletion packages/victory-chart/src/victory-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const defaultProps = {

const VictoryChartImpl: React.FC<VictoryChartProps> = (initialProps) => {
const propsWithDefaults = React.useMemo(
() => ({ ...defaultProps, ...initialProps }),
() => defaults({}, initialProps, defaultProps),
[initialProps],
);
const role = "chart";
Expand Down
2 changes: 1 addition & 1 deletion packages/victory-core/src/victory-label/victory-label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ export const VictoryLabel: {
role: string;
defaultStyles: typeof defaultStyles;
} & React.FC<VictoryLabelProps> = (initialProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));

if (props.text === null || props.text === undefined) {
return null;
Expand Down
4 changes: 3 additions & 1 deletion packages/victory-core/src/victory-primitives/arc.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint no-magic-numbers: ["error", { "ignore": [0, 1, 2, 180] }]*/
import React from "react";
import { defaults } from "lodash";

import * as Helpers from "../victory-util/helpers";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";
import { Path } from "./path";
Expand Down Expand Up @@ -65,7 +67,7 @@ const defaultProps = {
};

export const Arc = (initialProps: ArcProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));

return React.cloneElement(props.pathComponent!, {
...props.events,
Expand Down
4 changes: 3 additions & 1 deletion packages/victory-core/src/victory-primitives/background.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from "react";
import { defaults } from "lodash";

import * as Helpers from "../victory-util/helpers";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";
import { Rect } from "./rect";
Expand Down Expand Up @@ -33,7 +35,7 @@ const defaultProps = {
};

export const Background = (initialProps: BackgroundProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));

return props.polar
? React.cloneElement(props.circleComponent!, {
Expand Down
4 changes: 3 additions & 1 deletion packages/victory-core/src/victory-primitives/border.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from "react";
import { defaults } from "lodash";

import * as Helpers from "../victory-util/helpers";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";
import { Rect } from "./rect";
Expand Down Expand Up @@ -39,7 +41,7 @@ const defaultProps = {
};

export const Border = (initialProps: BorderProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));

return React.cloneElement(props.rectComponent!, {
...props.events,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from "react";
import { defaults } from "lodash";

import * as Helpers from "../victory-util/helpers";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";
import { Line } from "./line";
Expand Down Expand Up @@ -40,7 +42,7 @@ const defaultProps = {
};

export const LineSegment = (initialProps: LineSegmentProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));

return React.cloneElement(props.lineComponent!, {
...props.events,
Expand Down
4 changes: 3 additions & 1 deletion packages/victory-core/src/victory-primitives/point.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from "react";
import { defaults } from "lodash";

import * as Helpers from "../victory-util/helpers";
import * as pathHelpers from "../victory-util/point-path-helpers";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";
Expand Down Expand Up @@ -68,7 +70,7 @@ const defaultProps = {
};

export const Point = (initialProps: PointProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const userProps = UserProps.getSafeUserProps(props);

return React.cloneElement(props.pathComponent!, {
Expand Down
4 changes: 3 additions & 1 deletion packages/victory-core/src/victory-primitives/whisker.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from "react";
import { defaults } from "lodash";

import * as Helpers from "../victory-util/helpers";
import { VictoryCommonPrimitiveProps } from "../victory-util/common-props";
import { Line } from "./line";
Expand Down Expand Up @@ -43,7 +45,7 @@ const defaultProps = {
};

export const Whisker = (initialProps: WhiskerProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const {
ariaLabel,
groupComponent,
Expand Down
4 changes: 3 additions & 1 deletion packages/victory-errorbar/src/error-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable max-statements */
import React from "react";
import { defaults } from "lodash";

import {
Helpers,
Line,
Expand Down Expand Up @@ -116,7 +118,7 @@ const defaultProps = {
export const ErrorBar = (
initialProps: ErrorBarProps & typeof ErrorBar.default,
) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const { groupComponent } = props;
const userProps = UserProps.getSafeUserProps(props);
const { tabIndex, ariaLabel } = props;
Expand Down
2 changes: 1 addition & 1 deletion packages/victory-group/src/victory-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const VictoryGroupBase: React.FC<VictoryGroupProps> = (initialProps) => {
const { getAnimationProps, setAnimationState, getProps } =
Hooks.useAnimationState();
const propsWithDefaults = React.useMemo(
() => ({ ...defaultProps, ...initialProps }),
() => defaults({}, initialProps, defaultProps),
[initialProps],
);
const props = getProps(propsWithDefaults);
Expand Down
4 changes: 3 additions & 1 deletion packages/victory-line/src/curve.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2] }]*/
import React from "react";
import { defaults } from "lodash";

import {
Helpers,
Path,
Expand Down Expand Up @@ -39,7 +41,7 @@ const defaultProps = {
};

export const Curve: React.FC<CurveProps> = (initialProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const userProps = UserProps.getSafeUserProps(props);
const { polar, origin } = props;
const lineFunction = LineHelpers.getLineFunction(props);
Expand Down
3 changes: 2 additions & 1 deletion packages/victory-native/src/helpers/wrap-core-component.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import { defaults } from "lodash";

/**
* Wrap a core component, pass props through.
Expand All @@ -11,7 +12,7 @@ export function wrapCoreComponent<TProps extends object>({
defaultProps: TProps;
}) {
const WrappedComponent = (props: TProps) => {
const propsWithDefaults = { ...defaultProps, ...props };
const propsWithDefaults = defaults({}, props, defaultProps);
return <Component {...propsWithDefaults} />;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/victory-pie/src/slice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const defaultProps: SliceProps = {
};

export const Slice = (initialProps: SliceProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const defaultTransform = props.origin
? `translate(${props.origin.x}, ${props.origin.y})`
: undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/victory-stack/src/victory-stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const defaultProps = {
const VictoryStackBase = (initialProps: VictoryStackProps) => {
const { role } = VictoryStack;
const propsWithDefaults = React.useMemo(
() => ({ ...defaultProps, ...initialProps }),
() => defaults({}, initialProps, defaultProps),
[initialProps],
);
const { setAnimationState, getAnimationProps, getProps } =
Expand Down
4 changes: 3 additions & 1 deletion packages/victory-tooltip/src/flyout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from "react";
import { defaults } from "lodash";

import {
Helpers,
Path,
Expand Down Expand Up @@ -142,7 +144,7 @@ const defaultProps = {
};

export const Flyout: React.FC<FlyoutProps> = (initialProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const userProps = UserProps.getSafeUserProps(props);

// check for required props for this subcomponent
Expand Down
4 changes: 3 additions & 1 deletion packages/victory-voronoi/src/voronoi.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from "react";
import { defaults } from "lodash";

import {
Helpers,
ClipPath,
Expand Down Expand Up @@ -55,7 +57,7 @@ const defaultProps = {
};

export const Voronoi = (initialProps: VoronoiProps) => {
const props = evaluateProps({ ...defaultProps, ...initialProps });
const props = evaluateProps(defaults({}, initialProps, defaultProps));
const {
ariaLabel,
role,
Expand Down

0 comments on commit 8c4cbe7

Please sign in to comment.