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

TD - 3284 Nowrap Typography truncted tooltip #1032

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions src/Card/DetailCard/DetailCard.tsx
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the changes here? Typescript should already be able to infer the theme type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case we were getting an explicit TypeScript error about theme having an implicit 'any' type. So I added it to resolve the TypeScript error we were seeing. Let me know if you'd prefer a different approach to handle this.

Copy link
Contributor

Choose a reason for hiding this comment

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

So if the types are broken, I suggest that we investigate why the types might be broken rather than trying to override the types in the places that they show that they are broken.

I've done some digging and I think the issue relates to that discussed in this blog where we are trying to use forwardRef with a generic component.

I've gone ahead and tried to fix this for you. I suggest that you test this in VIRTO.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, { Fragment, useEffect, useRef, useState } from "react";
import LabelChipGroup from "../../LabelSelector/LabelChipGroup/LabelChipGroup";
import type { LabelChipGroupProps } from "../../LabelSelector/LabelChipGroup/LabelChipGroup.types";
import { ResizeObserver } from "@juggle/resize-observer";
import { Theme } from "@mui/material/styles";
import TruncatedTooltip from "../../TruncatedTooltip/TruncatedTooltip";

// TODO: add tests in browser once we are done with the migration to cypress. The old tests live in a txt file in this folder until then.
Expand Down Expand Up @@ -142,7 +143,7 @@ function DetailCardHeader({
<Box sx={{ width: headerContentWidth }}>
<TruncatedTooltip
component={Typography}
sx={theme => ({
sx={(theme: Theme) => ({
color: "black",
fontSize: 20,
fontWeight: 700,
Expand All @@ -155,7 +156,7 @@ function DetailCardHeader({
</TruncatedTooltip>
<TruncatedTooltip
component={Typography}
sx={theme => ({
sx={(theme: Theme) => ({
color: theme.palette.text.secondary,
fontSize: 14,
fontWeight: 400
Expand Down
3 changes: 1 addition & 2 deletions src/TruncatedTooltip/TruncatedTooltip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { Box, Link as MuiLink, Typography } from "@mui/material";
import { Meta, StoryFn, StoryObj } from "@storybook/react";
import React from "react";
import TruncatedTooltip from "./TruncatedTooltip";
import { TruncatedTooltipProps } from "./TruncatedTooltip.types";

export default {
component: TruncatedTooltip,
title: "General/TruncatedTooltip"
} satisfies Meta<typeof TruncatedTooltip>;

const Template: StoryFn<TruncatedTooltipProps> = args => {
const Template: StoryFn<typeof TruncatedTooltip> = args => {
return (
<Box
sx={{
Expand Down
39 changes: 18 additions & 21 deletions src/TruncatedTooltip/TruncatedTooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import { Box, Tooltip } from "@mui/material";
import React, {
Children,
ReactElement,
isValidElement,
useRef,
useState
} from "react";
import React, { Children, ReactElement, isValidElement, useState } from "react";

import { TruncatedTooltipProps } from "./TruncatedTooltip.types";

Expand All @@ -14,18 +8,21 @@ import { TruncatedTooltipProps } from "./TruncatedTooltip.types";
* Automatically grabs the tooltip from child.
* Renders a specified MUI component or element otherwise wraps string in a span.
*/
const TruncatedTooltip = <T extends React.ElementType = "span">({
children,
component,
multiline,
sx,
tooltip,
alwaysShowTooltip = false,
TooltipProps = undefined,
...rest
}: TruncatedTooltipProps<T>) => {
// Ref to the text element.
const textElementRef = useRef<HTMLInputElement | null>(null);
const TruncatedTooltip = React.forwardRef(function TruncateTooltip<
T extends React.ElementType = "span"
>(
{
children,
component,
multiline,
sx,
tooltip,
alwaysShowTooltip = false,
TooltipProps = undefined,
...rest
}: TruncatedTooltipProps<T>,
ref: React.Ref<T>
) {
// State to determine if the tooltip should show.
const [open, setOpen] = useState(false);

Expand Down Expand Up @@ -96,7 +93,7 @@ const TruncatedTooltip = <T extends React.ElementType = "span">({
>
<Box
component={component || "span"}
ref={textElementRef}
ref={ref}
sx={[
{
"& > *": {
Expand Down Expand Up @@ -127,6 +124,6 @@ const TruncatedTooltip = <T extends React.ElementType = "span">({
</Box>
</Tooltip>
);
};
});

export default TruncatedTooltip;