Skip to content

Commit

Permalink
feat: enable exactOptionalPropertyTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Aug 3, 2024
1 parent 257136b commit 9f9f739
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/filter/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface SingleFilterInput {
*
* If not provided, it means user didn't select a filter.
*/
name?: string;
name?: string | undefined;
/**
* Arguments for the filter function
*/
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/filter/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ describe("isValidRule", () => {
id: "1" as FilterId,
type: "Filter",
name: "Starts with",
path: undefined,
args: [],
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type StandardFnSchema<T extends ZodAnyFunction = ZodAnyFunction> = {
define: T;
implement: TypeOf<T>;
// context?: Record<string, unknown>;
skipValidate?: boolean;
skipValidate?: boolean | undefined;
};

export type GenericFnSchema<
Expand Down
6 changes: 3 additions & 3 deletions packages/filter/src/hooks/use-filter-schema-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
} from "../utils.js";

export interface FilterSchemaContext<Data = unknown>
extends Required<BasicFilterSphereInput<Data>> {
filterRule: FilterGroup;
onRuleChange?: (rule: FilterGroup) => void;
extends Readonly<Required<BasicFilterSphereInput<Data>>> {
readonly filterRule: FilterGroup;
readonly onRuleChange?: (rule: FilterGroup) => void;
}

interface InternalFilterContextType extends FilterSchemaContext {
Expand Down
5 changes: 4 additions & 1 deletion packages/filter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ export {
} from "./filter-sphere-provider.js";
export { useFilterGroup } from "./hooks/use-filter-group.js";
export { useFilterRule } from "./hooks/use-filter-rule.js";
export { FilterSchemaProvider } from "./hooks/use-filter-schema-context.js";
export {
FilterSchemaProvider,
type FilterSchemaContext,
} from "./hooks/use-filter-schema-context.js";
export { useFilterSelect } from "./hooks/use-filter-select.js";
export {
useFilterSphere,
Expand Down
4 changes: 2 additions & 2 deletions packages/filter/src/views/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export type SelectProps<T> = Omit<
SelectHTMLAttributes<HTMLSelectElement>,
"value" | "onChange" | "children"
> & {
value?: T;
options?: { value: T; label: string }[];
value?: T | undefined;
options?: { value: T; label: string }[] | undefined;
onChange?: (value: T) => void;
};

Expand Down
16 changes: 8 additions & 8 deletions packages/playground/src/filter/create-advanced-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type OpenFilterProps<Data = unknown> = {

export type CreateAdvancedFilterProps<Data = unknown> =
BasicFilterSphereInput<Data> & {
defaultRule?: FilterGroup | undefined;
defaultRule?: FilterGroup;
/**
*
* Set `null` to disable storage.
Expand All @@ -42,7 +42,7 @@ export type CreateAdvancedFilterProps<Data = unknown> =
*/
storageKey?: string | null;
dialogProps?: OpenFilterProps<Data>["dialogProps"];
container?: OpenFilterProps<Data>["container"];
container?: NonNullable<OpenFilterProps<Data>["container"]> | null;
};

export const openFlattenFilterDialog = async <Data>(
Expand Down Expand Up @@ -161,11 +161,11 @@ export const createAdvancedFilter = <Data>(
return {
getRule,
getPredicate,
openFilterDialog: async ({
abortSignal,
}: {
abortSignal?: AbortSignal;
} = {}) => {
openFilterDialog: async (
props: {
abortSignal?: AbortSignal;
} = {},
) => {
const r = await getRule();
const result = await openFlattenFilterDialog({
filterBuilder: {
Expand All @@ -177,7 +177,7 @@ export const createAdvancedFilter = <Data>(
defaultRule: r,
},
...options,
abortSignal,
...props,
});
rule = result.rule;
if (options.storageKey) {
Expand Down
25 changes: 13 additions & 12 deletions packages/playground/src/filter/flatten-filter-builder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Fragment } from "react";

interface FlattenFilterBuilderProps<Data = unknown>
extends BasicFilterSphereInput<Data> {
filterRule?: FilterGroup;
filterRule: FilterGroup;
onRuleChange?: (rule: FilterGroup) => void;
}

Expand Down Expand Up @@ -46,7 +46,8 @@ const isFlattenFilterGroup = (
);
};

const createFlattenFilterGroup = () =>
// eslint-disable-next-line react-refresh/only-export-components
export const createFlattenFilterGroup = () =>
createFilterGroup({
op: "or",
conditions: [
Expand All @@ -58,11 +59,11 @@ const createFlattenFilterGroup = () =>
});

export const FlattenFilterBuilder = <Data,>({
filterRule: filterGroup = createFlattenFilterGroup(),
filterRule,
...props
}: FlattenFilterBuilderProps<Data>) => {
const { context } = useFilterSphere({
ruleValue: filterGroup,
ruleValue: filterRule,
...props,
});
const {
Expand All @@ -71,11 +72,11 @@ export const FlattenFilterBuilder = <Data,>({
SingleFilter: FilterRule,
} = useView("templates");
const { Button: ButtonView } = useView("components");
const isValidFlattenRule = isFlattenFilterGroup(filterGroup);
const isValidFlattenRule = isFlattenFilterGroup(filterRule);

if (!isValidFlattenRule) {
return (
<FilterGroupContainer isRoot filterGroup={filterGroup}>
<FilterGroupContainer isRoot filterGroup={filterRule}>
<div>Invalid Rule</div>
<ButtonView
onClick={() => {
Expand All @@ -88,10 +89,10 @@ export const FlattenFilterBuilder = <Data,>({
);
}

const count = countNumberOfRules(filterGroup);
const count = countNumberOfRules(filterRule);
if (count <= 0) {
return (
<FilterGroupContainer isRoot filterGroup={filterGroup}>
<FilterGroupContainer isRoot filterGroup={filterRule}>
<ButtonView
onClick={() => {
props.onRuleChange?.(createFlattenFilterGroup());
Expand All @@ -105,14 +106,14 @@ export const FlattenFilterBuilder = <Data,>({

return (
<FilterSchemaProvider value={context}>
<FilterGroupContainer isRoot filterGroup={filterGroup}>
{filterGroup.conditions.map((andGroup, groupIdx) => {
<FilterGroupContainer isRoot filterGroup={filterRule}>
{filterRule.conditions.map((andGroup, groupIdx) => {
return (
<Fragment key={andGroup.id}>
{groupIdx > 0 && (
<RuleJoiner
parent={filterGroup}
joinBetween={[filterGroup.conditions[groupIdx - 1], andGroup]}
parent={filterRule}
joinBetween={[filterRule.conditions[groupIdx - 1], andGroup]}
/>
)}
<FilterGroupContainer isRoot={false} filterGroup={andGroup}>
Expand Down
27 changes: 17 additions & 10 deletions packages/playground/src/filter/flatten-filter-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
} from "@fn-sphere/filter";
import { useState } from "react";
import { Dialog, type DialogProps } from "tdesign-react";
import { FlattenFilterBuilder } from "./flatten-filter-builder";
import {
createFlattenFilterGroup,
FlattenFilterBuilder,
} from "./flatten-filter-builder";

type FilterValue<Data> = {
rule: FilterGroup;
Expand All @@ -20,7 +23,7 @@ type FilterValue<Data> = {
export type FlattenFilterDialogProps<Data> = {
filterBuilder: BasicFilterSphereInput<Data> & {
rule?: FilterGroup;
defaultRule?: FilterGroup;
defaultRule?: FilterGroup | undefined;
};
open: DialogProps["visible"];
dialogProps?: Omit<DialogProps, "visible" | "onConfirm">;
Expand All @@ -43,15 +46,22 @@ export const FlattenFilterDialog = <Data,>({
onConfirm,
onRuleChange,
}: FlattenFilterDialogProps<Data>) => {
const controlled = filterBuilder.rule !== undefined;
const {
defaultRule,
rule: filterBuilderRule,
...filterBuilderProps
} = filterBuilder;
// This state will be used only when the dialog is uncontrolled.
const [filterGroup, setFilterGroup] = useState(filterBuilder.defaultRule);
const [filterGroup, setFilterGroup] = useState(
defaultRule ?? createFlattenFilterGroup(),
);
const filterFnList = filterBuilder.filterFnList ?? presetFilter;
const realRule = controlled ? filterBuilder.rule : filterGroup;
const controlled = filterBuilderRule !== undefined;
const realRule = controlled ? filterBuilderRule : filterGroup;

return (
<Dialog
visible={open}
visible={!!open}
header={"Advanced Filter"}
{...dialogProps}
onConfirm={() => {
Expand All @@ -71,12 +81,9 @@ export const FlattenFilterDialog = <Data,>({
}}
>
<FlattenFilterBuilder
schema={filterBuilder.schema}
filterFnList={filterFnList}
fieldDeepLimit={filterBuilder.fieldDeepLimit}
mapFieldName={filterBuilder.mapFieldName}
mapFilterName={filterBuilder.mapFilterName}
filterRule={realRule}
{...filterBuilderProps}
onRuleChange={(newRule) => {
onRuleChange?.({
rule: newRule,
Expand Down
12 changes: 6 additions & 6 deletions packages/playground/src/filter/use-advanced-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ export const useAdvancedFilter = <Data>(
filterFnList: options.filterFnList,
filterRule: rule,
}),
openFilterDialog: async ({
abortSignal,
}: {
abortSignal?: AbortSignal;
} = {}) => {
openFilterDialog: async (
props: {
abortSignal?: AbortSignal;
} = {},
) => {
if (isOpen) {
console.error("The filter dialog is already open.");
return;
Expand All @@ -82,7 +82,7 @@ export const useAdvancedFilter = <Data>(
defaultRule: rule,
},
...options,
abortSignal,
...props,
});
setRule(data.rule);
if (options.storageKey) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
"exactOptionalPropertyTypes": true /* Interpret optional property types as written, rather than adding 'undefined'. */,
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
Expand Down

0 comments on commit 9f9f739

Please sign in to comment.