Skip to content

Commit

Permalink
feat(QueryBuilder): empty and is not empty operators as value renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
MEsteves22 authored and plagoa committed Dec 20, 2023
1 parent b3285f0 commit f90ca70
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
7 changes: 1 addition & 6 deletions packages/core/src/QueryBuilder/Rule/Operator/Operator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,7 @@ export const Operator = ({
id,
operator: selected.id.toString(),
value:
value === "range" ||
selected.id === "range" ||
selected.id === "IsNotEmpty" ||
selected.id === "Empty"
? null
: undefined,
value === "range" || selected.id === "range" ? null : undefined,
});
} else {
dispatchAction({
Expand Down
17 changes: 6 additions & 11 deletions packages/core/src/QueryBuilder/Rule/Rule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ export const Rule = (props: RuleProps) => {
return -1;
}, [attribute, attributes, combinator, operators]);

const shouldShowValueInput =
operator !== "Empty" && operator !== "IsNotEmpty";

return (
<HvGrid
container
Expand All @@ -103,14 +100,12 @@ export const Rule = (props: RuleProps) => {
)}
{attribute != null && (operator != null || availableOperators === 0) && (
<HvGrid item xs={12} md>
{shouldShowValueInput && (
<Value
attribute={attribute}
id={id}
operator={operator}
value={value}
/>
)}
<Value
attribute={attribute}
id={id}
operator={operator}
value={value}
/>
</HvGrid>
)}
<HvGrid item className={classes.actionsContainer}>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect } from "react";

import { useQueryBuilderContext } from "../../../Context";

export interface EmptyValueProps {
id: React.Key;
}

export const EmptyValue = ({ id }: EmptyValueProps) => {
const { dispatchAction } = useQueryBuilderContext();

// Clear value on first render
useEffect(() => {
dispatchAction({
type: "set-value",
id,
value: null,
});
}, [dispatchAction, id]);

return null;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./EmptyValue";
6 changes: 6 additions & 0 deletions packages/core/src/QueryBuilder/Rule/Value/Value.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NumericValue } from "./NumericValue";
import { TextValue } from "./TextValue";
import { DateTimeValue } from "./DateTimeValue";
import { HvQueryBuilderRendererProps } from "../../types";
import { EmptyValue } from "./EmptyValue";

export interface ValueProps {
id: React.Key;
Expand Down Expand Up @@ -46,6 +47,11 @@ export const Value = ({
}
}

// Built-in behavior for "Empty" and "IsNotEmpty" operators
if (operator === "Empty" || operator === "IsNotEmpty") {
return <EmptyValue id={id} />;
}

// Built-in attributes
switch (attrType) {
case "boolean": {
Expand Down

0 comments on commit f90ca70

Please sign in to comment.