Skip to content

Commit

Permalink
feat: support customize style for components
Browse files Browse the repository at this point in the history
  • Loading branch information
lawvs committed Sep 8, 2024
1 parent 74568a7 commit 8337984
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-cups-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fn-sphere/filter": patch
---

Support custom style for components
16 changes: 13 additions & 3 deletions packages/filter/src/views/field-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import {
type UpdateFieldOptions,
} from "../hooks/use-filter-select.js";
import { useView } from "../theme/index.js";
import type { CommonProps } from "./types.js";

export type FieldSelectProps = {
rule: SingleFilter;
} & UpdateFieldOptions;
} & UpdateFieldOptions &
CommonProps;

export const FieldSelect = ({
rule,
...updateFieldOptions
tryRetainArgs,
tryRetainFilter,
...props
}: FieldSelectProps) => {
const { Select: SelectView } = useView("components");
const { selectedField, fieldOptions, setField } = useFilterSelect(rule);
Expand All @@ -20,7 +24,13 @@ export const FieldSelect = ({
<SelectView
value={selectedField}
options={fieldOptions}
onChange={(field) => setField(field, updateFieldOptions)}
onChange={(field) =>
setField(field, {
tryRetainArgs: !!tryRetainArgs,
tryRetainFilter: !!tryRetainFilter,
})
}
{...props}
/>
);
};
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
Expand Up @@ -3,15 +3,17 @@ import { useCallback, type ReactNode } from "react";
import { useFilterGroup } from "../hooks/use-filter-group.js";
import { useRootRule } from "../hooks/use-root-rule.js";
import { useView } from "../theme/hooks.js";
import type { CommonProps } from "./types.js";

export type FilterGroupContainerProps = {
rule: FilterGroup;
children?: ReactNode;
};
} & CommonProps;

export const FilterGroupContainer = ({
rule,
children,
...props
}: FilterGroupContainerProps) => {
const { getLocaleText } = useRootRule();
const {
Expand Down Expand Up @@ -56,6 +58,7 @@ export const FilterGroupContainer = ({
gap: 8,
background: "rgba(0, 0, 0, 0.05)",
}}
{...props}
>
<Button onClick={handleToggleGroupOp}>{text}</Button>
{children}
Expand Down
13 changes: 7 additions & 6 deletions packages/filter/src/views/filter-group.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import type { FilterGroup } from "@fn-sphere/core";
import { Fragment } from "react";
import { useView } from "../theme/hooks.js";
import type { CommonProps } from "./types.js";

export type FilterGroupProps = {
rule: FilterGroup;
};
} & CommonProps;

export const FilterGroupView = ({ rule: filterGroup }: FilterGroupProps) => {
export const FilterGroupView = ({ rule, ...props }: FilterGroupProps) => {
const {
FilterGroupContainer,
SingleFilter: FilterRuleView,
RuleJoiner,
} = useView("templates");

return (
<FilterGroupContainer rule={filterGroup}>
{filterGroup.conditions.map((childRule, groupIdx) => {
<FilterGroupContainer rule={rule} {...props}>
{rule.conditions.map((childRule, groupIdx) => {
return (
<Fragment key={childRule.id}>
{groupIdx > 0 && (
<RuleJoiner
parent={filterGroup}
joinBetween={[filterGroup.conditions[groupIdx - 1]!, childRule]}
parent={rule}
joinBetween={[rule.conditions[groupIdx - 1]!, childRule]}
/>
)}
{childRule.type === "Filter" ? (
Expand Down
14 changes: 11 additions & 3 deletions packages/filter/src/views/filter-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import {
type UpdateFilterOptions,
} from "../hooks/use-filter-select.js";
import { useView } from "../theme/index.js";
import type { CommonProps } from "./types.js";

export type FilterSelectProps = {
rule: SingleFilter;
} & UpdateFilterOptions;
} & UpdateFilterOptions &
CommonProps;

export const FilterSelect = ({
rule,
...updateFilterOptions
tryRetainArgs,
...props
}: FilterSelectProps) => {
const { Select: SelectView } = useView("components");
const { selectedField, selectedFilter, filterOptions, setFilter } =
Expand All @@ -22,7 +25,12 @@ export const FilterSelect = ({
value={selectedFilter}
disabled={!selectedField}
options={filterOptions}
onChange={(val) => setFilter(val, updateFilterOptions)}
onChange={(val) =>
setFilter(val, {
tryRetainArgs: !!tryRetainArgs,
})
}
{...props}
/>
);
};
Expand Down
3 changes: 2 additions & 1 deletion packages/filter/src/views/rule-joiner.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { FilterGroup, FilterRule } from "@fn-sphere/core";
import type { CommonProps } from "./types.js";

export type RuleJoinerProps = {
parent: FilterGroup;
joinBetween: [FilterRule, FilterRule];
};
} & CommonProps;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const RuleJoiner = (_props: RuleJoinerProps) => {
Expand Down
8 changes: 7 additions & 1 deletion packages/filter/src/views/single-filter-container.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import type { SingleFilter } from "@fn-sphere/core";
import type { ReactNode } from "react";
import type { CommonProps } from "./types.js";

export type SingleFilterContainerProps = {
rule: SingleFilter;
children?: ReactNode;
};
} & CommonProps;

export const SingleFilterContainer = ({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
rule: _rule,
children,
...props
}: SingleFilterContainerProps) => {
return (
<div
Expand All @@ -17,8 +21,10 @@ export const SingleFilterContainer = ({
alignItems: "center",
gap: 8,
}}
{...props}
>
{children}
</div>
);
};
SingleFilterContainer.displayName = "SingleFilterContainer";
7 changes: 4 additions & 3 deletions packages/filter/src/views/single-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { type SingleFilter } from "@fn-sphere/core";
import { useFilterRule } from "../hooks/use-filter-rule.js";
import { useRootRule } from "../hooks/use-root-rule.js";
import { useView } from "../theme/index.js";
import type { CommonProps } from "./types.js";

export type SingleFilterRuleProps = {
rule: SingleFilter;
};
} & CommonProps;

export const SingleFilterView = ({ rule }: SingleFilterRuleProps) => {
export const SingleFilterView = ({ rule, ...props }: SingleFilterRuleProps) => {
const {
ruleState: { isInvert },
removeRule,
Expand All @@ -18,7 +19,7 @@ export const SingleFilterView = ({ rule }: SingleFilterRuleProps) => {
useView("templates");

return (
<SingleFilterContainer rule={rule}>
<SingleFilterContainer rule={rule} {...props}>
<FieldSelect rule={rule} />
{isInvert ? getLocaleText("operatorNot") : null}
<FilterSelect rule={rule} />
Expand Down
6 changes: 6 additions & 0 deletions packages/filter/src/views/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { CSSProperties } from "react";

export type CommonProps = {
className?: string;
style?: CSSProperties;
};

0 comments on commit 8337984

Please sign in to comment.