|
| 1 | +import { |
| 2 | + VictoryZoomContainerFn, |
| 3 | + useVictoryZoomContainer, |
| 4 | +} from "victory-zoom-container"; |
| 5 | +import { |
| 6 | + VictorySelectionContainerFn, |
| 7 | + useVictorySelectionContainer, |
| 8 | +} from "victory-selection-container"; |
| 9 | +import React from "react"; |
| 10 | +import { VictoryContainerFn } from "victory-core"; |
| 11 | +import { forOwn, groupBy, isEmpty, toPairs } from "lodash"; |
| 12 | + |
| 13 | +export type ContainerType = |
| 14 | + | "brush" |
| 15 | + | "cursor" |
| 16 | + | "selection" |
| 17 | + | "voronoi" |
| 18 | + | "zoom"; |
| 19 | + |
| 20 | +const CONTAINERS: { |
| 21 | + [key in ContainerType]: { |
| 22 | + name: string; |
| 23 | + component: React.ComponentType<any>; |
| 24 | + hook: (props: any) => { |
| 25 | + props: any; |
| 26 | + children: React.ReactNode; |
| 27 | + }; |
| 28 | + }; |
| 29 | +} = { |
| 30 | + zoom: { |
| 31 | + name: "Zoom", |
| 32 | + component: VictoryZoomContainerFn, |
| 33 | + hook: useVictoryZoomContainer, |
| 34 | + }, |
| 35 | + selection: { |
| 36 | + name: "Selection", |
| 37 | + component: VictorySelectionContainerFn, |
| 38 | + hook: useVictorySelectionContainer, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +function ensureArray<T>(thing: T): [] | T | T[] { |
| 43 | + if (!thing) { |
| 44 | + return []; |
| 45 | + } else if (!Array.isArray(thing)) { |
| 46 | + return [thing]; |
| 47 | + } |
| 48 | + return thing; |
| 49 | +} |
| 50 | + |
| 51 | +const combineEventHandlers = (eventHandlersArray: any[]) => { |
| 52 | + // takes an array of event handler objects and produces one eventHandlers object |
| 53 | + // creates a custom combinedHandler() for events with multiple conflicting handlers |
| 54 | + return eventHandlersArray.reduce((localHandlers, finalHandlers) => { |
| 55 | + forOwn(localHandlers, (localHandler, eventName) => { |
| 56 | + const existingHandler = finalHandlers[eventName]; |
| 57 | + if (existingHandler) { |
| 58 | + // create new handler for event that concats the existing handler's mutations with new ones |
| 59 | + finalHandlers[eventName] = function combinedHandler(...params) { |
| 60 | + // named for debug clarity |
| 61 | + // sometimes handlers return undefined; use empty array instead, for concat() |
| 62 | + const existingMutations = ensureArray(existingHandler(...params)); |
| 63 | + const localMutations = ensureArray(localHandler(...params)); |
| 64 | + return existingMutations.concat(localMutations); |
| 65 | + }; |
| 66 | + } else { |
| 67 | + finalHandlers[eventName] = localHandler; |
| 68 | + } |
| 69 | + }); |
| 70 | + return finalHandlers; |
| 71 | + }); |
| 72 | +}; |
| 73 | + |
| 74 | +const combineDefaultEvents = (defaultEvents: any[]) => { |
| 75 | + // takes a defaultEvents array and returns one equal or lesser length, |
| 76 | + // by combining any events that have the same target |
| 77 | + const eventsByTarget = groupBy(defaultEvents, "target"); |
| 78 | + const events = toPairs(eventsByTarget).map(([target, eventsArray]) => { |
| 79 | + const newEventsArray = eventsArray.filter(Boolean); |
| 80 | + return isEmpty(newEventsArray) |
| 81 | + ? null |
| 82 | + : { |
| 83 | + target, |
| 84 | + eventHandlers: combineEventHandlers( |
| 85 | + eventsArray.map((event) => event.eventHandlers), |
| 86 | + ), |
| 87 | + // note: does not currently handle eventKey or childName |
| 88 | + }; |
| 89 | + }); |
| 90 | + return events.filter(Boolean); |
| 91 | +}; |
| 92 | + |
| 93 | +// TODO: Type this function properly |
| 94 | +export function createContainerFn( |
| 95 | + containerA: ContainerType, |
| 96 | + containerB: ContainerType, |
| 97 | +) { |
| 98 | + const { |
| 99 | + name: containerAName, |
| 100 | + component: ContainerA, |
| 101 | + hook: useContainerA, |
| 102 | + } = CONTAINERS[containerA]; |
| 103 | + const { |
| 104 | + name: containerBName, |
| 105 | + component: ContainerB, |
| 106 | + hook: useContainerB, |
| 107 | + } = CONTAINERS[containerB]; |
| 108 | + |
| 109 | + function NewContainer(props: any) { |
| 110 | + const { children: childrenA, props: propsA } = useContainerA(props); |
| 111 | + const { children: childrenB, props: propsB } = useContainerB({ |
| 112 | + ...propsA, |
| 113 | + children: childrenA, |
| 114 | + }); |
| 115 | + |
| 116 | + return <VictoryContainerFn {...propsB}>{childrenB}</VictoryContainerFn>; |
| 117 | + } |
| 118 | + |
| 119 | + NewContainer.displayName = `Victory${containerAName}${containerBName}Container`; |
| 120 | + NewContainer.role = "container"; |
| 121 | + NewContainer.defaultEvents = (props: any) => |
| 122 | + combineDefaultEvents([ |
| 123 | + ...ContainerA.defaultEvents(props), |
| 124 | + ...ContainerB.defaultEvents(props), |
| 125 | + ]); |
| 126 | + |
| 127 | + return NewContainer; |
| 128 | +} |
0 commit comments