Skip to content

merge config prop passed to BentoProvider with the one passed at creation #999

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

Merged
merged 3 commits into from
Apr 8, 2025
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
17 changes: 12 additions & 5 deletions packages/bento-design-system/src/BentoProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Children, PartialBentoConfig } from ".";
import { BentoConfig, Children, PartialBentoConfig } from ".";
import { bentoSprinkles } from "./internal";
import { ToastProvider } from "./Toast/ToastProvider";
import { OverlayProvider } from "@react-aria/overlays";
import { DefaultMessages, DefaultMessagesContext } from "./DefaultMessagesContext";
import { LinkComponentContext, LinkComponentProps } from "./util/link";
import { ComponentType, useContext } from "react";
import { ComponentType, useContext, useMemo } from "react";
import { I18nProvider } from "@react-aria/i18n";
import { BentoConfigProvider } from "./BentoConfigContext";
import { BentoConfigProvider, deepmerge } from "./BentoConfigContext";
import { SprinklesFn } from "./util/ConfigurableTypes";
import { SprinklesContext } from "./SprinklesContext";
import { BentoTheme, BentoThemeProvider } from "./BentoThemeContext";
Expand Down Expand Up @@ -88,11 +88,18 @@ export function createBentoProvider(
...props
}: Props) {
const linkComponentFromContext = useContext(LinkComponentContext);
const defaultMessagesValue = useMemo(() => {
return {
defaultMessages,
};
}, [defaultMessages]);
return (
<I18nProvider locale={locale}>
<OverlayProvider style={{ height: "100%" }}>
<DefaultMessagesContext.Provider value={{ defaultMessages }}>
<BentoConfigProvider value={props.config ?? config}>
<DefaultMessagesContext.Provider value={defaultMessagesValue}>
<BentoConfigProvider
value={props.config ? (deepmerge(config, props.config) as BentoConfig) : config}
>
<OptionalThemeWrapper theme={props.theme || theme}>
<SprinklesContext.Provider value={props.sprinkles ?? sprinkles}>
<LinkComponentContext.Provider value={linkComponent ?? linkComponentFromContext}>
Expand Down
40 changes: 40 additions & 0 deletions packages/bento-design-system/test/createBentoProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { renderHook } from "@testing-library/react";
import { BentoConfigProvider, Children, createBentoProvider, useBentoConfig } from "../src";
import { defaultMessages } from "../src/defaultMessages/en";

describe("createBentoProvider", () => {
test("the config passed as prop is merged with the one passed at creation", () => {
const BentoProvider = createBentoProvider({ button: { uppercaseLabel: true } });

const wrapper = ({ children }: { children: React.ReactNode }) => (
<BentoProvider defaultMessages={defaultMessages} config={{ chip: { uppercase: true } }}>
{children as Children}
</BentoProvider>
);

const { result } = renderHook(() => useBentoConfig(), { wrapper });

expect(result.current.button.uppercaseLabel).toBe(true);
expect(result.current.chip.uppercase).toBe(true);
});

test("nested BentoConfigProvider partially overrides the BentoProvider config", () => {
const BentoProvider = createBentoProvider();

const wrapper = ({ children }: { children: React.ReactNode }) => (
<BentoProvider
defaultMessages={defaultMessages}
config={{ button: { uppercaseLabel: true } }}
>
<BentoConfigProvider value={{ chip: { uppercase: true } }}>
{children as Children}
</BentoConfigProvider>
</BentoProvider>
);

const { result } = renderHook(() => useBentoConfig(), { wrapper });

expect(result.current.button.uppercaseLabel).toBe(true);
expect(result.current.chip.uppercase).toBe(true);
});
});
Loading