-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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:- added elipsis and tooltip to the button content in table widget component #36865
Changes from 3 commits
9b27dfc
48d92df
92a26c9
8ffde6e
8f9d8d8
4798350
fa303f9
36bfa53
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 |
---|---|---|
|
@@ -36,45 +36,63 @@ const MAX_WIDTH = 500; | |
const TOOLTIP_OPEN_DELAY = 500; | ||
const MAX_CHARS_ALLOWED_IN_TOOLTIP = 200; | ||
|
||
function useToolTip(children: React.ReactNode, title?: string) { | ||
function isButtonTextTruncated(element: HTMLElement) { | ||
const spanElement = element.querySelector("span"); | ||
const offsetWidth = spanElement?.offsetWidth ?? 0; | ||
const scrollWidth = spanElement?.scrollWidth ?? 0; | ||
|
||
return scrollWidth > offsetWidth; | ||
} | ||
|
||
function useToolTip( | ||
children: React.ReactNode, | ||
title?: string, | ||
isButton?: boolean, | ||
) { | ||
const ref = createRef<HTMLDivElement>(); | ||
const [requiresTooltip, setRequiresTooltip] = useState(false); | ||
|
||
useEffect(() => { | ||
let timeout: ReturnType<typeof setTimeout>; | ||
const currentRef = ref.current; | ||
|
||
const mouseEnterHandler = () => { | ||
const element = ref.current?.querySelector("div") as HTMLDivElement; | ||
if (!currentRef) return; | ||
|
||
/* | ||
* Using setTimeout to simulate hoverOpenDelay of the tooltip | ||
* during initial render | ||
*/ | ||
const mouseEnterHandler = () => { | ||
timeout = setTimeout(() => { | ||
const element = currentRef?.querySelector("div") as HTMLDivElement; | ||
|
||
/* | ||
* Using setTimeout to simulate hoverOpenDelay of the tooltip | ||
* during initial render | ||
*/ | ||
if (element && element.offsetWidth < element.scrollWidth) { | ||
setRequiresTooltip(true); | ||
} else if (isButton && element && isButtonTextTruncated(element)) { | ||
setRequiresTooltip(true); | ||
} else { | ||
setRequiresTooltip(false); | ||
} | ||
|
||
ref.current?.removeEventListener("mouseenter", mouseEnterHandler); | ||
ref.current?.removeEventListener("mouseleave", mouseLeaveHandler); | ||
currentRef?.removeEventListener("mouseenter", mouseEnterHandler); | ||
currentRef?.removeEventListener("mouseleave", mouseLeaveHandler); | ||
}, TOOLTIP_OPEN_DELAY); | ||
}; | ||
|
||
const mouseLeaveHandler = () => { | ||
setRequiresTooltip(false); | ||
clearTimeout(timeout); | ||
}; | ||
|
||
ref.current?.addEventListener("mouseenter", mouseEnterHandler); | ||
ref.current?.addEventListener("mouseleave", mouseLeaveHandler); | ||
currentRef?.addEventListener("mouseenter", mouseEnterHandler); | ||
currentRef?.addEventListener("mouseleave", mouseLeaveHandler); | ||
|
||
return () => { | ||
ref.current?.removeEventListener("mouseenter", mouseEnterHandler); | ||
ref.current?.removeEventListener("mouseleave", mouseLeaveHandler); | ||
currentRef?.removeEventListener("mouseenter", mouseEnterHandler); | ||
currentRef?.removeEventListener("mouseleave", mouseLeaveHandler); | ||
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. Include Since Apply this diff to update the dependency array: }, [children]);
+ }, [children, isButton]);
|
||
clearTimeout(timeout); | ||
}; | ||
}, [children]); | ||
}, [children,isButton]); | ||
|
||
return requiresTooltip && children ? ( | ||
<Tooltip | ||
|
@@ -165,6 +183,11 @@ function AutoToolTipComponent(props: Props) { | |
return <LinkWrapper {...props} />; | ||
} | ||
|
||
if (props.columnType === ColumnTypes.BUTTON && props.title) { | ||
const buttonContent = useToolTip(props.children, props.title, true); | ||
return buttonContent; | ||
} | ||
|
||
return ( | ||
<ColumnWrapper className={props.className} textColor={props.textColor}> | ||
<CellWrapper | ||
|
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.
Check for null spanElement before accessing properties
If spanElement is null, comparison on line 44 will fail.