Skip to content

Commit

Permalink
refactor: update useFilterSphere hook to use 'predicate' instead of '…
Browse files Browse the repository at this point in the history
…getPredicate'
  • Loading branch information
lawvs committed Aug 11, 2024
1 parent 55b7fb1 commit d4c6a7d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
20 changes: 20 additions & 0 deletions .changeset/proud-monkeys-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"@fn-sphere/filter": patch
---

- Update `useFilterSphere` hook to use `predicate` instead of `getPredicate`:

```diff
import { useFilterSphere } from "@fn-sphere/filter";

- const { rule, predicate, context } = useFilterSphere({
+ const { rule, getPredicate, context } = useFilterSphere({
schema: YOUR_DATA_SCHEMA,
});

- const filteredData = YOUR_DATA.filter(getPredicate());
+ const filteredData = YOUR_DATA.filter(predicate);
```

- Update `countTotalRules()` to `get totalRuleCount` in `useFilterSphere` hook
- Add `validRuleCount` to `useFilterSphere` hook to get the count of valid rules
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const YOUR_DATA: z.infer<typeof YOUR_DATA_SCHEMA>[] = [
];

const Filter = () => {
const { rule, getPredicate, context } = useFilterSphere({
const { rule, predicate, context } = useFilterSphere({
schema: YOUR_DATA_SCHEMA,
onPredicateChange: (predicate) => {
const filteredData = YOUR_DATA.filter(predicate);
Expand Down
8 changes: 3 additions & 5 deletions packages/filter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ const YOUR_DATA: z.infer<typeof YOUR_DATA_SCHEMA>[] = [
];

const Filter = () => {
const { rule, getPredicate, context } = useFilterSphere({
const { rule, predicate, context } = useFilterSphere({
schema: YOUR_DATA_SCHEMA,
onPredicateChange: (predicate) => {
const filteredData = YOUR_DATA.filter(predicate);
console.log(filteredData);
},
});
const filteredData = YOUR_DATA.filter(predicate);
console.log(filteredData);

return (
<FilterSphereProvider context={context}>
Expand Down
23 changes: 18 additions & 5 deletions packages/filter/src/hooks/use-filter-sphere.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ export const defaultContext: FilterSchemaContext = {
*
* @example
* ```ts
* const { getPredicate, context } = useFilterSphere<YourData>({ schema: yourDataSchema });
* const predicate = getPredicate();
* const { predicate, context } = useFilterSphere<YourData>({ schema: yourDataSchema });
* const filteredData = data.filter(predicate);
* ```
*/
Expand Down Expand Up @@ -118,14 +117,28 @@ export const useFilterSphere = <Data,>(props: FilterSphereInput<Data>) => {

return {
filterRule: realRule,

/**
* @deprecated Use `countTotalRules` directly.
*/
countTotalRules,
countValidRules: () =>
countValidRules({
get totalRuleCount() {
return countNumberOfRules(realRule);
},
get validRuleCount() {
return countValidRules({
dataSchema: schema,
filterFnList,
rule: realRule,
}),
});
},
/**
* @deprecated Use `predicate` directly.
*/
getPredicate,
get predicate() {
return getPredicate();
},
reset,
context,
};
Expand Down
4 changes: 2 additions & 2 deletions packages/playground/src/filter/flatten-filter-builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const FlattenFilterBuilder = <Data,>({
filterRule,
...props
}: FlattenFilterBuilderProps<Data>) => {
const { context, countTotalRules } = useFilterSphere({
const { context, totalRuleCount } = useFilterSphere({
ruleValue: filterRule,
...props,
});
Expand All @@ -94,7 +94,7 @@ export const FlattenFilterBuilder = <Data,>({
);
}

if (countTotalRules() <= 0) {
if (totalRuleCount <= 0) {
return (
<div>
<ButtonView
Expand Down

0 comments on commit d4c6a7d

Please sign in to comment.