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

backwards generic params compatibility #2933

Merged
merged 2 commits into from
Oct 30, 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/fuzzy-candles-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"victory-create-container": patch
---

update createContainer types to be backwards compatibile
30 changes: 20 additions & 10 deletions packages/victory-create-container/src/create-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useVictorySelectionContainer,
} from "victory-selection-container";
import React from "react";
import { VictoryContainer } from "victory-core";
import { VictoryContainer, VictoryContainerProps } from "victory-core";
import { forOwn, groupBy, isEmpty, toPairs } from "lodash";
import {
VictoryVoronoiContainer,
Expand Down Expand Up @@ -123,17 +123,27 @@ export function makeCreateContainerFunction<
TContainerComponents[T]
>;

// Helper type to support backwards compatibility with old types
type CombinedContainerProps<A, B> = A extends ContainerType
? B extends ContainerType
? ContainerProps<A> & ContainerProps<B> // New style: infer from container types
: A & B // Old style: expect props as generic types
: A & B;

return function combineContainers<
TContainerA extends ContainerType,
TContainerB extends ContainerType,
>(containerA: TContainerA, containerB: TContainerB) {
const ContainerA = containerComponents[containerA];
const ContainerB = containerComponents[containerB];
const useContainerA = CONTAINER_HOOKS[containerA];
const useContainerB = CONTAINER_HOOKS[containerB];
TContainerA extends ContainerType | VictoryContainerProps,
TContainerB extends ContainerType | VictoryContainerProps,
>(
containerA: TContainerA extends ContainerType ? TContainerA : ContainerType,
containerB: TContainerB extends ContainerType ? TContainerB : ContainerType,
) {
const ContainerA = containerComponents[containerA as ContainerType];
const ContainerB = containerComponents[containerB as ContainerType];
const useContainerA = CONTAINER_HOOKS[containerA as ContainerType];
const useContainerB = CONTAINER_HOOKS[containerB as ContainerType];

const CombinedContainer = (
props: ContainerProps<TContainerA> & ContainerProps<TContainerB>,
props: CombinedContainerProps<TContainerA, TContainerB>,
) => {
const { children: childrenA, props: propsA } = useContainerA(props);
const { children: combinedChildren, props: combinedProps } =
Expand All @@ -152,7 +162,7 @@ export function makeCreateContainerFunction<
CombinedContainer.displayName = `Victory${containerA}${containerB}Container`;
CombinedContainer.role = "container";
CombinedContainer.defaultEvents = (
props: ContainerProps<TContainerA> & ContainerProps<TContainerB>,
props: CombinedContainerProps<TContainerA, TContainerB>,
) =>
combineDefaultEvents([
...ContainerA.defaultEvents(props),
Expand Down