-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Niloofar/Added menu item component #189
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,16 @@ | ||
.deriv-menu-item { | ||
display: flex; | ||
align-items: center; | ||
height: 100%; | ||
cursor: pointer; | ||
|
||
&:hover { | ||
background: var(--du-general-hover, #e6e9e9); | ||
} | ||
|
||
&--active { | ||
&:hover { | ||
background: transparent; | ||
} | ||
} | ||
} |
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,71 @@ | ||
import { | ||
ComponentProps, | ||
ElementType, | ||
PropsWithChildren, | ||
ReactNode, | ||
} from "react"; | ||
import clsx from "clsx"; | ||
import "./MenuItem.scss"; | ||
|
||
/** | ||
* Type definition for MenuItem props. | ||
* @typedef TMenuItem | ||
* @property {'a' | 'button'} as - The element type to render, 'a' for anchor or 'button' for button. | ||
* @property {ReactNode} leftComponent - Optional component to display on the left side. | ||
* @property {ReactNode} rightComponent - Optional component to display on the right side. | ||
* @property {boolean} disableHover - If true, disables hover effects. | ||
* @property {boolean} active - If true, applies an active state style. | ||
*/ | ||
interface TMenuItem extends ComponentProps<ElementType> { | ||
as?: "a" | "button"; | ||
leftComponent?: ReactNode; | ||
rightComponent?: ReactNode; | ||
disableHover?: boolean; | ||
active?: boolean; | ||
} | ||
|
||
/** | ||
* MenuItem component that can render as either an anchor or a button element, with optional left and right components. | ||
* The component uses the `as` prop to determine which HTML element to render. | ||
* It supports additional HTML attributes which are spread into the resulting element. | ||
* | ||
* @param {PropsWithChildren<TMenuItem>} props - The props object for the MenuItem component. | ||
* @param {'a' | 'button'} props.as - Determines the element type ('a' or 'button'). | ||
* @param {ReactNode} props.leftComponent - Optional component rendered on the left side of the MenuItem. | ||
* @param {ReactNode} props.children - The main content of the MenuItem. | ||
* @param {ReactNode} props.rightComponent - Optional component rendered on the right side of the MenuItem. | ||
* @param {boolean} props.disableHover - If set to true, no hover effects are applied. | ||
* @param {boolean} props.active - If set to true, the 'active' styling is applied. | ||
* @param {string} props.className - Additional className for custom styling. | ||
* @param {Object} props.otherProps - Spread into the element as additional HTML attributes. | ||
* @returns {React.ReactElement} A React Element of type 'a' or 'button' based on the 'as' prop. | ||
*/ | ||
export const MenuItem = ({ | ||
as = "a", | ||
leftComponent, | ||
children, | ||
rightComponent, | ||
disableHover, | ||
active, | ||
className, | ||
...props | ||
}: PropsWithChildren<TMenuItem>) => { | ||
const Tag = as; | ||
|
||
return ( | ||
<Tag | ||
className={clsx( | ||
"deriv-menu-item", | ||
{ "deriv-menu-item--active": active || disableHover }, | ||
className, | ||
)} | ||
{...props} | ||
> | ||
{leftComponent} | ||
{children} | ||
{rightComponent} | ||
</Tag> | ||
); | ||
}; | ||
|
||
MenuItem.displayName = "MenuItem"; |
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,63 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { MenuItem } from "../Header/MenuItem"; | ||
|
||
describe("MenuItem Component", () => { | ||
it('renders as a button when "as" prop is "button"', () => { | ||
render( | ||
<MenuItem as="button" onClick={jest.fn()}> | ||
Click me | ||
</MenuItem>, | ||
); | ||
const button = screen.getByRole("button", { name: "Click me" }); | ||
expect(button).toBeInTheDocument(); | ||
expect(button.tagName).toBe("BUTTON"); | ||
}); | ||
|
||
it('renders as a link when "as" prop is "a" and href is provided', () => { | ||
const href = "https://example.com"; | ||
render( | ||
<MenuItem as="a" href={href}> | ||
Visit Example | ||
</MenuItem>, | ||
); | ||
const link = screen.getByRole("link", { name: "Visit Example" }); | ||
expect(link).toBeInTheDocument(); | ||
expect(link.tagName).toBe("A"); | ||
expect(link).toHaveAttribute("href", href); | ||
}); | ||
|
||
it("applies active and disableHover classes correctly", () => { | ||
render( | ||
<MenuItem as="button" active disableHover className="custom-class"> | ||
Click me | ||
</MenuItem>, | ||
); | ||
const button = screen.getByRole("button", { name: "Click me" }); | ||
expect(button).toHaveClass("deriv-menu-item"); | ||
expect(button).toHaveClass("deriv-menu-item--active"); | ||
expect(button).toHaveClass("custom-class"); | ||
}); | ||
|
||
it("handles onClick event for button", async () => { | ||
const mockClick = jest.fn(); | ||
render( | ||
<MenuItem as="button" onClick={mockClick}> | ||
Click me | ||
</MenuItem>, | ||
); | ||
const button = screen.getByRole("button", { name: "Click me" }); | ||
await userEvent.click(button); | ||
expect(mockClick).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("does not break when missing optional props", () => { | ||
render( | ||
<MenuItem as="button" onClick={jest.fn()}> | ||
Click me | ||
</MenuItem>, | ||
); | ||
const button = screen.getByRole("button", { name: "Click me" }); | ||
expect(button).toBeInTheDocument(); | ||
}); | ||
}); |
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,36 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { Header } from "../src/main"; | ||
import { LegacyAdsIcon, LegacyWhatsappIcon } from "@deriv/quill-icons"; | ||
|
||
const meta = { | ||
title: "Components/MenuItem", | ||
component: Header.MenuItem, | ||
args: { | ||
as: "a", | ||
leftComponent: <LegacyWhatsappIcon width={16} height={16} />, | ||
rightComponent: <LegacyAdsIcon width={16} height={16} />, | ||
disableHover: false, | ||
active: true, | ||
}, | ||
argTypes: { | ||
as: { control: false }, | ||
leftComponent: { control: false }, | ||
rightComponent: { control: false }, | ||
disableHover: { control: false }, | ||
active: { control: false }, | ||
}, | ||
parameters: { layout: "centered" }, | ||
tags: ["autodocs"], | ||
} satisfies Meta<typeof Header.MenuItem>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
name: "Header.MenuItem", | ||
render: (args) => ( | ||
<Header.MenuItem {...args}> | ||
<span style={{ margin: "0 10px" }}>Menu Item</span> | ||
</Header.MenuItem> | ||
), | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this?
because without any parents this won't be applied on the element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @shayan-deriv it's better to be there cause we are using menuItem in the div and it definitely have a parent so we need this.