Repository navigation
fix(Card): items support for action #245
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
base: main
Are you sure you want to change the base?
Changes from all commits
b62bf8b
03af508
1cd0432
0bba0a2
b98095b
e462ed5
0eed694
b109324
e4c5af0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,5 +1,5 @@ | ||||||
| import Card from "@/components/Card/Card"; | ||||||
| import { render, screen } from "@testing-library/react"; | ||||||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||||||
| import Button from "@/components/Button"; | ||||||
| import Link from "@/components/Link"; | ||||||
| import IconButton from "@/components/IconButton"; | ||||||
|
|
@@ -141,6 +141,25 @@ describe("Card", () => { | |||||
| expect(onClick).toHaveBeenCalled(); | ||||||
| }); | ||||||
|
|
||||||
| it("should open a dropdown with the given items when the action has items and close it after an item is clicked", () => { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the outside click? close on escape (In fact if you did not activate this, please activate in the code) |
||||||
| const onClick = jest.fn(); | ||||||
| render(<Card action={{ icon: "more_vert", items: [{ header: "Account" }, { label: "Edit", onClick }] }} />); | ||||||
| expect(screen.queryByTestId("cardDropdown")).not.toBeInTheDocument(); | ||||||
| fireEvent.click(screen.getByText("more_vert")); | ||||||
| expect(screen.getByText("Account")).toBeInTheDocument(); | ||||||
| fireEvent.click(screen.getByText("Edit")); | ||||||
| expect(onClick).toHaveBeenCalledTimes(1); | ||||||
| expect(screen.queryByTestId("cardDropdown")).not.toBeInTheDocument(); | ||||||
| }); | ||||||
|
|
||||||
| it("should not run the click of a disabled dropdown item", () => { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const onClick = jest.fn(); | ||||||
| render(<Card action={{ icon: "more_vert", items: [{ label: "Edit", onClick, disabled: true }] }} />); | ||||||
| fireEvent.click(screen.getByText("more_vert")); | ||||||
| fireEvent.click(screen.getByText("Edit")); | ||||||
| expect(onClick).not.toHaveBeenCalled(); | ||||||
| }); | ||||||
|
|
||||||
| it("should render an image in the content part when contentImage is set as a url or path", () => { | ||||||
| const path = "/imagePath"; | ||||||
| render(<Card contentImage={path} />); | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| "use client"; | ||
|
hatesalp marked this conversation as resolved.
Resolved
|
||
|
|
||
| import { useRef } from "react"; | ||
| import { createPortal } from "react-dom"; | ||
| import styles from "../Card.module.scss"; | ||
| import Avatar from "@/components/Avatar"; | ||
| import IconButton from "@/components/IconButton"; | ||
|
|
@@ -6,7 +10,8 @@ import type { MouseEvent } from "react"; | |
| import type { IconGlobalType, Variant } from "../../../types"; | ||
| import ImageView from "@/components/ImageView"; | ||
| import GlobalIconWrapper from "../../Motif/GlobalIconWrapper/GlobalIconWrapper"; | ||
| import type { CardVariant } from "../types"; | ||
| import useAnchoredOverlay from "../../../hooks/useAnchoredOverlay"; | ||
| import type { CardDropdownItem, CardHeaderAction, CardVariant } from "../types"; | ||
|
|
||
| type Props = { | ||
| icon?: IconGlobalType; | ||
|
|
@@ -17,18 +22,44 @@ type Props = { | |
| subtitle?: string; | ||
| image?: string; | ||
| imagePosition?: "left" | "right"; | ||
| action?: { icon: IconGlobalType; onClick: (event: MouseEvent<HTMLButtonElement>) => void }; | ||
| action?: CardHeaderAction; | ||
| variant: CardVariant; | ||
| }; | ||
|
|
||
| const CardHeader = (props: Props) => { | ||
| const { icon, iconVariant, avatarIcon, avatarText, action, image, imagePosition, subtitle, title, variant } = props; | ||
| const menuItems = action?.items; | ||
|
|
||
| const actionRef = useRef<HTMLButtonElement>(null); | ||
| const { | ||
| attached, | ||
| hide, | ||
| style: menuStyle, | ||
| placement, | ||
| overlayRef: menuRef, | ||
| insideProps, | ||
| } = useAnchoredOverlay({ | ||
| anchorRef: actionRef, | ||
| placement: "bottomRight", | ||
| toggleOnAnchorClick: !!menuItems, | ||
| tabBridge: true, | ||
| closeOnOutsideClick: true, | ||
| closeOnEscape: true, | ||
| inactive: !menuItems, | ||
| }); | ||
|
|
||
| const isVisible = title || subtitle || avatarText || avatarIcon || icon || image || action; | ||
| const iconButtonVariant = variant === "neutral" || variant === "secondary" ? variant : variant === "warning" ? "strong" : "negative"; | ||
| const headerClassName = sanitizeModuleClasses(styles, "header", image && imagePosition === "right" && "headerImageAlignRight"); | ||
| const headerIconClassName = sanitizeModuleClasses(styles, "headerIcon", iconVariant && `headerIcon-${iconVariant}`); | ||
|
|
||
| const menuClassNames = sanitizeModuleClasses(styles, "dropdown", placement.startsWith("top") ? "top" : "bottom"); | ||
|
|
||
| const handleItemClick = (item: CardDropdownItem) => (event: MouseEvent<HTMLButtonElement>) => { | ||
| item.onClick?.(event); | ||
| hide(); | ||
| }; | ||
|
|
||
| const maybeContent = title || subtitle || avatarText || avatarIcon || icon || action; | ||
|
|
||
| return ( | ||
|
|
@@ -46,9 +77,30 @@ const CardHeader = (props: Props) => { | |
| {subtitle && <span className={styles.headerSubtitle}>{subtitle}</span>} | ||
| </div> | ||
| )} | ||
| {action && <IconButton name={action.icon} onClick={action.onClick} size="xxl" variant={iconButtonVariant} />} | ||
| {action && <IconButton ref={actionRef} name={action.icon} onClick={action.onClick} size="xxl" variant={iconButtonVariant} />} | ||
| </div> | ||
| )} | ||
| {menuItems && | ||
| attached && | ||
| createPortal( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This menu duplicates Dropdown's (overlay, items, caret styles). Could we extract Dropdown's menu into a DropdownMenu that takes an anchorRef, and use it here with the IconButton? We can talk it over with the team.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I implemented this dropdown actions as you mentioned in here at first actually, but Card component became so highly-coupled to Dropdown and its module, so custom dropdown behaved structure is implemented in here. We talked with @aktasmehmet, but he didn't see the code yet so maybe his opinion will be same with yours. Let's discuss this in here. @motif-ui/core-team |
||
| <div className={menuClassNames} style={menuStyle} ref={menuRef} {...insideProps} data-testid="cardDropdown"> | ||
| <ul className={styles.dropdownList}> | ||
| {menuItems.map((item, index) => ( | ||
| <li key={index}> | ||
| {item.header ? ( | ||
| <span className={styles.dropdownHeader}>{item.header}</span> | ||
| ) : ( | ||
| <button type="button" className={styles.dropdownItem} disabled={item.disabled} onClick={handleItemClick(item)}> | ||
| {item.icon && <GlobalIconWrapper icon={item.icon} style={{ color: item.iconColor }} />} | ||
| {item.label && <span>{item.label}</span>} | ||
| </button> | ||
| )} | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div>, | ||
|
Comment on lines
+86
to
+101
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be a seperate component
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, I would create a hook here which also handles all the overlay stuff inside and return this part as component and actionRef to use inside ref of the icon button. This way, it will be much cleaner. |
||
| document.body, | ||
| )} | ||
| </div> | ||
| ) | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,20 @@ import { ButtonProps } from "../Button/types"; | |
| import { LinkProps } from "../Link/types"; | ||
| import { IconButtonProps } from "../IconButton/types"; | ||
|
|
||
| export type CardDropdownItem = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This matches
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we decide to keep separate custom Dropdown structure, we shouldn't create one type for this case. On the other hand, if we decide to use Dropdown this type will be removed anyway. |
||
| label?: string; | ||
| icon?: IconGlobalType; | ||
| iconColor?: string; | ||
| header?: string; | ||
|
Comment on lines
+10
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These 2 props are not necessary right now. They make it more complicated (like usage of header and label, icon, disabled, etc. together.). IconColor is a completely different thing. We dont provide such deep styling prop. So, please remove both.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And dont forget the jsdoc comment for this type below π |
||
| onClick?: (event: MouseEvent<HTMLButtonElement>) => void; | ||
| disabled?: boolean; | ||
| }; | ||
|
|
||
| /** The action at the top right: an icon button that runs onClick, or opens a dropdown with the given items */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for the comment |
||
| export type CardHeaderAction = { icon: IconGlobalType } & ( | ||
| { onClick: (event: MouseEvent<HTMLButtonElement>) => void; items?: never } | { items: CardDropdownItem[]; onClick?: never } | ||
| ); | ||
|
Comment on lines
+17
to
+19
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you prevent user having an onClick event when there are items? It might be possible for user to execute a code block when the dropdown opens. So, I suggest: and please move this right before CardVariant. It looks like the main type of the component (which is not) at the very top. |
||
|
|
||
| export type CardActionButtonPosition = "left" | "center" | "right" | "spaceBetween" | "stretch"; | ||
|
|
||
| export type CardProps = { | ||
|
|
@@ -14,7 +28,24 @@ export type CardProps = { | |
| iconVariant?: Variant; | ||
| avatarIcon?: IconGlobalType; | ||
| image?: string; | ||
| action?: { icon: IconGlobalType; onClick: (event: MouseEvent<HTMLButtonElement>) => void }; | ||
| /** | ||
| * Runs `onClick`, or opens a dropdown with the given `items`. | ||
| * ``` | ||
| * { | ||
| * icon: IconGlobalType; | ||
| * onClick?: (event: MouseEvent<HTMLButtonElement>) => void; | ||
| * items?: { | ||
| * label?: string; | ||
| * icon?: IconGlobalType; | ||
| * iconColor?: string; | ||
| * header?: string; | ||
| * onClick?: (event: MouseEvent<HTMLButtonElement>) => void; | ||
| * disabled?: boolean; | ||
| * }[]; | ||
| * } | ||
| * ``` | ||
| */ | ||
| action?: CardHeaderAction; | ||
|
hatesalp marked this conversation as resolved.
Resolved
|
||
| contentTitle?: string; | ||
| contentSubtitle?: string; | ||
| contentText?: string; | ||
|
|
||
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.
I'll check this file seperately after other threads are resolved