From 918d453099fe1e4d1e23d15285eae1b93b6b4866 Mon Sep 17 00:00:00 2001 From: Kenan Date: Mon, 22 Jan 2024 16:50:47 +0000 Subject: [PATCH 1/3] Migrate victory-create-container to TypeScript --- .changeset/eight-bananas-itch.md | 5 +++ ...reate-container.js => create-container.ts} | 43 ++++++++++++++----- .../src/{index.js => index.ts} | 0 3 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 .changeset/eight-bananas-itch.md rename packages/victory-create-container/src/{create-container.js => create-container.ts} (85%) rename packages/victory-create-container/src/{index.js => index.ts} (100%) 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 85% rename from packages/victory-create-container/src/create-container.js rename to packages/victory-create-container/src/create-container.ts index 40072162f..671df94b1 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) => { +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( @@ -125,9 +139,16 @@ const checkBehaviorName = (behavior, behaviors) => { }; const makeCreateContainerFunction = - (mixinMap, Container) => - (behaviorA, behaviorB, ...invalid) => { - const behaviors = keys(mixinMap); + ( + mixinMap: Record, + Container: React.ComponentType, + ) => + ( + behaviorA: ContainerType, + behaviorB: ContainerType, + ...invalid: ContainerType[] + ) => { + const behaviors = Object.keys(mixinMap) as ContainerType[]; checkBehaviorName(behaviorA, behaviors); checkBehaviorName(behaviorB, behaviors); diff --git a/packages/victory-create-container/src/index.js b/packages/victory-create-container/src/index.ts similarity index 100% rename from packages/victory-create-container/src/index.js rename to packages/victory-create-container/src/index.ts From 59a0cba1f85662bd7648c622788383c5e7a8609c Mon Sep 17 00:00:00 2001 From: Kenan Date: Mon, 22 Jan 2024 16:54:57 +0000 Subject: [PATCH 2/3] delete index.d.ts --- .../victory-create-container/src/index.d.ts | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 packages/victory-create-container/src/index.d.ts 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; From 4ae7c65438a0492213915c750b31af5c9e95fd8c Mon Sep 17 00:00:00 2001 From: Kenan Date: Mon, 22 Jan 2024 16:58:52 +0000 Subject: [PATCH 3/3] fix exports --- packages/victory-create-container/src/create-container.ts | 8 +++----- packages/victory-create-container/src/index.ts | 6 +----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/victory-create-container/src/create-container.ts b/packages/victory-create-container/src/create-container.ts index 671df94b1..95c998449 100644 --- a/packages/victory-create-container/src/create-container.ts +++ b/packages/victory-create-container/src/create-container.ts @@ -75,7 +75,7 @@ const combineDefaultEvents = (defaultEvents: any[]) => { return events.filter(Boolean); }; -const combineContainerMixins = ( +export const combineContainerMixins = ( mixins: MixinFunction[], Container: React.ComponentType, ) => { @@ -138,7 +138,7 @@ const checkBehaviorName = ( } }; -const makeCreateContainerFunction = +export const makeCreateContainerFunction = ( mixinMap: Record, Container: React.ComponentType, @@ -169,7 +169,7 @@ const makeCreateContainerFunction = return combineContainerMixins([...firstMixins, ...secondMixins], Container); }; -const createContainer = makeCreateContainerFunction( +export const createContainer = makeCreateContainerFunction( { zoom: [zoomContainerMixin], voronoi: [voronoiContainerMixin], @@ -179,5 +179,3 @@ const createContainer = makeCreateContainerFunction( }, VictoryContainer, ); - -export { createContainer, makeCreateContainerFunction, combineContainerMixins }; diff --git a/packages/victory-create-container/src/index.ts b/packages/victory-create-container/src/index.ts index 3a1c18681..5f9b97723 100644 --- a/packages/victory-create-container/src/index.ts +++ b/packages/victory-create-container/src/index.ts @@ -1,5 +1 @@ -export { - combineContainerMixins, - makeCreateContainerFunction, - createContainer, -} from "./create-container"; +export * from "./create-container";