Skip to content

Commit

Permalink
refactor: export presetFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Jul 29, 2024
1 parent b14c6d8 commit 92cbffe
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-singers-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fn-sphere/filter": patch
---

Export presetFilter
4 changes: 2 additions & 2 deletions packages/filter/src/filter-builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FilterProvider } from "./hooks/filter-provider.js";
import { useView } from "./specs/hooks.js";
import { FilterUiProvider, presetUiSpec } from "./specs/index.js";
import type { UiSpec } from "./specs/types.js";
import type { BasicFilterProps } from "./types.js";
import type { BasicFilterBuilderProps } from "./types.js";

export const FilterBuilder = <Data,>({
schema,
Expand All @@ -22,7 +22,7 @@ export const FilterBuilder = <Data,>({
views?: Partial<UiSpec["views"]>;
primitives?: Partial<UiSpec["primitives"]>;
};
} & BasicFilterProps<Data>) => {
} & BasicFilterBuilderProps<Data>) => {
const FilterGroup = useView("FilterGroup");

const normalizedSchema = {
Expand Down
4 changes: 2 additions & 2 deletions packages/filter/src/flatten-filter-builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { countNumberOfRules, type FilterGroup } from "@fn-sphere/core";
import { Fragment } from "react";
import { FilterProvider } from "./hooks/filter-provider.js";
import { useView } from "./specs/index.js";
import type { BasicFilterProps } from "./types.js";
import type { BasicFilterBuilderProps } from "./types.js";
import {
createEmptyFilterGroup,
defaultMapFieldName,
defaultMapFilterName,
isFlattenFilterGroup,
} from "./utils.js";

type FilterBuilderProps<Data = unknown> = BasicFilterProps<Data> & {
type FilterBuilderProps<Data = unknown> = BasicFilterBuilderProps<Data> & {
rule?: FilterGroup;
onChange?: (rule: FilterGroup) => void;
};
Expand Down
21 changes: 13 additions & 8 deletions packages/filter/src/hooks/filter-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import {
type FilterField,
type FilterGroup,
findFilterableFields,
presetFilter,
} from "@fn-sphere/core";
import { type ReactNode, createContext } from "react";
import { createContext, type ReactNode } from "react";
import { z } from "zod";
import { type FilterMap, fromFilterMap, toFilterMap } from "../filter-map.js";
import type { BasicFilterProps } from "../types.js";
import type { BasicFilterBuilderProps } from "../types.js";
import {
createEmptyFilterGroup,
defaultMapFieldName,
Expand All @@ -16,9 +17,11 @@ import {
type FilterContextType = {
filterRule: FilterGroup;
onRuleChange?: (filterGroup: FilterGroup) => void;
} & BasicFilterProps<unknown>;
} & BasicFilterBuilderProps<unknown>;

type NormalizedFilterContextType = Required<BasicFilterProps<unknown>> & {
type NormalizedFilterContextType = Required<
BasicFilterBuilderProps<unknown>
> & {
onRuleChange: (filterMap: FilterMap) => void;

// derived properties
Expand All @@ -28,7 +31,7 @@ type NormalizedFilterContextType = Required<BasicFilterProps<unknown>> & {

const defaultContext: NormalizedFilterContextType = {
schema: z.unknown(),
filterList: [],
filterList: presetFilter,
deepLimit: 1,
mapFieldName: defaultMapFieldName,
mapFilterName: defaultMapFilterName,
Expand All @@ -48,24 +51,26 @@ export const FilterProvider = ({
value: FilterContextType;
children: ReactNode;
}) => {
const filterList = value.filterList ?? presetFilter;

const filterableFields = findFilterableFields({
schema: value.schema,
filterList: value.filterList,
filterList,
maxDeep: value.deepLimit,
});

const contextValue = {
deepLimit: value.deepLimit ?? defaultContext.deepLimit,
mapFieldName: value.mapFieldName ?? defaultContext.mapFieldName,
mapFilterName: value.mapFilterName ?? defaultContext.mapFilterName,
filterList,
schema: value.schema,
filterList: value.filterList,
onRuleChange: (filterMap: FilterMap) => {
value.onRuleChange?.(fromFilterMap(filterMap));
},
filterMap: toFilterMap(value.filterRule),
filterableFields,
};
} satisfies NormalizedFilterContextType;

return (
<FilterBuilderContext.Provider value={contextValue}>
Expand Down
11 changes: 10 additions & 1 deletion packages/filter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ export { FlattenFilterBuilder } from "./flatten-filter-builder.js";
export { useFilterGroup } from "./hooks/use-filter-group.js";
export { useFilterRule } from "./hooks/use-filter-rule.js";
export * from "./specs/index.js";
export type { BasicFilterProps } from "./types.js";
export type { BasicFilterBuilderProps } from "./types.js";
export {
createEmptyFilterGroup,
createEmptyFilterRule,
defaultMapFieldName,
defaultMapFilterName,
} from "./utils.js";

export {
booleanFilter,
commonFilters,
dateFilter,
genericFilter,
numberFilter,
presetFilter,
stringFilter,
} from "@fn-sphere/core";
export type { FilterGroup, FilterRule, SingleFilter } from "@fn-sphere/core";
4 changes: 2 additions & 2 deletions packages/filter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export type FlattenFilterGroup = {
}[];
};

export type BasicFilterProps<Data = unknown> = {
export type BasicFilterBuilderProps<Data = unknown> = {
schema: ZodType<Data>;
filterList: FnSchema[];
filterList?: FnSchema[];
/**
* The maximum nesting depth limit of the filter rule.
*
Expand Down
6 changes: 3 additions & 3 deletions packages/filter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type FilterGroup,
type SingleFilter,
} from "@fn-sphere/core";
import type { BasicFilterProps, FlattenFilterGroup } from "./types.js";
import type { BasicFilterBuilderProps, FlattenFilterGroup } from "./types.js";

export const createEmptyFilterRule = () =>
({
Expand Down Expand Up @@ -36,7 +36,7 @@ export const isFlattenFilterGroup = (
};

export const defaultMapFieldName: NonNullable<
BasicFilterProps["mapFieldName"]
BasicFilterBuilderProps["mapFieldName"]
> = (field) => {
if (field.fieldSchema.description) {
return field.fieldSchema.description;
Expand All @@ -48,7 +48,7 @@ export const defaultMapFieldName: NonNullable<
};

export const defaultMapFilterName: NonNullable<
BasicFilterProps["mapFilterName"]
BasicFilterBuilderProps["mapFilterName"]
> = (filterSchema) => {
return filterSchema.name;
};
6 changes: 3 additions & 3 deletions packages/playground/src/filter/create-advanced-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
createEmptyFilterGroup,
defaultMapFieldName,
defaultMapFilterName,
type BasicFilterProps,
type BasicFilterBuilderProps,
} from "@fn-sphere/filter";
import { createElement } from "react";
import { createRoot } from "react-dom/client";
Expand All @@ -19,7 +19,7 @@ import {
} from "./flatten-filter-dialog.js";

type OpenFilterProps<Data = unknown> = {
filterBuilder: BasicFilterProps<Data> & {
filterBuilder: BasicFilterBuilderProps<Data> & {
// uncontrolled mode only for the dialog
defaultRule: FilterGroup | undefined;
};
Expand All @@ -32,7 +32,7 @@ type OpenFilterProps<Data = unknown> = {
type PartialBy<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;

export type CreateAdvancedFilterProps<Data = unknown> = PartialBy<
BasicFilterProps<Data>,
BasicFilterBuilderProps<Data>,
"filterList"
> & {
defaultRule?: FilterGroup | undefined;
Expand Down

0 comments on commit 92cbffe

Please sign in to comment.