-
Notifications
You must be signed in to change notification settings - Fork 295
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 #111 from mondaycom/feature/moshe/menu_item_button
Feature/moshe/menu item button
- Loading branch information
Showing
10 changed files
with
519 additions
and
9 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
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,32 @@ | ||
import React from "react"; | ||
import { render, cleanup } from "@testing-library/react"; | ||
import { expect } from "../../../test/test-helpers"; | ||
import MenuItem from "./MenuItem"; | ||
|
||
describe("<MenuItem />", () => { | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
it("should be able to render menu item", () => { | ||
const { container } = render(<MenuItem />); | ||
}); | ||
|
||
it("should be able to render menu item text", () => { | ||
const { container } = render(<MenuItem title="my item title" icon="fa fa-star" />); | ||
const menuItemElement = container.querySelector(".monday-style-menu-item"); | ||
expect(menuItemElement.innerText).to.eq("my item title"); | ||
}); | ||
|
||
describe("render children inside popover element", () => { | ||
it("should be able to render children ", () => { | ||
const { container } = render( | ||
<MenuItem title="my item title" icon="fa fa-star"> | ||
<div className="bla-element">Bla bla bla</div> | ||
</MenuItem> | ||
); | ||
const childElement = container.querySelector(".bla-element"); | ||
expect(childElement.innerText).to.eq("Bla bla bla"); | ||
}); | ||
}); | ||
}); |
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,135 @@ | ||
/* eslint-disable react/jsx-props-no-spreading */ | ||
import React, { useRef } from "react"; | ||
|
||
import PropTypes from "prop-types"; | ||
import cx from "classnames"; | ||
import Button from "../../Button/Button"; | ||
import Tooltip from "../../Tooltip/Tooltip"; | ||
|
||
import useMergeRefs from "../../../hooks/useMergeRefs"; | ||
|
||
import useMenuItemMouseEvents from "../MenuItem/hooks/useMenuItemMouseEvents"; | ||
import useMenuItemKeyboardEvents from "../MenuItem/hooks/useMenuItemKeyboardEvents"; | ||
|
||
import "./MenuItemButton.scss"; | ||
|
||
const MenuItemButton = ({ | ||
classname, | ||
kind, | ||
leftIcon, | ||
rightIcon, | ||
disabled, | ||
disableReason, | ||
index, | ||
activeItemIndex, | ||
onClick, | ||
menuId, | ||
tooltipPosition, | ||
tooltipShowDelay, | ||
children, | ||
resetOpenSubMenuIndex, | ||
setSubMenuIsOpenByIndex, | ||
setActiveItemIndex, | ||
menuRef, | ||
closeMenu, | ||
useDocumentEventListeners | ||
}) => { | ||
const ref = useRef(null); | ||
const referenceElementRef = useRef(null); | ||
|
||
const mergedRef = useMergeRefs({ refs: [ref, referenceElementRef] }); | ||
|
||
const shouldShowTooltip = disabled && disableReason; | ||
const tooltipContent = disableReason; | ||
|
||
const isActive = activeItemIndex === index; | ||
|
||
const isMouseEnter = useMenuItemMouseEvents( | ||
ref, | ||
resetOpenSubMenuIndex, | ||
setSubMenuIsOpenByIndex, | ||
isActive, | ||
setActiveItemIndex, | ||
index, | ||
false | ||
); | ||
|
||
const { onClickCallback } = useMenuItemKeyboardEvents( | ||
onClick, | ||
disabled, | ||
isActive, | ||
index, | ||
setActiveItemIndex, | ||
false, | ||
false, | ||
setSubMenuIsOpenByIndex, | ||
menuRef, | ||
isMouseEnter, | ||
closeMenu, | ||
useDocumentEventListeners | ||
); | ||
|
||
return ( | ||
<Tooltip | ||
content={shouldShowTooltip ? tooltipContent : null} | ||
position={tooltipPosition} | ||
showDelay={tooltipShowDelay} | ||
> | ||
<li | ||
id={`${menuId}-${index}`} | ||
className={cx("monday-style-menu-item-button", classname)} | ||
ref={mergedRef} | ||
role="menuitem" | ||
aria-current={isActive} | ||
> | ||
<Button | ||
active={isActive} | ||
disabled={disabled} | ||
leftIcon={leftIcon} | ||
rightIcon={rightIcon} | ||
onClick={onClickCallback} | ||
kind={kind} | ||
size={Button.sizes.SMALL} | ||
blurOnMouseUp={false} | ||
> | ||
<div className="menu-item-button-content">{children}</div> | ||
</Button> | ||
</li> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
MenuItemButton.kinds = Button.kinds; | ||
|
||
MenuItemButton.defaultProps = { | ||
classname: "", | ||
kind: MenuItemButton.kinds.PRIMARY, | ||
leftIcon: null, | ||
rightIcon: null, | ||
index: undefined, | ||
activeItemIndex: -1, | ||
disabled: false, | ||
disableReason: undefined, | ||
onClick: undefined, | ||
tooltipPosition: "right", | ||
tooltipShowDelay: 300 | ||
}; | ||
|
||
MenuItemButton.propTypes = { | ||
classname: PropTypes.string, | ||
kind: PropTypes.oneOf([MenuItemButton.kinds.PRIMARY, MenuItemButton.kinds.SECONDARY, MenuItemButton.kinds.TERTIARY]), | ||
leftIcon: PropTypes.string, | ||
rightIcon: PropTypes.string, | ||
index: PropTypes.number, | ||
activeItemIndex: PropTypes.number, | ||
disabled: PropTypes.bool, | ||
disableReason: PropTypes.string, | ||
onClick: PropTypes.func, | ||
tooltipPosition: PropTypes.oneOf("right", "left", "top", "bottom"), | ||
tooltipShowDelay: PropTypes.number | ||
}; | ||
|
||
MenuItemButton.isSelectable = true; | ||
MenuItemButton.isMenuChild = true; | ||
|
||
export default MenuItemButton; |
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,21 @@ | ||
@import "../../../styles/themes.scss"; | ||
@import "../../../styles/typography.scss"; | ||
|
||
.monday-style-menu-item-button { | ||
margin: unset; | ||
@include font-default; | ||
display: flex; | ||
flex-direction: row; | ||
padding: $spacing-xs 0; | ||
align-items: center; | ||
|
||
.monday-style-button { | ||
width: 100%; | ||
|
||
.menu-item-button-content { | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
} | ||
} | ||
} |
Oops, something went wrong.