Skip to content

Commit b32df18

Browse files
author
Gabriel Gavilan
committed
Add highlighting in search.
1 parent 1120104 commit b32df18

3 files changed

Lines changed: 48 additions & 34 deletions

File tree

packages/preview/src/components/@core/icon/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ import toast from "cogo-toast";
22
import copy from "copy-to-clipboard";
33
import React from "react";
44

5-
function Icon({ icon, name }) {
5+
function Icon({ icon, name, highlightPattern = null }) {
66
const copyToClipboard = () => {
77
copy(name);
88
toast.success(`Copied '${name}' to clipboard`, {
99
position: "bottom-center"
1010
});
1111
};
1212

13+
const highlightedName = () => {
14+
if (highlightPattern)
15+
return name
16+
.split(highlightPattern)
17+
.map((part) => (part.match(highlightPattern) ? <b>{part}</b> : part));
18+
return name;
19+
};
20+
1321
return (
1422
<div className="item" tabIndex={0} onClick={copyToClipboard} key={name}>
1523
<div className="icon h2">{typeof icon === "function" && icon()}</div>
16-
<div className="name">{name}</div>
24+
<div className="name">{highlightedName()}</div>
1725
</div>
1826
);
1927
}

packages/preview/src/components/pages/search/index.tsx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,27 @@ export default function SearchPageComponent() {
1515
0
1616
}
1717

18-
return query.length > 2 ? (
19-
<>
20-
<h2>
21-
Results for: <i>{query}</i>
22-
</h2>
23-
<div className="icons">
24-
{allIcons.map(icon => (
25-
<SearchIconSet
26-
key={icon.id}
27-
icon={icon}
28-
query={query}
29-
setResults={setResults} />
30-
))}
31-
</div>
32-
{
33-
getTotal(results) === 0 &&
34-
<h3>No icons found</h3>
35-
}
36-
</>
37-
) : (
38-
<h2>Please enter at least 3 characters to search...</h2>
39-
);
18+
if (query.length > 2) {
19+
const hightlightPattern = new RegExp(`(${query})`, "i");
20+
return (
21+
<>
22+
<h2>
23+
Results for: <i>{query}</i>
24+
</h2>
25+
<div className="icons">
26+
{allIcons.map((icon) => (
27+
<SearchIconSet
28+
key={icon.id}
29+
icon={icon}
30+
query={query}
31+
setResults={setResults}
32+
highlightPattern={hightlightPattern}
33+
/>
34+
))}
35+
</div>
36+
{getTotal(results) === 0 && <h3>No icons found</h3>}
37+
</>
38+
);
39+
}
40+
return <h2>Please enter at least 3 characters to search...</h2>;
4041
}

packages/preview/src/components/pages/search/search-iconset.tsx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getIcons } from "@utils/getIcons";
55

66
import SearchPageIconLoading from "./loading";
77

8-
export default function SearchIconSet({ icon, query, setResults }) {
8+
export default function SearchIconSet({ icon, query, setResults, highlightPattern }) {
99
const IconSet = loadable.lib(() => getIcons(icon.id));
1010

1111
return (
@@ -16,16 +16,21 @@ export default function SearchIconSet({ icon, query, setResults }) {
1616
return (
1717
<>
1818
{found.map(name => (
19-
<Icon key={name} icon={icons[name]} name={name} />
20-
))}
21-
{setResults(prevResults => {
22-
return prevResults.hasOwnProperty(icon.id) ?
23-
prevResults :
24-
{
19+
<Icon
20+
key={name}
21+
icon={icons[name]}
22+
name={name}
23+
highlightPattern={highlightPattern}
24+
/>
25+
))}
26+
{setResults(prevResults => {
27+
return prevResults.hasOwnProperty(icon.id) ?
28+
prevResults :
29+
{
2530
...prevResults,
26-
[icon.id]: found.length
27-
}
28-
})}
31+
[icon.id]: found.length
32+
}
33+
})}
2934
</>
3035
)
3136
}}

0 commit comments

Comments
 (0)