Skip to content

Commit

Permalink
feat: add getLocaleText API
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Aug 7, 2024
1 parent 3be44bd commit 715e792
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-toes-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fn-sphere/filter": patch
---

Add `getLocaleText` API
8 changes: 7 additions & 1 deletion packages/filter/src/hooks/use-filter-schema-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { createContext, type ReactNode, useContext } from "react";
import { z } from "zod";
import { type FilterMap, fromFilterMap, toFilterMap } from "../filter-map.js";
import type { BasicFilterSphereInput } from "../types.js";
import { defaultMapFieldName, defaultMapFilterName, noop } from "../utils.js";
import {
defaultGetLocaleText,
defaultMapFieldName,
defaultMapFilterName,
noop,
} from "../utils.js";

export interface FilterSchemaContext<Data = unknown>
extends Readonly<Required<BasicFilterSphereInput<Data>>> {
Expand All @@ -32,6 +37,7 @@ const defaultContext: InternalFilterContextType = {
fieldDeepLimit: 1,
mapFieldName: defaultMapFieldName,
mapFilterName: defaultMapFilterName,
getLocaleText: defaultGetLocaleText,

filterMap: {},
filterableFields: [],
Expand Down
13 changes: 9 additions & 4 deletions packages/filter/src/hooks/use-filter-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import { useFilterRule } from "./use-filter-rule.js";
import { useFilterSchemaContext } from "./use-filter-schema-context.js";

export const useFilterSelect = (rule: SingleFilter) => {
const { filterMap, filterableFields, mapFieldName, mapFilterName } =
useFilterSchemaContext();
const {
filterMap,
filterableFields,
mapFieldName,
mapFilterName,
getLocaleText,
} = useFilterSchemaContext();
const { updateRule } = useFilterRule(rule);

const ruleNode = filterMap[rule.id];
Expand All @@ -29,15 +34,15 @@ export const useFilterSelect = (rule: SingleFilter) => {
: undefined;

const fieldOptions = filterableFields.map((field) => ({
label: mapFieldName(field),
label: getLocaleText(mapFieldName(field)),
value: field,
}));

const selectedFilter = selectedField?.filterFnList.find(
(filter) => filter.name === rule.name,
);
const filterOptions = selectedField?.filterFnList.map((filter) => ({
label: mapFilterName(filter, selectedField),
label: getLocaleText(mapFilterName(filter, selectedField)),
value: filter,
}));

Expand Down
8 changes: 7 additions & 1 deletion packages/filter/src/hooks/use-filter-sphere.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
import { useState } from "react";
import { z } from "zod";
import type { BasicFilterSphereInput } from "../types.js";
import { defaultMapFieldName, defaultMapFilterName, noop } from "../utils.js";
import {
defaultGetLocaleText,
defaultMapFieldName,
defaultMapFilterName,
noop,
} from "../utils.js";
import type { FilterSchemaContext } from "./use-filter-schema-context.js";

export interface FilterSphereInput<Data> extends BasicFilterSphereInput<Data> {
Expand Down Expand Up @@ -40,6 +45,7 @@ export const defaultContext: FilterSchemaContext = {
fieldDeepLimit: 1,
mapFieldName: defaultMapFieldName,
mapFilterName: defaultMapFilterName,
getLocaleText: defaultGetLocaleText,
onRuleChange: noop,
};

Expand Down
2 changes: 2 additions & 0 deletions packages/filter/src/hooks/use-root-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const useRootRule = () => {
filterMap,
mapFieldName,
mapFilterName,
getLocaleText,
onFilterMapChange,
} = useFilterSchemaContext();

Expand All @@ -31,6 +32,7 @@ export const useRootRule = () => {

mapFieldName,
mapFilterName,
getLocaleText,
getRootRule,
updateRootRule,
};
Expand Down
4 changes: 2 additions & 2 deletions packages/filter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export interface BasicFilterSphereInput<Data = unknown> {
field: FilterField,
) => string;
/**
* The callback when the filter rule changes.
* Returns the translation for the key.
*/
// onRuleChange?: (rule: FilterGroup) => void;
getLocaleText?: (key: string) => string;
}
2 changes: 2 additions & 0 deletions packages/filter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export const defaultMapFilterName: (
) => string = (filterSchema) => {
return filterSchema.name;
};

export const defaultGetLocaleText = (key: string) => key;
4 changes: 3 additions & 1 deletion packages/filter/src/views/data-input-views.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { forwardRef } from "react";
import { z } from "zod";
import { useRootRule } from "../hooks/use-root-rule.js";
import { useView } from "../theme/hooks.js";
import type { DataInputViewSpec } from "../theme/types.js";

Expand Down Expand Up @@ -108,12 +109,13 @@ export const presetDataInputSpecs: DataInputViewSpec[] = [
},
view: function LiteralSelect({ requiredDataSchema, rule, updateInput }) {
const { Select: SelectView } = useView("components");
const { getLocaleText } = useRootRule();
const unionSchema = requiredDataSchema[0] as z.ZodUnion<
[z.ZodLiteral<unknown>]
>;
const options = unionSchema.options.map(
(item: z.ZodLiteral<unknown>) => ({
label: item.description ?? String(item.value),
label: getLocaleText(item.description ?? String(item.value)),
value: item.value,
}),
);
Expand Down
5 changes: 4 additions & 1 deletion packages/filter/src/views/filter-group-container.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FilterGroup } from "@fn-sphere/core";
import type { ReactNode } from "react";
import { useRootRule } from "../hooks/use-root-rule.js";

export type FilterGroupContainerProps = {
filterGroup: FilterGroup;
Expand All @@ -10,13 +11,15 @@ export const FilterGroupContainer = ({
filterGroup,
children,
}: FilterGroupContainerProps) => {
const { getLocaleText } = useRootRule();
// const {
// ruleState: { isRoot },
// } = useFilterGroup(filterGroup);
// if (isRoot) {
// return children;
// }
const text = filterGroup.op === "or" ? "Or" : "And";
const text =
filterGroup.op === "or" ? getLocaleText("Or") : getLocaleText("And");
return (
<div
className="filter-group-container"
Expand Down
6 changes: 4 additions & 2 deletions packages/filter/src/views/filter-group.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FilterGroup } from "@fn-sphere/core";
import { Fragment } from "react";
import { useFilterGroup } from "../hooks/use-filter-group.js";
import { useRootRule } from "../hooks/use-root-rule.js";
import { useView } from "../theme/hooks.js";

export type FilterGroupProps = {
Expand All @@ -10,6 +11,7 @@ export type FilterGroupProps = {
export const FilterGroupView = ({ rule: filterGroup }: FilterGroupProps) => {
const { appendChildRule, appendChildGroup } = useFilterGroup(filterGroup);
const { Button: ButtonView } = useView("components");
const { getLocaleText } = useRootRule();
const {
FilterGroupContainer,
SingleFilter: FilterRuleView,
Expand All @@ -24,14 +26,14 @@ export const FilterGroupView = ({ rule: filterGroup }: FilterGroupProps) => {
appendChildRule();
}}
>
Add Filter
{getLocaleText("Add Filter")}
</ButtonView>
<ButtonView
onClick={() => {
appendChildGroup();
}}
>
Add Group
{getLocaleText("Add Group")}
</ButtonView>
</FilterGroupContainer>
);
Expand Down
10 changes: 5 additions & 5 deletions packages/filter/src/views/filter-rule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export const SingleFilterView = ({ rule }: SingleFilterRuleProps) => {
removeRule,
appendRule,
} = useFilterRule(rule);
const { getRootRule, updateRootRule } = useRootRule();
const { getLocaleText, getRootRule, updateRootRule } = useRootRule();
const { Button: ButtonView } = useView("components");
const { FieldSelect, FilterSelect, FilterDataInput, SingleFilterContainer } =
useView("templates");

return (
<SingleFilterContainer rule={rule}>
<FieldSelect rule={rule} />
{isInvert ? "Not" : null}
{isInvert ? getLocaleText("Not") : null}
<FilterSelect rule={rule} />
<FilterDataInput rule={rule} />
{isValid ? null : "!"}
Expand All @@ -34,7 +34,7 @@ export const SingleFilterView = ({ rule }: SingleFilterRuleProps) => {
appendRule();
}}
>
And
{getLocaleText("And")}
</ButtonView>
<ButtonView
onClick={() => {
Expand All @@ -48,10 +48,10 @@ export const SingleFilterView = ({ rule }: SingleFilterRuleProps) => {
updateRootRule(rootRule);
}}
>
Or
{getLocaleText("Or")}
</ButtonView>
<ButtonView aria-label="delete" onClick={() => removeRule(true)}>
Delete
{getLocaleText("Delete")}
</ButtonView>
</SingleFilterContainer>
);
Expand Down

0 comments on commit 715e792

Please sign in to comment.