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

TS Migrate: victory-group #2396

Merged
merged 9 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion packages/victory-group/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"victory-shared-events": "^36.5.3"
},
"devDependencies": {
"victory-bar": "*"
"victory-bar": "*",
"victory-group": "*"
},
"peerDependencies": {
"react": ">=16.6.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export function getCalculatedProps(props, childComponents) {
const style = Wrapper.getStyle(props.theme, props.style, role);
const { offset, colorScale, color, polar, horizontal } = props;
const categories =
props.categories || Wrapper.getCategories(props, childComponents);
const datasets = props.datasets || Wrapper.getDataFromChildren(props);
props.categories || Wrapper.getCategories(props, childComponents, null);
const datasets = props.datasets || Wrapper.getDataFromChildren(props, null);
const domain = {
x: Wrapper.getDomain(
assign({}, props, { categories }),
Expand Down Expand Up @@ -67,12 +67,14 @@ export function getCalculatedProps(props, childComponents) {
// With shared events, the props change on every event, and every value is re-calculated
const withoutSharedEvents = (props) => {
const { children } = props;
const modifiedChildren = React.Children.toArray(children).map((child) => {
return {
...child,
props: Helpers.omit(child.props, ["sharedEvents"]),
};
});
const modifiedChildren = React.Children.toArray(children).map(
(child: Record<string, any> | any) => {
return {
...child,
props: Helpers.omit(child.props, ["sharedEvents"]),
};
},
);
props.children = modifiedChildren;
return props;
};
Expand Down
37 changes: 0 additions & 37 deletions packages/victory-group/src/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/victory-group/src/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/victory-group/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./victory-group";
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe("components/victory-group", () => {
</VictoryGroup>,
);
const svg = container.querySelector("svg");
expect(svg.style.width).toEqual("100%");
expect(svg.style.height).toEqual("100%");
expect(svg?.style.width).toEqual("100%");
expect(svg?.style.height).toEqual("100%");
});

it("renders an svg with the correct viewBox", () => {
Expand All @@ -30,7 +30,7 @@ describe("components/victory-group", () => {
);
const svg = container.querySelector("svg");
const viewBoxValue = `0 0 ${450} ${300}`;
expect(svg.getAttribute("viewBox")).toEqual(viewBoxValue);
expect(svg?.getAttribute("viewBox")).toEqual(viewBoxValue);
});

it("accepts user props", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import {
VictoryContainer,
VictoryTheme,
Wrapper,
CategoryPropType,
ColorScalePropType,
DomainPropType,
DomainPaddingPropType,
EventPropTypeInterface,
StringOrNumberOrCallback,
VictoryCommonProps,
VictoryDatableProps,
VictoryMultiLabelableProps,
VictoryStyleInterface,
} from "victory-core";
import { VictorySharedEvents } from "victory-shared-events";
import { getChildren, useMemoizedProps } from "./helper-methods";
Expand All @@ -21,9 +31,34 @@ const fallbackProps = {
offset: 0,
};

const VictoryGroup = (initialProps) => {
export type VictoryGroupTTargetType = "data" | "labels" | "parent";
export interface VictoryGroupBaseProps
extends VictoryCommonProps,
VictoryDatableProps,
VictoryMultiLabelableProps {
categories?: CategoryPropType;
children?: React.ReactNode;
color?: string;
colorScale?: ColorScalePropType;
domain?: DomainPropType;
domainPadding?: DomainPaddingPropType;
events?: EventPropTypeInterface<
VictoryGroupTTargetType,
StringOrNumberOrCallback
>[];
eventKey?: StringOrNumberOrCallback;
expectedComponents?: string[];
horizontal?: boolean;
offset?: number;
style?: VictoryStyleInterface;
displayName?: string;
role?: string;
getChildren?: any;
}

const VictoryGroupBase: React.FC<VictoryGroupBaseProps> = (initialProps) => {
// eslint-disable-next-line no-use-before-define
const { role } = VictoryGroupMemo;
const role = VictoryGroup?.role;
const { getAnimationProps, setAnimationState, getProps } =
Hooks.useAnimationState();
const props = getProps(initialProps);
Expand Down Expand Up @@ -51,7 +86,7 @@ const VictoryGroup = (initialProps) => {
const children = getChildren(props, childComponents, calculatedProps);
return children.map((child, index) => {
const childProps = assign(
{ animate: getAnimationProps(props, child, index, "victory-group") },
{ animate: getAnimationProps(props, child, index) },
child.props,
);
return React.cloneElement(child, childProps);
Expand Down Expand Up @@ -144,32 +179,18 @@ const VictoryGroup = (initialProps) => {
return React.cloneElement(container, container.props, newChildren);
};

VictoryGroup.propTypes = {
VictoryGroupBase.propTypes = {
...CommonProps.baseProps,
...CommonProps.dataProps,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
]),
color: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
colorScale: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string),
PropTypes.oneOf([
"grayscale",
"qualitative",
"heatmap",
"warm",
"cool",
"red",
"green",
"blue",
]),
]),
horizontal: PropTypes.bool,
offset: PropTypes.number,
};

VictoryGroup.defaultProps = {
VictoryGroupBase.defaultProps = {
containerComponent: <VictoryContainer />,
groupComponent: <g />,
samples: 50,
Expand All @@ -178,19 +199,22 @@ VictoryGroup.defaultProps = {
theme: VictoryTheme.grayscale,
};

// We need to attatch the static properties to the memoized version, or else
// TODO: This utility could be moved and used for other components
const typedMemo: <T>(component: T, equalityCheck?: any) => T = React.memo;
scottrippey marked this conversation as resolved.
Show resolved Hide resolved

// We need to attach the static properties to the memoized version, or else
// VictoryChart will not be able to get this component's role type
const VictoryGroupMemo = React.memo(VictoryGroup, isEqual);
export const VictoryGroup = typedMemo<VictoryGroupBaseProps>(
VictoryGroupBase,
isEqual,
);

VictoryGroupMemo.displayName = "VictoryGroup";
VictoryGroupMemo.role = "group";
VictoryGroup.displayName = "VictoryGroup";

VictoryGroupMemo.expectedComponents = [
VictoryGroup.role = "group";
VictoryGroup.expectedComponents = [
"groupComponent",
"containerComponent",
"labelComponent",
];

VictoryGroupMemo.getChildren = getChildren;

export default VictoryGroupMemo;
VictoryGroup.getChildren = getChildren;
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.