Skip to content

Commit

Permalink
Migrate victory-create-container to TypeScript (#2731)
Browse files Browse the repository at this point in the history
  • Loading branch information
KenanYusuf authored Jan 23, 2024
1 parent 0d848e6 commit 2f9e081
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-bananas-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"victory-create-container": patch
---

Migrate victory-create-container to TypeScript
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from "react";
import {
toPairs,
groupBy,
Expand All @@ -6,7 +7,6 @@ import {
flow,
isEmpty,
isFunction,
keys,
} from "lodash";
import { VictoryContainer, Log } from "victory-core";
import { voronoiContainerMixin } from "victory-voronoi-container";
Expand All @@ -15,16 +15,25 @@ import { selectionContainerMixin } from "victory-selection-container";
import { brushContainerMixin } from "victory-brush-container";
import { cursorContainerMixin } from "victory-cursor-container";

const ensureArray = (thing) => {
export type ContainerType =
| "brush"
| "cursor"
| "selection"
| "voronoi"
| "zoom";

type MixinFunction = (...args: any[]) => any;

function ensureArray<T>(thing: T): [] | T | T[] {
if (!thing) {
return [];
} else if (!Array.isArray(thing)) {
return [thing];
}
return thing;
};
}

const combineEventHandlers = (eventHandlersArray) => {
const combineEventHandlers = (eventHandlersArray: any[]) => {
// takes an array of event handler objects and produces one eventHandlers object
// creates a custom combinedHandler() for events with multiple conflicting handlers
return eventHandlersArray.reduce((localHandlers, finalHandlers) => {
Expand All @@ -47,7 +56,7 @@ const combineEventHandlers = (eventHandlersArray) => {
});
};

const combineDefaultEvents = (defaultEvents) => {
const combineDefaultEvents = (defaultEvents: any[]) => {
// takes a defaultEvents array and returns one equal or lesser length,
// by combining any events that have the same target
const eventsByTarget = groupBy(defaultEvents, "target");
Expand All @@ -66,12 +75,14 @@ const combineDefaultEvents = (defaultEvents) => {
return events.filter(Boolean);
};

const combineContainerMixins = (mixins, Container) => {
export const combineContainerMixins = (
mixins: MixinFunction[],
Container: React.ComponentType<any>,
) => {
// similar to Object.assign(A, B), this function will decide conflicts in favor mixinB.
// this applies to propTypes and defaultProps.
// getChildren will call A's getChildren() and pass the resulting children to B's.
// defaultEvents attempts to resolve any conflicts between A and B's defaultEvents.

const Classes = mixins.map((mixin) => mixin(Container));
const instances = Classes.map((Class) => new Class());
const NaiveCombinedContainer = flow(mixins)(Container);
Expand Down Expand Up @@ -114,7 +125,10 @@ const combineContainerMixins = (mixins, Container) => {
};
};

const checkBehaviorName = (behavior, behaviors) => {
const checkBehaviorName = (
behavior: ContainerType,
behaviors: ContainerType[],
) => {
if (behavior && !includes(behaviors, behavior)) {
Log.warn(
`"${behavior}" is not a valid behavior. Choose from [${behaviors.join(
Expand All @@ -124,10 +138,17 @@ const checkBehaviorName = (behavior, behaviors) => {
}
};

const makeCreateContainerFunction =
(mixinMap, Container) =>
(behaviorA, behaviorB, ...invalid) => {
const behaviors = keys(mixinMap);
export const makeCreateContainerFunction =
(
mixinMap: Record<ContainerType, MixinFunction[]>,
Container: React.ComponentType<any>,
) =>
(
behaviorA: ContainerType,
behaviorB: ContainerType,
...invalid: ContainerType[]
) => {
const behaviors = Object.keys(mixinMap) as ContainerType[];

checkBehaviorName(behaviorA, behaviors);
checkBehaviorName(behaviorB, behaviors);
Expand All @@ -148,7 +169,7 @@ const makeCreateContainerFunction =
return combineContainerMixins([...firstMixins, ...secondMixins], Container);
};

const createContainer = makeCreateContainerFunction(
export const createContainer = makeCreateContainerFunction(
{
zoom: [zoomContainerMixin],
voronoi: [voronoiContainerMixin],
Expand All @@ -158,5 +179,3 @@ const createContainer = makeCreateContainerFunction(
},
VictoryContainer,
);

export { createContainer, makeCreateContainerFunction, combineContainerMixins };
19 changes: 0 additions & 19 deletions packages/victory-create-container/src/index.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/victory-create-container/src/index.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/victory-create-container/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./create-container";

1 comment on commit 2f9e081

@vercel
Copy link

@vercel vercel bot commented on 2f9e081 Jan 23, 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.