|
| 1 | +# DRAFT: Build Search-Based Filtering Systems |
| 2 | + |
| 3 | +**Final Destination:** `docs/router/framework/react/how-to/build-search-filtering-systems.md` |
| 4 | +**Progressive Series Position:** Specialized Use Cases - Guide #9 |
| 5 | +**Depends On:** `setup-basic-search-params.md`, `navigate-with-search-params.md`, `validate-search-params.md` |
| 6 | +**Status:** Ready for implementation - comprehensive UI patterns available |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Content Staged from navigate-with-search-params.md |
| 11 | + |
| 12 | +### Search Result Navigation |
| 13 | + |
| 14 | +Handle search result navigation with query preservation: |
| 15 | + |
| 16 | +```tsx |
| 17 | +import { Link, useSearch } from '@tanstack/react-router' |
| 18 | + |
| 19 | +function SearchResults() { |
| 20 | + const search = useSearch({ from: '/search' }) |
| 21 | + |
| 22 | + return ( |
| 23 | + <div> |
| 24 | + {/* Preserve search query, change view */} |
| 25 | + <nav> |
| 26 | + <Link search={(prev) => ({ ...prev, view: 'grid' })}> |
| 27 | + Grid View |
| 28 | + </Link> |
| 29 | + <Link search={(prev) => ({ ...prev, view: 'list' })}> |
| 30 | + List View |
| 31 | + </Link> |
| 32 | + </nav> |
| 33 | + |
| 34 | + {/* Pagination with query preservation */} |
| 35 | + <div> |
| 36 | + {search.page > 1 && ( |
| 37 | + <Link search={(prev) => ({ ...prev, page: prev.page - 1 })}> |
| 38 | + Previous |
| 39 | + </Link> |
| 40 | + )} |
| 41 | + |
| 42 | + <Link search={(prev) => ({ ...prev, page: (prev.page || 1) + 1 })}> |
| 43 | + Next |
| 44 | + </Link> |
| 45 | + </div> |
| 46 | + |
| 47 | + {/* Related searches */} |
| 48 | + <div> |
| 49 | + Related searches: |
| 50 | + {[ |
| 51 | + 'laptops gaming', |
| 52 | + 'laptops business', |
| 53 | + 'laptops student' |
| 54 | + ].map(suggestion => ( |
| 55 | + <Link |
| 56 | + key={suggestion} |
| 57 | + search={(prev) => ({ ...prev, query: suggestion, page: 1 })} |
| 58 | + > |
| 59 | + {suggestion} |
| 60 | + </Link> |
| 61 | + ))} |
| 62 | + </div> |
| 63 | + </div> |
| 64 | + ) |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### Filter Navigation |
| 69 | + |
| 70 | +Build filtering interfaces with search parameter navigation: |
| 71 | + |
| 72 | +```tsx |
| 73 | +import { Link, useSearch } from '@tanstack/react-router' |
| 74 | + |
| 75 | +const categories = ['electronics', 'clothing', 'books', 'home'] |
| 76 | +const sortOptions = [ |
| 77 | + { value: 'relevance', label: 'Relevance' }, |
| 78 | + { value: 'price-asc', label: 'Price: Low to High' }, |
| 79 | + { value: 'price-desc', label: 'Price: High to Low' }, |
| 80 | + { value: 'rating', label: 'Customer Rating' } |
| 81 | +] |
| 82 | + |
| 83 | +function FilterNavigation() { |
| 84 | + const search = useSearch({ from: '/products' }) |
| 85 | + |
| 86 | + return ( |
| 87 | + <aside> |
| 88 | + {/* Category filters */} |
| 89 | + <div> |
| 90 | + <h3>Categories</h3> |
| 91 | + {categories.map(category => ( |
| 92 | + <Link |
| 93 | + key={category} |
| 94 | + search={(prev) => ({ |
| 95 | + ...prev, |
| 96 | + category: prev.category === category ? undefined : category, |
| 97 | + page: 1 |
| 98 | + })} |
| 99 | + className={search.category === category ? 'active' : ''} |
| 100 | + > |
| 101 | + {category} |
| 102 | + </Link> |
| 103 | + ))} |
| 104 | + </div> |
| 105 | + |
| 106 | + {/* Sort options */} |
| 107 | + <div> |
| 108 | + <h3>Sort By</h3> |
| 109 | + {sortOptions.map(option => ( |
| 110 | + <Link |
| 111 | + key={option.value} |
| 112 | + search={(prev) => ({ ...prev, sort: option.value, page: 1 })} |
| 113 | + className={search.sort === option.value ? 'active' : ''} |
| 114 | + > |
| 115 | + {option.label} |
| 116 | + </Link> |
| 117 | + ))} |
| 118 | + </div> |
| 119 | + |
| 120 | + {/* Clear all filters */} |
| 121 | + <Link |
| 122 | + search={(prev) => { |
| 123 | + const { category, sort, minPrice, maxPrice, ...rest } = prev |
| 124 | + return rest |
| 125 | + }} |
| 126 | + > |
| 127 | + Clear All Filters |
| 128 | + </Link> |
| 129 | + </aside> |
| 130 | + ) |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### Programmatic Search Controls |
| 135 | + |
| 136 | +Navigate programmatically with search parameter updates: |
| 137 | + |
| 138 | +```tsx |
| 139 | +import { useNavigate } from '@tanstack/react-router' |
| 140 | + |
| 141 | +function SearchControls() { |
| 142 | + const navigate = useNavigate() |
| 143 | + |
| 144 | + const handleSortChange = (sortBy: string) => { |
| 145 | + navigate({ |
| 146 | + search: (prev) => ({ ...prev, sort: sortBy, page: 1 }) |
| 147 | + }) |
| 148 | + } |
| 149 | + |
| 150 | + const handleClearFilters = () => { |
| 151 | + navigate({ |
| 152 | + search: (prev) => { |
| 153 | + const { category, minPrice, maxPrice, ...rest } = prev |
| 154 | + return rest |
| 155 | + } |
| 156 | + }) |
| 157 | + } |
| 158 | + |
| 159 | + return ( |
| 160 | + <div> |
| 161 | + <select onChange={(e) => handleSortChange(e.target.value)}> |
| 162 | + <option value="relevance">Sort by Relevance</option> |
| 163 | + <option value="price-asc">Price: Low to High</option> |
| 164 | + <option value="price-desc">Price: High to Low</option> |
| 165 | + </select> |
| 166 | + |
| 167 | + <button onClick={handleClearFilters}> |
| 168 | + Clear Filters |
| 169 | + </button> |
| 170 | + </div> |
| 171 | + ) |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## Implementation Notes |
| 178 | + |
| 179 | +### Additional Content Needed: |
| 180 | +- [ ] Complete e-commerce filtering example |
| 181 | +- [ ] Advanced filter combinations (price ranges, multi-select) |
| 182 | +- [ ] Filter state persistence and sharing |
| 183 | +- [ ] Search result highlighting and sorting |
| 184 | +- [ ] Infinite scroll pagination patterns |
| 185 | +- [ ] Filter URL state management best practices |
| 186 | +- [ ] Accessibility considerations for filter UIs |
| 187 | +- [ ] Mobile-responsive filter patterns |
| 188 | + |
| 189 | +### Cross-References to Add: |
| 190 | +- Link to `setup-basic-search-params.md` for foundation |
| 191 | +- Link to `navigate-with-search-params.md` for navigation patterns |
| 192 | +- Link to `validate-search-params.md` for filter validation |
| 193 | +- Forward link to `search-params-with-data-loading.md` for data integration |
| 194 | + |
| 195 | +### README Update Required: |
| 196 | +- [ ] Mark guide as completed in progressive series |
| 197 | +- [ ] Uncomment "Common Next Steps" in related guides |
0 commit comments