Skip to content

Commit

Permalink
refactor: update FilterSphereProvider theme prop
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Aug 8, 2024
1 parent e0f5632 commit e05bcbe
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 37 deletions.
15 changes: 15 additions & 0 deletions .changeset/silent-jars-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@fn-sphere/filter": minor
---

Removed inline theme merging logic from `FilterSphereProvider`.

Introduced `createFilterTheme` for theme merging.

Migration guide:

```tsx
- <FilterSphereProvider theme={customTheme}>
+ const theme = createFilterTheme(customTheme);
+ <FilterSphereProvider theme={theme}>
```
30 changes: 3 additions & 27 deletions packages/filter/src/filter-sphere-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@ import {
FilterSchemaProvider,
type FilterSchemaContext,
} from "./hooks/use-filter-schema-context.js";
import {
FilterThemeProvider,
presetTheme,
type ThemeSpec,
} from "./theme/index.js";

export type FilterThemeInput = {
dataInputViews?: ThemeSpec["dataInputViews"];
components?: Partial<ThemeSpec["components"]>;
primitives?: Partial<ThemeSpec["primitives"]>;
templates?: Partial<ThemeSpec["templates"]>;
};

import { FilterThemeProvider, type ThemeSpec } from "./theme/index.js";
export interface FilterSphereProviderProps<Data> {
context: FilterSchemaContext<Data>;
theme?: FilterThemeInput;
theme?: ThemeSpec;
}

export const FilterSphereProvider = <Data,>({
Expand All @@ -27,19 +15,7 @@ export const FilterSphereProvider = <Data,>({
children,
}: PropsWithChildren<FilterSphereProviderProps<Data>>) => {
const MaybeThemeProviderWithChildren = theme ? (
<FilterThemeProvider
theme={{
dataInputViews: [
...(theme.dataInputViews ?? []),
...presetTheme.dataInputViews,
],
components: { ...presetTheme.components, ...theme.components },
primitives: { ...presetTheme.primitives, ...theme.primitives },
templates: { ...presetTheme.templates, ...theme.templates },
}}
>
{children}
</FilterThemeProvider>
<FilterThemeProvider theme={theme}>{children}</FilterThemeProvider>
) : (
<Fragment>{children}</Fragment>
);
Expand Down
1 change: 0 additions & 1 deletion packages/filter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export { createFilterGroup, createSingleFilter } from "@fn-sphere/core";
export {
FilterSphereProvider,
type FilterSphereProviderProps,
type FilterThemeInput,
} from "./filter-sphere-provider.js";
export { useFilterGroup } from "./hooks/use-filter-group.js";
export { useFilterRule } from "./hooks/use-filter-rule.js";
Expand Down
5 changes: 5 additions & 0 deletions packages/filter/src/theme/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export const useFilterTheme = () => {
return useContext(FilterThemeContext);
};

/**
* This component takes a theme prop and applies it to the children.
*
* It should preferably be used at the root of your filter component tree.
*/
export const FilterThemeProvider = ({
theme,
children,
Expand Down
26 changes: 26 additions & 0 deletions packages/filter/src/theme/create-filter-theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { presetTheme } from "./preset.js";
import type { ThemeSpec } from "./types.js";

export type FilterThemeInput = {
dataInputViews?: ThemeSpec["dataInputViews"];
components?: Partial<ThemeSpec["components"]>;
primitives?: Partial<ThemeSpec["primitives"]>;
templates?: Partial<ThemeSpec["templates"]>;
};

/**
* Takes an incomplete theme object and generates a complete theme object
*
* The returned `ThemeSpec` should be passed to `FilterThemeProvider` or `FilterSphereProvider`
*/
export const createFilterTheme = (theme: FilterThemeInput): ThemeSpec => {
return {
dataInputViews: [
...(theme.dataInputViews ?? []),
...presetTheme.dataInputViews,
],
components: { ...presetTheme.components, ...theme.components },
primitives: { ...presetTheme.primitives, ...theme.primitives },
templates: { ...presetTheme.templates, ...theme.templates },
};
};
4 changes: 4 additions & 0 deletions packages/filter/src/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export { FilterThemeProvider } from "./context.js";
export {
createFilterTheme,
type FilterThemeInput,
} from "./create-filter-theme.js";
export { useDataInputView, useView } from "./hooks.js";
export { presetTheme } from "./preset.js";
export type * from "./types.js";
18 changes: 9 additions & 9 deletions packages/playground/src/filter/flatten-filter-builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
} from "@fn-sphere/filter";
import {
createFilterGroup,
createFilterTheme,
createSingleFilter,
FilterSphereProvider,
useFilterSphere,
Expand Down Expand Up @@ -59,6 +60,13 @@ export const createFlattenFilterGroup = () =>
],
});

const theme = createFilterTheme({
templates: {
FilterGroupContainer: FlattenFilterGroupContainer,
SingleFilter: FlattenSingleFilterView,
},
});

export const FlattenFilterBuilder = <Data,>({
filterRule,
...props
Expand Down Expand Up @@ -101,15 +109,7 @@ export const FlattenFilterBuilder = <Data,>({
}

return (
<FilterSphereProvider
context={context}
theme={{
templates: {
FilterGroupContainer: FlattenFilterGroupContainer,
SingleFilter: FlattenSingleFilterView,
},
}}
>
<FilterSphereProvider context={context} theme={theme}>
<FilterGroup rule={filterRule} />
</FilterSphereProvider>
);
Expand Down

0 comments on commit e05bcbe

Please sign in to comment.