Skip to content

Commit

Permalink
feat: support filter value with the likes fn
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Aug 17, 2024
1 parent e2d6d08 commit 20b6800
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/components/filter-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
useSetHasValidFilterRule,
useUpdateFilterPredicate,
} from "../state";
import { createFlattenFilterGroup, schema, themeSpec } from "./filter-sphere";
import {
createFlattenFilterGroup,
filterFnList,
schema,
themeSpec,
} from "./filter-sphere";
import { Button } from "./ui/button";
import {
Dialog,
Expand All @@ -29,6 +34,7 @@ export function FilterButton() {
const updateFilterPredicate = useUpdateFilterPredicate();
const { predicate, validRuleCount, reset, context } = useFilterSphere({
schema,
filterFnList,
defaultRule: createFlattenFilterGroup(),
});
const isFilterEnabled = useIsFilterEnabled();
Expand Down
27 changes: 26 additions & 1 deletion src/components/filter-sphere.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {
createFilterGroup,
createFilterTheme,
createSingleFilter,
defineTypedFn,
presetFilter,
SingleFilter,
ThemeSpec,
useFilterRule,
Expand All @@ -11,6 +13,7 @@ import {
import { CircleAlert, X } from "lucide-react";
import { ChangeEvent, useCallback } from "react";
import { z } from "zod";
import { isYText, isYXmlText } from "../y-shape";
import { Button } from "./ui/button";
import { Input } from "./ui/input";
import {
Expand Down Expand Up @@ -40,11 +43,33 @@ export const schema = z.object({
.describe("Type"),
key: z.string().describe("Key"),
path: z.string().describe("Path"),
value: z.unknown(),
value: z.unknown().describe("Value"),
});

export type YShapeItem = z.infer<typeof schema>;

const likeFn = defineTypedFn({
name: "Likes",
define: z.function().args(z.unknown(), z.string()).returns(z.boolean()),
implement: (value, string) => {
if (typeof value === "string") {
return value.includes(string);
}
if (typeof value === "number") {
return value.toString().includes(string);
}
if (isYText(value)) {
return value.toString().includes(string);
}
if (isYXmlText(value)) {
return value.toString().includes(string);
}
return false;
},
});

export const filterFnList = [likeFn, ...presetFilter];

const componentsSpec = {
Button: (props) => {
return <Button variant="outline" {...props} />;
Expand Down

0 comments on commit 20b6800

Please sign in to comment.