diff --git a/.changeset/eight-bananas-itch.md b/.changeset/eight-bananas-itch.md new file mode 100644 index 000000000..4a272adc8 --- /dev/null +++ b/.changeset/eight-bananas-itch.md @@ -0,0 +1,5 @@ +--- +"victory-create-container": patch +--- + +Migrate victory-create-container to TypeScript diff --git a/packages/victory-create-container/src/create-container.js b/packages/victory-create-container/src/create-container.ts similarity index 83% rename from packages/victory-create-container/src/create-container.js rename to packages/victory-create-container/src/create-container.ts index 40072162f..95c998449 100644 --- a/packages/victory-create-container/src/create-container.js +++ b/packages/victory-create-container/src/create-container.ts @@ -1,3 +1,4 @@ +import React from "react"; import { toPairs, groupBy, @@ -6,7 +7,6 @@ import { flow, isEmpty, isFunction, - keys, } from "lodash"; import { VictoryContainer, Log } from "victory-core"; import { voronoiContainerMixin } from "victory-voronoi-container"; @@ -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(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) => { @@ -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"); @@ -66,12 +75,14 @@ const combineDefaultEvents = (defaultEvents) => { return events.filter(Boolean); }; -const combineContainerMixins = (mixins, Container) => { +export const combineContainerMixins = ( + mixins: MixinFunction[], + Container: React.ComponentType, +) => { // 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); @@ -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( @@ -124,10 +138,17 @@ const checkBehaviorName = (behavior, behaviors) => { } }; -const makeCreateContainerFunction = - (mixinMap, Container) => - (behaviorA, behaviorB, ...invalid) => { - const behaviors = keys(mixinMap); +export const makeCreateContainerFunction = + ( + mixinMap: Record, + Container: React.ComponentType, + ) => + ( + behaviorA: ContainerType, + behaviorB: ContainerType, + ...invalid: ContainerType[] + ) => { + const behaviors = Object.keys(mixinMap) as ContainerType[]; checkBehaviorName(behaviorA, behaviors); checkBehaviorName(behaviorB, behaviors); @@ -148,7 +169,7 @@ const makeCreateContainerFunction = return combineContainerMixins([...firstMixins, ...secondMixins], Container); }; -const createContainer = makeCreateContainerFunction( +export const createContainer = makeCreateContainerFunction( { zoom: [zoomContainerMixin], voronoi: [voronoiContainerMixin], @@ -158,5 +179,3 @@ const createContainer = makeCreateContainerFunction( }, VictoryContainer, ); - -export { createContainer, makeCreateContainerFunction, combineContainerMixins }; diff --git a/packages/victory-create-container/src/index.d.ts b/packages/victory-create-container/src/index.d.ts deleted file mode 100644 index e39a797c6..000000000 --- a/packages/victory-create-container/src/index.d.ts +++ /dev/null @@ -1,19 +0,0 @@ -import * as React from "react"; - -export type ContainerType = - | "brush" - | "cursor" - | "selection" - | "voronoi" - | "zoom"; -export function createContainer( - c1: ContainerType, - c2: ContainerType, -): React.ComponentType; - -export function combineContainerMixins(mixins: any, Container: any): any; - -export const makeCreateContainerFunction: ( - mixinMap: any, - Container: any, -) => (behaviorA: any, behaviorB: any) => any; diff --git a/packages/victory-create-container/src/index.js b/packages/victory-create-container/src/index.js deleted file mode 100644 index 3a1c18681..000000000 --- a/packages/victory-create-container/src/index.js +++ /dev/null @@ -1,5 +0,0 @@ -export { - combineContainerMixins, - makeCreateContainerFunction, - createContainer, -} from "./create-container"; diff --git a/packages/victory-create-container/src/index.ts b/packages/victory-create-container/src/index.ts new file mode 100644 index 000000000..5f9b97723 --- /dev/null +++ b/packages/victory-create-container/src/index.ts @@ -0,0 +1 @@ +export * from "./create-container";