-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract
FilterSphereProvider
- Loading branch information
Showing
8 changed files
with
108 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
"@fn-sphere/filter": minor | ||
--- | ||
|
||
Add new `FilterSphereProvider` component to provide filter context to children components. | ||
|
||
Add `useFilterSphere` hook to access filter predicate. | ||
|
||
The `FilterBuilder` component no longer adds a provider to its child components. | ||
|
||
```ts | ||
const { filterRule, schema, predicate } = useFilterSphere<YourData>(); | ||
const filteredData = data.filter(predicate); | ||
``` |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Fragment, type PropsWithChildren } from "react"; | ||
import { FilterSchemaProvider } from "./hooks/use-filter-schema-context.js"; | ||
import { | ||
FilterThemeProvider, | ||
presetTheme, | ||
type ThemeSpec, | ||
} from "./theme/index.js"; | ||
import type { BasicFilterSphereInput } from "./types.js"; | ||
|
||
export type FilterThemeInput = { | ||
dataInputViews?: ThemeSpec["dataInputViews"]; | ||
components?: Partial<ThemeSpec["components"]>; | ||
primitives?: Partial<ThemeSpec["primitives"]>; | ||
templates?: Partial<ThemeSpec["templates"]>; | ||
}; | ||
|
||
export interface FilterSphereProviderProps<Data> | ||
extends BasicFilterSphereInput<Data> { | ||
theme?: FilterThemeInput; | ||
} | ||
|
||
export const FilterSphereProvider = <Data,>({ | ||
schema, | ||
filterList, | ||
filterRule, | ||
onRuleChange, | ||
mapFieldName, | ||
mapFilterName, | ||
fieldDeepLimit, | ||
theme, | ||
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> | ||
) : ( | ||
<Fragment>{children}</Fragment> | ||
); | ||
|
||
return ( | ||
<FilterSchemaProvider | ||
value={{ | ||
schema, | ||
filterList, | ||
filterRule, | ||
onRuleChange, | ||
mapFieldName, | ||
mapFilterName, | ||
fieldDeepLimit, | ||
}} | ||
> | ||
{MaybeThemeProviderWithChildren} | ||
</FilterSchemaProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { useFilterSchemaContext } from "../hooks/use-filter-schema-context.js"; | ||
import { useView } from "../theme/hooks.js"; | ||
|
||
export const FilterBuilder = () => { | ||
const { FilterGroup } = useView("templates"); | ||
const { filterRule } = useFilterSchemaContext(); | ||
return <FilterGroup rule={filterRule} />; | ||
}; |