-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a505a5
commit 826ac8a
Showing
5 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/components/highlighted-text/HighlightedText.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { HighlightedText } from "./HighlightedText"; | ||
|
||
const meta: Meta<typeof HighlightedText> = { | ||
title: "Components/HighlightedText", | ||
component: HighlightedText, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof HighlightedText>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
text: "Lorem test string Test string", | ||
highlight: "TesT", | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React, { ComponentProps } from "react"; | ||
import { splitTextWithHighlight } from "../../utils"; | ||
|
||
import styles from "./styles/index.module.scss"; | ||
|
||
interface IHighlightedTextProps { | ||
text: string; | ||
highlight?: string; | ||
className?: ComponentProps<"div">["className"]; | ||
} | ||
|
||
export const HighlightedText: React.FC<IHighlightedTextProps> = (props) => { | ||
const { highlight, text, className } = props; | ||
|
||
if (!highlight) { | ||
return text; | ||
} | ||
|
||
const parts = splitTextWithHighlight(text, highlight); | ||
|
||
return ( | ||
<span className={className}> | ||
{parts.map((part, index) => { | ||
if (part.toLowerCase() === highlight.toLowerCase()) { | ||
return ( | ||
<span className={styles.highlight_text} key={`h-text-${index}`}> | ||
{part} | ||
</span> | ||
); | ||
} | ||
return <span key={`nh-text-${index}`}>{part}</span>; | ||
})} | ||
</span> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./HighlightedText"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.highlight_text { | ||
background-color: var(--pv-color-attention-tint-4); | ||
color: var(--pv-color-attention-shade-3); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters