|
| 1 | +"use client" |
| 2 | + |
| 3 | +import * as React from "react" |
| 4 | +import { X, Plus } from "lucide-react" |
| 5 | + |
| 6 | +import { cn } from "@/lib/utils" |
| 7 | +import { Button } from "@/ui/button" |
| 8 | +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/ui/select" |
| 9 | +import { Input } from "@/ui/input" |
| 10 | + |
| 11 | +export interface FilterCondition { |
| 12 | + id: string |
| 13 | + field: string |
| 14 | + operator: string |
| 15 | + value: string | number | boolean |
| 16 | +} |
| 17 | + |
| 18 | +export interface FilterGroup { |
| 19 | + id: string |
| 20 | + logic: "and" | "or" |
| 21 | + conditions: FilterCondition[] |
| 22 | +} |
| 23 | + |
| 24 | +export interface FilterBuilderProps { |
| 25 | + fields?: Array<{ value: string; label: string; type?: string }> |
| 26 | + value?: FilterGroup |
| 27 | + onChange?: (value: FilterGroup) => void |
| 28 | + className?: string |
| 29 | +} |
| 30 | + |
| 31 | +const defaultOperators = [ |
| 32 | + { value: "equals", label: "Equals" }, |
| 33 | + { value: "notEquals", label: "Does not equal" }, |
| 34 | + { value: "contains", label: "Contains" }, |
| 35 | + { value: "notContains", label: "Does not contain" }, |
| 36 | + { value: "isEmpty", label: "Is empty" }, |
| 37 | + { value: "isNotEmpty", label: "Is not empty" }, |
| 38 | + { value: "greaterThan", label: "Greater than" }, |
| 39 | + { value: "lessThan", label: "Less than" }, |
| 40 | + { value: "greaterOrEqual", label: "Greater than or equal" }, |
| 41 | + { value: "lessOrEqual", label: "Less than or equal" }, |
| 42 | +] |
| 43 | + |
| 44 | +const textOperators = ["equals", "notEquals", "contains", "notContains", "isEmpty", "isNotEmpty"] |
| 45 | +const numberOperators = ["equals", "notEquals", "greaterThan", "lessThan", "greaterOrEqual", "lessOrEqual", "isEmpty", "isNotEmpty"] |
| 46 | +const booleanOperators = ["equals", "notEquals"] |
| 47 | + |
| 48 | +function FilterBuilder({ |
| 49 | + fields = [], |
| 50 | + value, |
| 51 | + onChange, |
| 52 | + className, |
| 53 | +}: FilterBuilderProps) { |
| 54 | + const [filterGroup, setFilterGroup] = React.useState<FilterGroup>( |
| 55 | + value || { |
| 56 | + id: "root", |
| 57 | + logic: "and", |
| 58 | + conditions: [], |
| 59 | + } |
| 60 | + ) |
| 61 | + |
| 62 | + React.useEffect(() => { |
| 63 | + if (value && value !== filterGroup) { |
| 64 | + setFilterGroup(value) |
| 65 | + } |
| 66 | + }, [value]) |
| 67 | + |
| 68 | + const handleChange = (newGroup: FilterGroup) => { |
| 69 | + setFilterGroup(newGroup) |
| 70 | + onChange?.(newGroup) |
| 71 | + } |
| 72 | + |
| 73 | + const addCondition = () => { |
| 74 | + const newCondition: FilterCondition = { |
| 75 | + id: `condition-${Date.now()}`, |
| 76 | + field: fields[0]?.value || "", |
| 77 | + operator: "equals", |
| 78 | + value: "", |
| 79 | + } |
| 80 | + handleChange({ |
| 81 | + ...filterGroup, |
| 82 | + conditions: [...filterGroup.conditions, newCondition], |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + const removeCondition = (conditionId: string) => { |
| 87 | + handleChange({ |
| 88 | + ...filterGroup, |
| 89 | + conditions: filterGroup.conditions.filter((c) => c.id !== conditionId), |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + const updateCondition = (conditionId: string, updates: Partial<FilterCondition>) => { |
| 94 | + handleChange({ |
| 95 | + ...filterGroup, |
| 96 | + conditions: filterGroup.conditions.map((c) => |
| 97 | + c.id === conditionId ? { ...c, ...updates } : c |
| 98 | + ), |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + const toggleLogic = () => { |
| 103 | + handleChange({ |
| 104 | + ...filterGroup, |
| 105 | + logic: filterGroup.logic === "and" ? "or" : "and", |
| 106 | + }) |
| 107 | + } |
| 108 | + |
| 109 | + const getOperatorsForField = (fieldValue: string) => { |
| 110 | + const field = fields.find((f) => f.value === fieldValue) |
| 111 | + const fieldType = field?.type || "text" |
| 112 | + |
| 113 | + switch (fieldType) { |
| 114 | + case "number": |
| 115 | + return defaultOperators.filter((op) => numberOperators.includes(op.value)) |
| 116 | + case "boolean": |
| 117 | + return defaultOperators.filter((op) => booleanOperators.includes(op.value)) |
| 118 | + case "text": |
| 119 | + default: |
| 120 | + return defaultOperators.filter((op) => textOperators.includes(op.value)) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + const needsValueInput = (operator: string) => { |
| 125 | + return !["isEmpty", "isNotEmpty"].includes(operator) |
| 126 | + } |
| 127 | + |
| 128 | + return ( |
| 129 | + <div className={cn("space-y-3", className)}> |
| 130 | + <div className="flex items-center gap-2"> |
| 131 | + <span className="text-sm font-medium">Where</span> |
| 132 | + {filterGroup.conditions.length > 1 && ( |
| 133 | + <Button |
| 134 | + variant="outline" |
| 135 | + size="sm" |
| 136 | + onClick={toggleLogic} |
| 137 | + className="h-7 text-xs" |
| 138 | + > |
| 139 | + {filterGroup.logic.toUpperCase()} |
| 140 | + </Button> |
| 141 | + )} |
| 142 | + </div> |
| 143 | + |
| 144 | + <div className="space-y-2"> |
| 145 | + {filterGroup.conditions.map((condition) => ( |
| 146 | + <div key={condition.id} className="flex items-start gap-2"> |
| 147 | + <div className="flex-1 grid grid-cols-12 gap-2"> |
| 148 | + <div className="col-span-4"> |
| 149 | + <Select |
| 150 | + value={condition.field} |
| 151 | + onValueChange={(value) => |
| 152 | + updateCondition(condition.id, { field: value }) |
| 153 | + } |
| 154 | + > |
| 155 | + <SelectTrigger className="h-9 text-sm"> |
| 156 | + <SelectValue placeholder="Select field" /> |
| 157 | + </SelectTrigger> |
| 158 | + <SelectContent> |
| 159 | + {fields.map((field) => ( |
| 160 | + <SelectItem key={field.value} value={field.value}> |
| 161 | + {field.label} |
| 162 | + </SelectItem> |
| 163 | + ))} |
| 164 | + </SelectContent> |
| 165 | + </Select> |
| 166 | + </div> |
| 167 | + |
| 168 | + <div className="col-span-4"> |
| 169 | + <Select |
| 170 | + value={condition.operator} |
| 171 | + onValueChange={(value) => |
| 172 | + updateCondition(condition.id, { operator: value }) |
| 173 | + } |
| 174 | + > |
| 175 | + <SelectTrigger className="h-9 text-sm"> |
| 176 | + <SelectValue placeholder="Operator" /> |
| 177 | + </SelectTrigger> |
| 178 | + <SelectContent> |
| 179 | + {getOperatorsForField(condition.field).map((op) => ( |
| 180 | + <SelectItem key={op.value} value={op.value}> |
| 181 | + {op.label} |
| 182 | + </SelectItem> |
| 183 | + ))} |
| 184 | + </SelectContent> |
| 185 | + </Select> |
| 186 | + </div> |
| 187 | + |
| 188 | + {needsValueInput(condition.operator) && ( |
| 189 | + <div className="col-span-4"> |
| 190 | + <Input |
| 191 | + className="h-9 text-sm" |
| 192 | + placeholder="Value" |
| 193 | + value={condition.value as string} |
| 194 | + onChange={(e) => |
| 195 | + updateCondition(condition.id, { value: e.target.value }) |
| 196 | + } |
| 197 | + /> |
| 198 | + </div> |
| 199 | + )} |
| 200 | + </div> |
| 201 | + |
| 202 | + <Button |
| 203 | + variant="ghost" |
| 204 | + size="icon-sm" |
| 205 | + className="h-9 w-9 shrink-0" |
| 206 | + onClick={() => removeCondition(condition.id)} |
| 207 | + > |
| 208 | + <X className="h-4 w-4" /> |
| 209 | + <span className="sr-only">Remove condition</span> |
| 210 | + </Button> |
| 211 | + </div> |
| 212 | + ))} |
| 213 | + </div> |
| 214 | + |
| 215 | + <Button |
| 216 | + variant="outline" |
| 217 | + size="sm" |
| 218 | + onClick={addCondition} |
| 219 | + className="h-8" |
| 220 | + disabled={fields.length === 0} |
| 221 | + > |
| 222 | + <Plus className="h-3 w-3" /> |
| 223 | + Add filter |
| 224 | + </Button> |
| 225 | + </div> |
| 226 | + ) |
| 227 | +} |
| 228 | + |
| 229 | +FilterBuilder.displayName = "FilterBuilder" |
| 230 | + |
| 231 | +export { FilterBuilder } |
0 commit comments