Skip to content

Commit c3a37fe

Browse files
committed
feat: add highlightKeyword function and apply it to authors field in DatasetCard for test; refs #60
1 parent f8ad760 commit c3a37fe

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/components/SearchPage/DatasetCard.tsx

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface DatasetCardProps {
2222
};
2323
index: number;
2424
onChipClick: (key: string, value: string) => void;
25+
keyword?: string; // for keyword highlight
2526
}
2627

2728
const DatasetCard: React.FC<DatasetCardProps> = ({
@@ -30,6 +31,7 @@ const DatasetCard: React.FC<DatasetCardProps> = ({
3031
parsedJson,
3132
index,
3233
onChipClick,
34+
keyword, //
3335
}) => {
3436
const { name, readme, modality, subj, info } = parsedJson.value;
3537
const datasetLink = `${RoutesEnum.DATABASES}/${dbname}/${dsname}`;
@@ -38,6 +40,33 @@ const DatasetCard: React.FC<DatasetCardProps> = ({
3840
const rawDOI = info?.DatasetDOI?.replace(/^doi:/, "");
3941
const doiLink = rawDOI ? `https://doi.org/${rawDOI}` : null;
4042

43+
// keyword hightlight functional component
44+
const highlightKeyword = (text: string, keyword?: string) => {
45+
if (!keyword || !text?.toLowerCase().includes(keyword.toLowerCase())) {
46+
return text;
47+
}
48+
49+
const regex = new RegExp(`(${keyword})`, "gi");
50+
const parts = text.split(regex);
51+
52+
return (
53+
<>
54+
{parts.map((part, i) =>
55+
part.toLowerCase() === keyword.toLowerCase() ? (
56+
<mark
57+
key={i}
58+
style={{ backgroundColor: "yellow", fontWeight: 600 }}
59+
>
60+
{part}
61+
</mark>
62+
) : (
63+
<React.Fragment key={i}>{part}</React.Fragment>
64+
)
65+
)}
66+
</>
67+
);
68+
};
69+
4170
return (
4271
<Card sx={{ mb: 3, position: "relative" }}>
4372
<CardContent>
@@ -68,6 +97,7 @@ const DatasetCard: React.FC<DatasetCardProps> = ({
6897
target="_blank"
6998
>
7099
{name || "Untitled Dataset"}
100+
{/* {highlightKeyword(name || "Untitled Dataset", keyword)} */}
71101
</Typography>
72102
<Typography>
73103
Database: {dbname} &nbsp;&nbsp;|&nbsp;&nbsp; Dataset: {dsname}
@@ -130,6 +160,7 @@ const DatasetCard: React.FC<DatasetCardProps> = ({
130160
sx={{ textOverflow: "ellipsis" }}
131161
>
132162
<strong>Summary:</strong> {readme}
163+
{/* <strong>Summary:</strong> {highlightKeyword(readme, keyword)} */}
133164
</Typography>
134165
)}
135166
</Stack>
@@ -139,11 +170,14 @@ const DatasetCard: React.FC<DatasetCardProps> = ({
139170
{info?.Authors && (
140171
<Typography variant="body2" mt={1}>
141172
<strong>Authors:</strong>{" "}
142-
{Array.isArray(info.Authors)
143-
? info.Authors.join(", ")
144-
: typeof info.Authors === "string"
145-
? info.Authors
146-
: "N/A"}
173+
{highlightKeyword(
174+
Array.isArray(info.Authors)
175+
? info.Authors.join(", ")
176+
: typeof info.Authors === "string"
177+
? info.Authors
178+
: "N/A",
179+
keyword
180+
)}
147181
</Typography>
148182
)}
149183
</Typography>

src/pages/SearchPage.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ const SearchPage: React.FC = () => {
556556
dsname={item.dsname}
557557
parsedJson={parsedJson}
558558
onChipClick={handleChipClick}
559+
keyword={formData.keyword} // for keyword highlight
559560
/>
560561
) : (
561562
<SubjectCard

0 commit comments

Comments
 (0)