|
| 1 | +# Filter Builder Component |
| 2 | + |
| 3 | +An Airtable-like filter builder component for building complex query conditions in Object UI. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Filter Builder component provides a user-friendly interface for creating and managing filter conditions. It supports: |
| 8 | + |
| 9 | +- ✅ Dynamic add/remove filter conditions |
| 10 | +- ✅ Field selection from configurable list |
| 11 | +- ✅ Type-aware operators (text, number, boolean) |
| 12 | +- ✅ AND/OR logic toggling |
| 13 | +- ✅ Schema-driven configuration |
| 14 | +- ✅ Tailwind CSS styled with Shadcn UI components |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Basic Example |
| 19 | + |
| 20 | +```typescript |
| 21 | +{ |
| 22 | + type: 'filter-builder', |
| 23 | + name: 'userFilters', |
| 24 | + label: 'User Filters', |
| 25 | + fields: [ |
| 26 | + { value: 'name', label: 'Name', type: 'text' }, |
| 27 | + { value: 'email', label: 'Email', type: 'text' }, |
| 28 | + { value: 'age', label: 'Age', type: 'number' }, |
| 29 | + { value: 'status', label: 'Status', type: 'text' } |
| 30 | + ], |
| 31 | + value: { |
| 32 | + id: 'root', |
| 33 | + logic: 'and', |
| 34 | + conditions: [ |
| 35 | + { |
| 36 | + id: 'cond-1', |
| 37 | + field: 'status', |
| 38 | + operator: 'equals', |
| 39 | + value: 'active' |
| 40 | + } |
| 41 | + ] |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +### With Initial Empty State |
| 47 | + |
| 48 | +```typescript |
| 49 | +{ |
| 50 | + type: 'filter-builder', |
| 51 | + name: 'productFilters', |
| 52 | + label: 'Product Filters', |
| 53 | + fields: [ |
| 54 | + { value: 'name', label: 'Product Name', type: 'text' }, |
| 55 | + { value: 'price', label: 'Price', type: 'number' }, |
| 56 | + { value: 'inStock', label: 'In Stock', type: 'boolean' } |
| 57 | + ], |
| 58 | + value: { |
| 59 | + id: 'root', |
| 60 | + logic: 'and', |
| 61 | + conditions: [] |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Props |
| 67 | + |
| 68 | +### Schema Properties |
| 69 | + |
| 70 | +| Property | Type | Required | Description | |
| 71 | +|----------|------|----------|-------------| |
| 72 | +| `type` | `string` | ✅ | Must be `'filter-builder'` | |
| 73 | +| `name` | `string` | ✅ | Form field name for the filter value | |
| 74 | +| `label` | `string` | ❌ | Label displayed above the filter builder | |
| 75 | +| `fields` | `Field[]` | ✅ | Array of available fields for filtering | |
| 76 | +| `value` | `FilterGroup` | ❌ | Initial filter configuration | |
| 77 | + |
| 78 | +### Field Type |
| 79 | + |
| 80 | +```typescript |
| 81 | +interface Field { |
| 82 | + value: string; // Field identifier |
| 83 | + label: string; // Display label |
| 84 | + type?: string; // Field type: 'text' | 'number' | 'boolean' |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +### FilterGroup Type |
| 89 | + |
| 90 | +```typescript |
| 91 | +interface FilterGroup { |
| 92 | + id: string; // Group identifier |
| 93 | + logic: 'and' | 'or'; // Logic operator |
| 94 | + conditions: FilterCondition[]; // Array of conditions |
| 95 | +} |
| 96 | + |
| 97 | +interface FilterCondition { |
| 98 | + id: string; // Condition identifier |
| 99 | + field: string; // Field value |
| 100 | + operator: string; // Operator (see below) |
| 101 | + value: string | number | boolean; // Filter value |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Operators |
| 106 | + |
| 107 | +The available operators change based on the field type: |
| 108 | + |
| 109 | +### Text Fields |
| 110 | +- `equals` - Equals |
| 111 | +- `notEquals` - Does not equal |
| 112 | +- `contains` - Contains |
| 113 | +- `notContains` - Does not contain |
| 114 | +- `isEmpty` - Is empty |
| 115 | +- `isNotEmpty` - Is not empty |
| 116 | + |
| 117 | +### Number Fields |
| 118 | +- `equals` - Equals |
| 119 | +- `notEquals` - Does not equal |
| 120 | +- `greaterThan` - Greater than |
| 121 | +- `lessThan` - Less than |
| 122 | +- `greaterOrEqual` - Greater than or equal |
| 123 | +- `lessOrEqual` - Less than or equal |
| 124 | +- `isEmpty` - Is empty |
| 125 | +- `isNotEmpty` - Is not empty |
| 126 | + |
| 127 | +### Boolean Fields |
| 128 | +- `equals` - Equals |
| 129 | +- `notEquals` - Does not equal |
| 130 | + |
| 131 | +## Events |
| 132 | + |
| 133 | +The component emits change events when the filter configuration is modified: |
| 134 | + |
| 135 | +```typescript |
| 136 | +{ |
| 137 | + target: { |
| 138 | + name: 'filters', |
| 139 | + value: { |
| 140 | + id: 'root', |
| 141 | + logic: 'and', |
| 142 | + conditions: [...] |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +## Demo |
| 149 | + |
| 150 | +To see the filter builder in action: |
| 151 | + |
| 152 | +```bash |
| 153 | +pnpm --filter prototype dev |
| 154 | +# Visit http://localhost:5173/?demo=filter-builder |
| 155 | +``` |
| 156 | + |
| 157 | +## Styling |
| 158 | + |
| 159 | +The component is fully styled with Tailwind CSS and follows the Object UI design system. All Shadcn UI components are used for consistent look and feel. |
| 160 | + |
| 161 | +You can customize the appearance using the `className` prop or by overriding Tailwind classes. |
| 162 | + |
| 163 | +## Integration |
| 164 | + |
| 165 | +The Filter Builder integrates seamlessly with Object UI's schema system and can be used in: |
| 166 | + |
| 167 | +- Forms |
| 168 | +- Data tables |
| 169 | +- Search interfaces |
| 170 | +- Admin panels |
| 171 | +- Dashboard filters |
| 172 | + |
| 173 | +## Example in Context |
| 174 | + |
| 175 | +```typescript |
| 176 | +const pageSchema = { |
| 177 | + type: 'page', |
| 178 | + title: 'User Management', |
| 179 | + body: [ |
| 180 | + { |
| 181 | + type: 'card', |
| 182 | + body: [ |
| 183 | + { |
| 184 | + type: 'filter-builder', |
| 185 | + name: 'userFilters', |
| 186 | + label: 'Filter Users', |
| 187 | + fields: [ |
| 188 | + { value: 'name', label: 'Name', type: 'text' }, |
| 189 | + { value: 'email', label: 'Email', type: 'text' }, |
| 190 | + { value: 'age', label: 'Age', type: 'number' }, |
| 191 | + { value: 'department', label: 'Department', type: 'text' }, |
| 192 | + { value: 'active', label: 'Active', type: 'boolean' } |
| 193 | + ] |
| 194 | + } |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + type: 'table', |
| 199 | + // table configuration... |
| 200 | + } |
| 201 | + ] |
| 202 | +}; |
| 203 | +``` |
| 204 | + |
| 205 | +## Technical Details |
| 206 | + |
| 207 | +- Built with React 18+ hooks |
| 208 | +- Uses Radix UI primitives (Select, Popover) |
| 209 | +- Type-safe with TypeScript |
| 210 | +- Accessible keyboard navigation |
| 211 | +- Responsive design |
| 212 | + |
| 213 | +## Browser Support |
| 214 | + |
| 215 | +Works in all modern browsers that support: |
| 216 | +- ES6+ |
| 217 | +- CSS Grid |
| 218 | +- Flexbox |
| 219 | +- crypto.randomUUID() |
0 commit comments