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

Migrate victory-create-container to TypeScript #2731

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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";