|
| 1 | +import React, { useMemo } from "react"; |
| 2 | +import { View, Text, TextInput, Pressable } from "react-native"; |
| 3 | +import { X } from "lucide-react-native"; |
| 4 | +import type { FieldDefinition } from "~/components/renderers/types"; |
| 5 | +import { |
| 6 | + type SimpleFilter, |
| 7 | + type FilterOperator, |
| 8 | + OPERATOR_META, |
| 9 | + operatorsForFieldType, |
| 10 | +} from "~/lib/query-builder"; |
| 11 | + |
| 12 | +/* ------------------------------------------------------------------ */ |
| 13 | +/* Props */ |
| 14 | +/* ------------------------------------------------------------------ */ |
| 15 | + |
| 16 | +export interface FilterRowProps { |
| 17 | + filter: SimpleFilter; |
| 18 | + fields: FieldDefinition[]; |
| 19 | + onUpdate: (patch: Partial<SimpleFilter>) => void; |
| 20 | + onRemove: () => void; |
| 21 | +} |
| 22 | + |
| 23 | +/* ------------------------------------------------------------------ */ |
| 24 | +/* Component */ |
| 25 | +/* ------------------------------------------------------------------ */ |
| 26 | + |
| 27 | +export function FilterRow({ filter, fields, onUpdate, onRemove }: FilterRowProps) { |
| 28 | + const selectedFieldDef = fields.find((f) => f.name === filter.field); |
| 29 | + const fieldType = selectedFieldDef?.type ?? "text"; |
| 30 | + |
| 31 | + const availableOperators = useMemo( |
| 32 | + () => operatorsForFieldType(fieldType), |
| 33 | + [fieldType], |
| 34 | + ); |
| 35 | + |
| 36 | + const operatorMeta = OPERATOR_META[filter.operator]; |
| 37 | + const needsValue = operatorMeta?.valueCount !== 0; |
| 38 | + const needsSecondValue = operatorMeta?.valueCount === 2; |
| 39 | + |
| 40 | + return ( |
| 41 | + <View className="mb-2 flex-row items-center gap-2"> |
| 42 | + {/* Field picker (simplified as a scrollable row of chips) */} |
| 43 | + <Pressable |
| 44 | + className="flex-1 rounded-lg border border-input bg-background px-3 py-2" |
| 45 | + onPress={() => { |
| 46 | + // Cycle through fields for simplicity |
| 47 | + const currentIdx = fields.findIndex((f) => f.name === filter.field); |
| 48 | + const nextField = fields[(currentIdx + 1) % fields.length]; |
| 49 | + if (nextField) { |
| 50 | + onUpdate({ field: nextField.name }); |
| 51 | + } |
| 52 | + }} |
| 53 | + > |
| 54 | + <Text className="text-xs text-foreground" numberOfLines={1}> |
| 55 | + {selectedFieldDef?.label ?? (filter.field || "Field…")} |
| 56 | + </Text> |
| 57 | + </Pressable> |
| 58 | + |
| 59 | + {/* Operator picker */} |
| 60 | + <Pressable |
| 61 | + className="rounded-lg border border-input bg-background px-2 py-2" |
| 62 | + onPress={() => { |
| 63 | + const currentIdx = availableOperators.indexOf(filter.operator); |
| 64 | + const next = availableOperators[(currentIdx + 1) % availableOperators.length]; |
| 65 | + if (next) onUpdate({ operator: next }); |
| 66 | + }} |
| 67 | + > |
| 68 | + <Text className="text-xs text-muted-foreground" numberOfLines={1}> |
| 69 | + {operatorMeta?.label ?? filter.operator} |
| 70 | + </Text> |
| 71 | + </Pressable> |
| 72 | + |
| 73 | + {/* Value input */} |
| 74 | + {needsValue && ( |
| 75 | + <TextInput |
| 76 | + className="flex-1 rounded-lg border border-input bg-background px-3 py-2 text-xs text-foreground" |
| 77 | + value={String(filter.value ?? "")} |
| 78 | + onChangeText={(text) => onUpdate({ value: text })} |
| 79 | + placeholder="Value" |
| 80 | + placeholderTextColor="#94a3b8" |
| 81 | + /> |
| 82 | + )} |
| 83 | + |
| 84 | + {/* Second value (between) */} |
| 85 | + {needsSecondValue && ( |
| 86 | + <TextInput |
| 87 | + className="flex-1 rounded-lg border border-input bg-background px-3 py-2 text-xs text-foreground" |
| 88 | + value={String(filter.value2 ?? "")} |
| 89 | + onChangeText={(text) => onUpdate({ value2: text } as Partial<SimpleFilter>)} |
| 90 | + placeholder="To" |
| 91 | + placeholderTextColor="#94a3b8" |
| 92 | + /> |
| 93 | + )} |
| 94 | + |
| 95 | + {/* Remove */} |
| 96 | + <Pressable onPress={onRemove} className="p-1"> |
| 97 | + <X size={16} color="#ef4444" /> |
| 98 | + </Pressable> |
| 99 | + </View> |
| 100 | + ); |
| 101 | +} |
0 commit comments