generated from cloudoperators/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(ui): migrate MenuSection to TypeScript * feat(ui): add more tests * chore(ui): migrate Menu, MenuItem, MenuSection components to TypeScript * chore(ui): prevent headless-ui error * chore(ui): fix button rendering issue detected in story and other tiny adjustments * chore(ui): make tests for rendered element more explicit * Create five-boats-cheat.md * chore(ui): remove type casting when using context don’t cast type when using MenuContext as per Vova’s suggestion * chore(ui): re-introduce changed logic to determine MenuItem element non-intentionally removed this chnage by Guoda * Update .changeset/five-boats-cheat.md Co-authored-by: Guoda <[email protected]> * chore(ui): consistency Co-authored-by: Guoda <[email protected]> * chore(ui): consistency Co-authored-by: Guoda <[email protected]> * chore(ui): use React element type Co-authored-by: Guoda <[email protected]> * chore(ui): make sure href is only passed when element is anchor Co-authored-by: Guoda <[email protected]> * chore(ui): use React.FC to fix typecheck * chore(ui): code formatting * chore(ui): remove file extensions from relevant imports * chore(ui): add more tests * chore(ui): set onClick to null explicitly for standalone stories * chore(ui): remove unnecessary underscores * chore(ui): add more children to story * chore(ui): add back children check to fix link/button issue * chore(ui): remove underscore, eslint-ignore line * chore(ui): apply interactive styles to interactive elements only * chore(ui): disable `children` control in story --------- Co-authored-by: I531348 <[email protected]> Co-authored-by: Guoda <[email protected]> Co-authored-by: Wowa Barsukov <[email protected]>
- Loading branch information
1 parent
a278544
commit 7cb142d
Showing
19 changed files
with
495 additions
and
412 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@cloudoperators/juno-ui-components": minor | ||
--- | ||
|
||
Migrate Menu, MenuItem and MenuSection components to Typescript (#237) |
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
File renamed without changes.
109 changes: 0 additions & 109 deletions
109
packages/ui-components/src/components/MenuItem/MenuItem.component.js
This file was deleted.
Oops, something went wrong.
112 changes: 112 additions & 0 deletions
112
packages/ui-components/src/components/MenuItem/MenuItem.component.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,112 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Juno contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useContext, MouseEvent, FC } from "react" | ||
import { Menu as HLMenu } from "@headlessui/react" | ||
import { MenuContext } from "../Menu/Menu.component" | ||
import { Icon, KnownIconsEnum } from "../Icon/Icon.component" | ||
|
||
const itemStyles = ` | ||
jn-text-theme-default | ||
jn-flex | ||
jn-items-center | ||
jn-w-full | ||
bg-clip-padding | ||
jn-truncate | ||
jn-text-left | ||
jn-bg-theme-background-lvl-1 | ||
disabled:jn-cursor-not-allowed | ||
data-[headlessui-state="disabled"]:jn-cursor-not-allowed | ||
` | ||
|
||
const smallStyles = ` | ||
jn-text-sm | ||
jn-p-2 | ||
` | ||
|
||
const normalStyles = ` | ||
jn-text-base | ||
jn-pt-[0.6875rem] | ||
jn-pb-[0.5rem] | ||
jn-px-[0.875rem] | ||
` | ||
// Define styles applicable to interactive elements only: | ||
const actionableItemStyles = ` | ||
hover:jn-bg-theme-background-lvl-3 | ||
cursor-pointer | ||
data-[headlessui-state="disabled"]:jn-bg-theme-background-lvl-3 | ||
` | ||
|
||
export interface MenuItemProps { | ||
/** Children of the menu item */ | ||
children?: React.ReactNode | ||
/** Pass a custom className to the menu item */ | ||
className?: string | ||
/** Whether the menu item is disabled */ | ||
disabled?: boolean | ||
/** Pass the name of an icon the button should show. Can be any icon included with Juno. */ | ||
icon?: keyof typeof KnownIconsEnum | ||
/** The label of the menu item. Will take precedence over children passed to the component. */ | ||
label?: string | ||
/** Pass an href to the menu item. Will result in the menu item being rendered as an `<a>`. */ | ||
href?: string | ||
/** Pass an onClick handler to the menu item. Will result in the menu item being rendered as a `<button>`. */ | ||
// eslint-disable-next-line no-unused-vars | ||
onClick?: (event: MouseEvent<HTMLButtonElement>) => void | ||
} | ||
|
||
interface MenuContextType { | ||
variant: "small" | "normal" | ||
} | ||
|
||
/** | ||
A menu item to be used inside Menu. | ||
Can render `<a>`, `<button>`, or `<div>` based on props. | ||
*/ | ||
export const MenuItem: FC<MenuItemProps> = ({ | ||
children = null, | ||
className = "", | ||
disabled = false, | ||
href = "", | ||
icon = null, | ||
label = "", | ||
onClick = undefined, | ||
...props | ||
}) => { | ||
const menuContext = useContext<MenuContextType | undefined>(MenuContext) | ||
const variant = menuContext?.variant ?? "normal" | ||
|
||
// Determine which element to render: | ||
const Element: React.ElementType = href ? "a" : children ? "div" : onClick ? "button" : "div" | ||
|
||
const handleClick = (event: MouseEvent<HTMLButtonElement>) => { | ||
onClick && onClick(event) | ||
} | ||
|
||
return ( | ||
<HLMenu.Item | ||
as={Element} | ||
// conditionally pass a href attribute only if href was supplied to prevent errors from headless-ui internal checks: | ||
{...(href && !disabled && Element === "a" ? { href } : {})} | ||
onClick={onClick && Element === "button" && !disabled ? handleClick : undefined} | ||
disabled={disabled} | ||
className={` | ||
juno-menu-item | ||
juno-menu-item-${Element} | ||
${itemStyles} | ||
${Element !== "div" ? actionableItemStyles : ""} | ||
${variant === "small" ? smallStyles : normalStyles} | ||
${disabled ? "jn-cursor-not-allowed" : ""} | ||
${className} | ||
`} | ||
{...props} | ||
> | ||
<span className={`${disabled ? "jn-opacity-50" : ""}`}> | ||
{icon && <Icon icon={icon} size="18" className="jn-inline-block jn-mr-2" />} | ||
{children || label} | ||
</span> | ||
</HLMenu.Item> | ||
) | ||
} |
Oops, something went wrong.