-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathSearchControls.tsx
More file actions
241 lines (218 loc) · 7.12 KB
/
SearchControls.tsx
File metadata and controls
241 lines (218 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { Input, Button, Select, Tag, Segmented, DatePicker } from "antd";
import {
BarsOutlined,
AppstoreOutlined,
SearchOutlined,
ReloadOutlined,
} from "@ant-design/icons";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
interface FilterOption {
key: string;
label: string;
mode?: "tags" | "multiple";
options: { label: string; value: string }[];
}
interface SearchControlsProps {
searchTerm: string;
onSearchChange: (value: string) => void;
searchPlaceholder?: string;
// Filter props
filters?: FilterOption[];
selectedFilters?: Record<string, string[]>;
onFiltersChange?: (filters: Record<string, string[]>) => void;
onClearFilters?: () => void;
// Date range props
dateRange?: [Date | null, Date | null] | null;
onDateChange?: (dates: [Date | null, Date | null] | null) => void;
// Reload props
onReload?: () => void;
// View props
viewMode?: "card" | "list";
onViewModeChange?: (mode: "card" | "list") => void;
// Control visibility
showFilters?: boolean;
showSort?: boolean;
showViewToggle?: boolean;
showReload?: boolean;
showDatePicker?: boolean;
// Styling
className?: string;
}
export function SearchControls({
viewMode,
className,
searchTerm,
showFilters = true,
showViewToggle = true,
searchPlaceholder,
filters = [],
dateRange,
showDatePicker = false,
showReload = true,
onReload,
onDateChange,
onSearchChange,
onFiltersChange,
onViewModeChange,
onClearFilters,
}: SearchControlsProps) {
const { t } = useTranslation();
const [selectedFilters, setSelectedFilters] = useState<{
[key: string]: string[];
}>({});
const filtersMap: Record<string, FilterOption> = filters.reduce(
(prev, cur) => ({ ...prev, [cur.key]: cur }),
{}
);
// select change
const handleFilterChange = (filterKey: string, value: string) => {
const filteredValues = {
...selectedFilters,
[filterKey]: !value ? [] : [value],
};
setSelectedFilters(filteredValues);
};
// 清除已选筛选
const handleClearFilter = (filterKey: string, value: string | string[]) => {
const isMultiple = filtersMap[filterKey]?.mode === "multiple";
if (!isMultiple) {
setSelectedFilters({
...selectedFilters,
[filterKey]: [],
});
} else {
const currentValues = selectedFilters[filterKey]?.[0] || [];
const newValues = currentValues.filter((v) => v !== value);
setSelectedFilters({
...selectedFilters,
[filterKey]: [newValues],
});
}
};
const handleClearAllFilters = () => {
setSelectedFilters({});
onClearFilters?.();
};
const hasActiveFilters = Object.values(selectedFilters).some(
(values) => values?.[0]?.length > 0
);
useEffect(() => {
if (Object.keys(selectedFilters).length === 0) return;
onFiltersChange?.(selectedFilters);
}, [selectedFilters]);
return (
<div className={className}>
<div className="flex items-center justify-between gap-8">
{/* Left side - Search and Filters */}
<div className="flex items-center gap-2 flex-1">
{/* Search */}
<div className="relative flex-1">
<Input
allowClear
placeholder={searchPlaceholder || t('components.searchControls.searchPlaceholder')}
value={searchTerm}
onChange={(e) => onSearchChange(e.target.value)}
prefix={<SearchOutlined className="w-4 h-4 text-gray-400" />}
/>
</div>
{/* Filters */}
{showFilters && filters.length > 0 && (
<div className="flex items-center gap-2">
{filters.map((filter: FilterOption) => (
<Select
maxTagCount="responsive"
mode={filter.mode}
key={filter.key}
placeholder={filter.label}
value={selectedFilters[filter.key]?.[0] || undefined}
onChange={(value) => handleFilterChange(filter.key, value)}
style={{ width: 144 }}
allowClear
>
{filter.options.map((option) => (
<Select.Option key={option.value} value={option.value}>
{option.label}
</Select.Option>
))}
</Select>
))}
</div>
)}
</div>
{showDatePicker && (
<DatePicker.RangePicker
value={dateRange as any}
onChange={onDateChange}
style={{ width: 260 }}
allowClear
placeholder={[t('components.searchControls.startTime'), t('components.searchControls.endTime')]}
/>
)}
{/* Right side */}
<div className="flex items-center gap-2">
{showViewToggle && onViewModeChange && (
<Segmented
options={[
{ value: "list", icon: <BarsOutlined /> },
{ value: "card", icon: <AppstoreOutlined /> },
]}
value={viewMode}
onChange={(value) => onViewModeChange(value as "list" | "card")}
/>
)}
{showReload && (
<Button
icon={<ReloadOutlined />}
onClick={() => onReload?.()}
></Button>
)}
</div>
</div>
{/* Active Filters Display */}
{hasActiveFilters && (
<div className="mt-4 pt-4 border-t border-gray-200">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2 flex-wrap flex-1">
<span className="text-sm font-medium text-gray-700">
{t('components.searchControls.filters.label')}
</span>
{Object.entries(selectedFilters).map(([filterKey, values]) =>
values.map((value) => {
const filter = filtersMap[filterKey];
const getLabeledValue = (item: string) => {
const option = filter?.options.find(
(o) => o.value === item
);
return (
<Tag
key={`${filterKey}-${item}`}
closable
onClose={() => handleClearFilter(filterKey, item)}
color="blue"
>
{filter?.label}: {option?.label || item}
</Tag>
);
};
return Array.isArray(value)
? value.map((item) => getLabeledValue(item))
: getLabeledValue(value);
})
)}
</div>
{/* Clear all filters button on the right */}
<Button
type="text"
size="small"
onClick={handleClearAllFilters}
className="text-gray-500 hover:text-gray-700"
>
{t('components.searchControls.filters.clearAll')}
</Button>
</div>
</div>
)}
</div>
);
}