-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
182 lines (173 loc) · 6.7 KB
/
index.html
File metadata and controls
182 lines (173 loc) · 6.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React Search Interface</title>
<!-- Include TailwindCSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Include React and ReactDOM via CDN -->
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<!-- Include Babel for JSX compilation -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- Include lodash for debouncing -->
<script src="https://cdn.jsdelivr.net/npm/lodash/lodash.min.js"></script>
<!-- Include Lucide Icons -->
<script src="https://cdn.jsdelivr.net/npm/lucide@0.123.2/dist/lucide.js"></script>
<style>
body {
font-family: Arial, sans-serif;
}
</style>
</head>
<body>
<div id="root"></div>
<!-- React and JavaScript code -->
<script type="text/babel">
const { useState, useEffect } = React;
const SearchInterface = () => {
const [searchTerm, setSearchTerm] = useState('');
const [filters, setFilters] = useState({
category: 'all',
sortBy: 'relevance',
itemsPerPage: 10,
});
const [results, setResults] = useState([]);
const [suggestions, setSuggestions] = useState([]);
const [loading, setLoading] = useState(false);
// Simulated data
const sampleData = [
{
id: 1,
title: 'JavaScript Basics',
category: 'programming',
content: 'Learn the basics of JavaScript programming',
},
{
id: 2,
title: 'React Fundamentals',
category: 'programming',
content: 'Understanding React core concepts',
},
{
id: 3,
title: 'Database Design',
category: 'database',
content: 'Introduction to database design principles',
},
];
const fetchSearchResults = async (term, filters) => {
setLoading(true);
await new Promise((resolve) => setTimeout(resolve, 500));
const filtered = sampleData.filter((item) => {
if (filters.category !== 'all' && item.category !== filters.category) return false;
return (
item.title.toLowerCase().includes(term.toLowerCase()) ||
item.content.toLowerCase().includes(term.toLowerCase())
);
});
setResults(filtered);
setLoading(false);
};
const debouncedSearch = _.debounce((term, filters) => {
fetchSearchResults(term, filters);
}, 300);
useEffect(() => {
if (searchTerm) {
debouncedSearch(searchTerm, filters);
} else {
setResults([]);
}
}, [searchTerm, filters]);
useEffect(() => {
if (searchTerm.length > 2) {
const matchingSuggestions = sampleData
.filter((item) =>
item.title.toLowerCase().includes(searchTerm.toLowerCase())
)
.map((item) => item.title)
.slice(0, 5);
setSuggestions(matchingSuggestions);
} else {
setSuggestions([]);
}
}, [searchTerm]);
return (
<div className="max-w-4xl mx-auto p-4">
<div className="bg-white shadow rounded p-6">
<h1 className="text-xl font-semibold mb-4">Database Search</h1>
<div className="relative">
<input
type="text"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
placeholder="Search database..."
className="w-full p-2 pr-10 border rounded-lg"
/>
<span
className="absolute right-3 top-2.5 h-5 w-5 text-gray-400"
aria-hidden="true"
>
🔍
</span>
{suggestions.length > 0 && (
<div className="absolute z-10 w-full bg-white border rounded-lg mt-1 shadow-lg">
{suggestions.map((suggestion, index) => (
<div
key={index}
className="p-2 hover:bg-gray-100 cursor-pointer"
onClick={() => setSearchTerm(suggestion)}
>
{suggestion}
</div>
))}
</div>
)}
</div>
<div className="flex gap-4 mt-4">
<select
value={filters.category}
onChange={(e) =>
setFilters({ ...filters, category: e.target.value })
}
className="p-2 border rounded-lg"
>
<option value="all">All Categories</option>
<option value="programming">Programming</option>
<option value="database">Database</option>
</select>
<select
value={filters.sortBy}
onChange={(e) =>
setFilters({ ...filters, sortBy: e.target.value })
}
className="p-2 border rounded-lg"
>
<option value="relevance">Sort by Relevance</option>
<option value="date">Sort by Date</option>
<option value="title">Sort by Title</option>
</select>
</div>
{loading && <div className="mt-4">Loading...</div>}
<div className="mt-4">
{results.map((result) => (
<div key={result.id} className="p-4 border rounded-lg mb-2">
<h3 className="font-semibold">{result.title}</h3>
<p className="text-sm text-gray-600">{result.category}</p>
<p>{result.content}</p>
</div>
))}
{!loading && results.length === 0 && searchTerm && (
<div>No results found for "{searchTerm}"</div>
)}
</div>
</div>
</div>
);
};
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<SearchInterface />);
</script>
</body>
</html>