Skip to content
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

fix(headercell): added new hover prop to control component hover state #928

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export default {
control: { type: "select" },
options: ["left", "center", "right"],
},
hover: {
control: { type: "boolean" },
},
shouldShowTooltipOnHover: {
control: { type: "boolean" },
},
Expand All @@ -44,6 +47,7 @@ export const Default = {
active: false,
direction: "desc",
hideSortIcon: false,
hover: false,
shouldShowTooltipOnHover: true,
tooltipProps: { sdsStyle: "dark" },
tooltipText: "This is a header cell",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Args } from "@storybook/types";
import RawCellHeader from "src/core/CellHeader";

export const TestDemo = (): JSX.Element => (
export const TestDemo = (props: Args): JSX.Element => (
<table>
<tbody>
<tr>
Expand All @@ -10,6 +11,8 @@ export const TestDemo = (): JSX.Element => (
shouldShowTooltipOnHover
active
tooltipText="testTooltipTitle"
hover
{...props}
>
Header
</RawCellHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const CellBasicNameSpaceTest = (props: CellHeaderProps) => {
direction="asc"
active
hideSortIcon
hover
horizontalAlign="center"
shouldShowTooltipOnHover
tooltipProps={{ sdsStyle: "light" }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,15 @@ exports[`<CellHeader /> Default story renders snapshot 1`] = `
<tbody>
<tr>
<th
class="css-bony3j"
data-mui-internal-clone-element="true"
class="css-ezzkc2"
direction="desc"
tabindex="0"
>
<div
class="css-rm9mvq"
>
<span>
Header
</span>
<div
class="css-1ft6wgl"
>
<svg
aria-hidden="true"
class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-11x9hfs-MuiSvgIcon-root"
data-file-name="IconChevronDownSmall"
data-testid="IconChevronDownSmall"
fillcontrast="white"
focusable="false"
height="16"
viewBox="0 0 16 16"
width="16"
/>
</div>
</div>
</th>
</tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("<CellHeader />", () => {
});

it("renders tooltip on hover", async () => {
render(<Test />);
render(<Test hover={true} />);
const headerCellElement = screen.getByTestId("CellHeader");
fireEvent.mouseOver(headerCellElement);
await screen.findByText("testTooltipTitle");
Expand All @@ -29,8 +29,8 @@ describe("<CellHeader />", () => {
expect(style.textAlign).toBe("right");
});

it("renders a sort icon when header is active", async () => {
render(<Test />);
it("renders a sort icon when header is active and hover is true", async () => {
render(<Test hover={true} />);
const headerCellElement = screen.getByTestId("CellHeader");
const sortIcon =
headerCellElement.getElementsByClassName("MuiSvgIcon-root")[0];
Expand Down
19 changes: 13 additions & 6 deletions packages/components/src/core/CellHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface CellHeaderContentProps {
hideSortIcon?: boolean;
horizontalAlign?: "left" | "center" | "right";
children: React.ReactNode;
hover?: boolean;
}

interface CellHeaderRawProps
Expand All @@ -39,6 +40,7 @@ const CellHeaderContent = (
direction = "desc",
hideSortIcon = false,
horizontalAlign,
hover,
} = props;

const sdsIconName: keyof IconNameToSizes =
Expand All @@ -61,7 +63,7 @@ const CellHeaderContent = (
return (
<StyledCellHeaderContainer horizontalAlign={horizontalAlign}>
<span>{children}</span>
{(!hideSortIcon || active) && sortIcon}
{(!hideSortIcon || active) && hover && sortIcon}
</StyledCellHeaderContainer>
);
};
Expand All @@ -74,10 +76,11 @@ const CellHeader = forwardRef<HTMLTableCellElement, CellHeaderProps>(
tooltipProps,
tooltipText = "",
tooltipSubtitle,
hover = false,
...rest
} = props;

if (shouldShowTooltipOnHover) {
if (shouldShowTooltipOnHover && hover) {
return (
<Tooltip
arrow
Expand All @@ -87,15 +90,19 @@ const CellHeader = forwardRef<HTMLTableCellElement, CellHeaderProps>(
title={tooltipText}
{...tooltipProps}
>
<StyledTableHeader ref={ref} {...rest}>
<CellHeaderContent {...props}>{children}</CellHeaderContent>
<StyledTableHeader ref={ref} hover={hover} {...rest}>
<CellHeaderContent {...props} hover={hover}>
{children}
</CellHeaderContent>
</StyledTableHeader>
</Tooltip>
);
}
return (
<StyledTableHeader ref={ref} {...rest}>
<CellHeaderContent {...props}>{children}</CellHeaderContent>
<StyledTableHeader ref={ref} hover={hover} {...rest}>
<CellHeaderContent hover={hover} {...props}>
{children}
</CellHeaderContent>
</StyledTableHeader>
);
}
Expand Down
20 changes: 15 additions & 5 deletions packages/components/src/core/CellHeader/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface CellHeaderExtraProps extends CommonThemeProps {
active?: boolean;
hideSortIcon?: boolean;
horizontalAlign?: "left" | "center" | "right";
hover?: boolean;
}

const contentPositionMapping = {
Expand All @@ -27,6 +28,7 @@ const doNotForwardProps = [
"tooltipProps",
"tooltipText",
"hideSortIcon",
"hover",
];

export const StyledSortingIcon = styled(Icon, {
Expand Down Expand Up @@ -55,28 +57,36 @@ export const StyledTableHeader = styled("th", {
${focusVisibleA11yStyle}

${(props: CellHeaderExtraProps) => {
const { active = false, horizontalAlign = "left" } = props;
const { active = false, horizontalAlign = "left", hover = true } = props;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it defaults to "true" 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it ok? or should it be the other way around? 🤔
What do you think @clarsen-czi?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clarsen-czi sorry I think we still need your feedback here 😁 🙏

If we default hover to true, it would mean that the CellHeader could have hover effect when no corresponding action is present/implemented, so the question here is whether we should default hover to false instead to prevent that?

Thank you!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call @tihuan! I agree that having it default to false makes the most sense. Thanks for the suggestion!


const spaces = getSpaces(props);
const semanticColors = getSemanticColors(props);

const defaultColor = active
? semanticColors?.accent?.textAction
: semanticColors?.base?.textSecondary;

const hoverColor = active
? semanticColors?.accent?.textActionHover
: semanticColors?.base?.textPrimary;

return `
color: ${active ? semanticColors?.accent?.textAction : semanticColors?.base?.textSecondary};
color: ${defaultColor};
padding: ${spaces?.l}px ${spaces?.m}px;
text-align: ${horizontalAlign};
min-width: 96px;
cursor: pointer;
cursor: ${hover ? "pointer" : "default"};
vertical-align: bottom;

& .MuiButtonBase-root {
outline: none;
}

&:hover {
color: ${active ? semanticColors?.accent?.textActionHover : semanticColors?.base?.textPrimary};
color: ${hover ? hoverColor : defaultColor};

& .MuiButtonBase-root {
color: ${active ? semanticColors?.accent?.textActionHover : semanticColors?.base?.textPrimary};
color: ${hoverColor};
opacity: 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ export const Table = (props: Args): JSX.Element => {
<CellHeader horizontalAlign="center" hideSortIcon>
Category
</CellHeader>
<CellHeader active>Active Header</CellHeader>
<CellHeader>
<CellHeader active hover>
Active Header
</CellHeader>
<CellHeader hover>
A very long table header title to test sort icon positioning
</CellHeader>
<CellHeader hideSortIcon>Component</CellHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exports[`<Table /> Default story renders snapshot 1`] = `
class="css-fitsu2"
>
<th
class="css-13v2ecb"
class="css-t4gp9z"
>
<div
class="css-187gjam"
Expand Down Expand Up @@ -74,7 +74,7 @@ exports[`<Table /> Default story renders snapshot 1`] = `
</div>
</th>
<th
class="css-bony3j"
class="css-ezzkc2"
>
<div
class="css-rm9mvq"
Expand All @@ -85,7 +85,7 @@ exports[`<Table /> Default story renders snapshot 1`] = `
</div>
</th>
<th
class="css-148k9j7"
class="css-1scpxyh"
>
<div
class="css-wg04tn"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ export const TableHeader = (props: Args): JSX.Element => {
return (
<table>
<RawTableHeader {...props}>
<CellHeader active>Column 1</CellHeader>
<CellHeader>Column 2</CellHeader>
<CellHeader>Column 3</CellHeader>
<CellHeader active hover>
Column 1
</CellHeader>
<CellHeader hover>Column 2</CellHeader>
<CellHeader hover>Column 3</CellHeader>
</RawTableHeader>
</table>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import RawTableHeader from "src/core/TableHeader";
export const TestDemo = (): JSX.Element => (
<table>
<RawTableHeader data-testid="TableHeader">
<CellHeader active>Column 1</CellHeader>
<CellHeader>Column 2</CellHeader>
<CellHeader>Column 3</CellHeader>
<CellHeader active hover>
Column 1
</CellHeader>
<CellHeader hover>Column 2</CellHeader>
<CellHeader hover>Column 3</CellHeader>
</RawTableHeader>
</table>
);