-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #247 from heorhi-deriv/FEQ-2641-Implement-Section-…
…Message-Component [FEQ] george / FEQ-2641 / Implement Section Message Component
- Loading branch information
Showing
7 changed files
with
336 additions
and
14 deletions.
There are no files selected for viewing
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
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,72 @@ | ||
$error-bgColor: $color-status-error-1; | ||
$error-icColor: $color-red-11; | ||
$warning-bgColor: $color-status-warning-1; | ||
$warning-icColor: $color-yellow-4; | ||
$success-bgColor: $color-status-success; | ||
$success-icColor: $color-green-9; | ||
$info-bgColor: $color-status-information-1; | ||
$info-icColor: $color-blue-11; | ||
$description-color: $alpha-color-black-8; | ||
$general-color: $color-gray-2; | ||
|
||
.deriv-section-message { | ||
width: 100%; | ||
display: inline-flex; | ||
align-items: center; | ||
column-gap: 8px; | ||
border-radius: 16px; | ||
padding: 16px; | ||
|
||
&__title { | ||
margin-bottom: 8px; | ||
} | ||
|
||
&__description { | ||
color: $description-color; | ||
} | ||
|
||
&__cta-section { | ||
margin-top: 16px; | ||
} | ||
|
||
&__icon { | ||
display: flex; | ||
align-self: flex-start; | ||
|
||
&--info { | ||
fill: $info-icColor; | ||
} | ||
|
||
&--success { | ||
fill: $success-icColor; | ||
} | ||
|
||
&--warning { | ||
fill: $warning-icColor; | ||
} | ||
|
||
&--error { | ||
fill: $error-icColor; | ||
} | ||
} | ||
|
||
&--info { | ||
background-color: $info-bgColor; | ||
} | ||
|
||
&--success { | ||
background-color: $success-bgColor; | ||
} | ||
|
||
&--warning { | ||
background-color: $warning-bgColor; | ||
} | ||
|
||
&--error { | ||
background-color: $error-bgColor; | ||
} | ||
|
||
&--general { | ||
background-color: $general-color; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/components/SectionMessage/__test__/SectionMessage.spec.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,59 @@ | ||
import React from "react"; | ||
import { render } from "@testing-library/react"; | ||
import { SectionMessage } from ".."; | ||
|
||
describe("SectionMessage Component", () => { | ||
it("renders with all props", () => { | ||
const ctaSection = <button type="button">Click me</button>; | ||
const { getByText, getByRole } = render( | ||
<SectionMessage title="Title" ctaSection={ctaSection}> | ||
Warning Message | ||
</SectionMessage>, | ||
); | ||
expect(getByText("Warning Message")).toBeInTheDocument(); | ||
expect(getByText("Title")).toBeInTheDocument(); | ||
expect(getByRole("button", { name: "Click me" })).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders with specified variant", () => { | ||
const { container } = render( | ||
<SectionMessage variant="error">Error Message</SectionMessage>, | ||
); | ||
expect(container.firstChild).toHaveClass( | ||
"deriv-section-message--error", | ||
); | ||
}); | ||
|
||
it("renders with predefined error icon", () => { | ||
const { container } = render( | ||
<SectionMessage variant="error">Message with Icon</SectionMessage>, | ||
); | ||
expect( | ||
container.querySelector(".deriv-section-message__icon"), | ||
).toBeInTheDocument(); | ||
expect( | ||
container.querySelector(".deriv-section-message__icon--error"), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders with custom icon", () => { | ||
const Img = <img src="" alt="icon" />; | ||
|
||
const { container, getByAltText } = render( | ||
<SectionMessage icon={Img}>Message with Icon</SectionMessage>, | ||
); | ||
expect(getByAltText("icon")).toBeInTheDocument(); | ||
expect( | ||
container.querySelector(".deriv-section-message__icon"), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders with custom class name", () => { | ||
const { container } = render( | ||
<SectionMessage className="custom-class"> | ||
Custom Message | ||
</SectionMessage>, | ||
); | ||
expect(container.firstChild).toHaveClass("custom-class"); | ||
}); | ||
}); |
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,96 @@ | ||
import React from "react"; | ||
import { | ||
LabelPairedCircleCheckMdBoldIcon, | ||
LabelPairedCircleExclamationMdBoldIcon, | ||
LabelPairedCircleInfoMdBoldIcon, | ||
LabelPairedTriangleExclamationMdBoldIcon, | ||
IconTypes, | ||
} from "@deriv/quill-icons"; | ||
import clsx from "clsx"; | ||
import { Text } from "../Text"; | ||
import "./SectionMessage.scss"; | ||
|
||
type TVariant = "warning" | "info" | "success" | "error" | "general"; | ||
|
||
type SectionMessageProps = { | ||
children: JSX.Element | string; | ||
className?: string; | ||
ctaSection?: JSX.Element; | ||
icon?: JSX.Element; | ||
title?: JSX.Element | string; | ||
variant?: TVariant; | ||
}; | ||
|
||
const VariantIcons: Record<Exclude<TVariant, "general">, IconTypes> = { | ||
error: LabelPairedTriangleExclamationMdBoldIcon, | ||
info: LabelPairedCircleInfoMdBoldIcon, | ||
success: LabelPairedCircleCheckMdBoldIcon, | ||
warning: LabelPairedCircleExclamationMdBoldIcon, | ||
}; | ||
|
||
/** | ||
* `SectionMessage` is a React component designed to display a styled section message. | ||
* It includes an optional icon, title, description, and call-to-action (CTA) section. | ||
* | ||
* @component | ||
* @param {Object} props - Component props. | ||
* @param {JSX.Element|string} props.children - The main content or message of the section. | ||
* @param {string} [props.className] - Optional additional class names for styling. | ||
* @param {JSX.Element} [props.ctaSection] - Optional CTA section element, such as a button / button group or link. | ||
* @param {JSX.Element} [props.icon] - Optional icon element. | ||
* @param {JSX.Element|string} [props.title] - Optional title element or string to summarize the message. | ||
* @param {TVariant} [props.variant="general"] - Optional variant to style the section with different themes: "warning", "info", "success", "error", or "general". | ||
*/ | ||
export const SectionMessage = ({ | ||
children, | ||
className, | ||
ctaSection, | ||
icon, | ||
title, | ||
variant = "general", | ||
}: SectionMessageProps) => { | ||
const IconComponent = variant !== "general" && VariantIcons[variant]; | ||
|
||
return ( | ||
<div | ||
className={clsx( | ||
"deriv-section-message", | ||
`deriv-section-message--${variant}`, | ||
className, | ||
)} | ||
> | ||
{(icon || IconComponent) && ( | ||
<div className="deriv-section-message__icon"> | ||
{IconComponent ? ( | ||
<IconComponent | ||
className={`deriv-section-message__icon--${variant}`} | ||
/> | ||
) : ( | ||
icon | ||
)} | ||
</div> | ||
)} | ||
<div className="deriv-section-message__content"> | ||
{title && ( | ||
<Text | ||
as="div" | ||
align="start" | ||
size="md" | ||
weight="bold" | ||
className="deriv-section-message__title" | ||
> | ||
{title} | ||
</Text> | ||
)} | ||
<div className="deriv-section-message__description"> | ||
{children} | ||
</div> | ||
{ctaSection && ( | ||
<div className="deriv-section-message__cta-section"> | ||
{ctaSection} | ||
</div> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; |
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
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
Oops, something went wrong.