-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
49 lines (46 loc) · 1.33 KB
/
App.jsx
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
import React from 'react';
import { useState } from 'react';
export function App(props) {
const [searchTerm, setSearchTerm] = useState('');
const data = [
{ id: 1, name: 'John Doe', email: '[email protected]' },
{ id: 2, name: 'Jane Smith', email: '[email protected]' },
{ id: 3, name: 'Bob Johnson', email: '[email protected]' },
{ id: 4, name: 'Alice Williams', email: '[email protected]' },
];
const highlightText = (text, term) => {
const regex = new RegExp(`(${term})`, 'gi');
return text.replace(regex, '<span style="color: yellow;">$1</span>');
};
const filteredData = data.filter((item) =>
Object.values(item).some((value) =>
value.toString().toLowerCase().includes(searchTerm.toLowerCase())
)
);
return (
<div className='App'>
<div>
<input
type="text"
placeholder="Search..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
{filteredData.map((item) => (
<div key={item.id}>
<h3
dangerouslySetInnerHTML={{
__html: highlightText(item.name, searchTerm),
}}
/>
<p
dangerouslySetInnerHTML={{
__html: highlightText(item.email, searchTerm),
}}
/>
</div>
))}
</div>
</div>
);
}