-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix:- added elipsis and tooltip to the button content in table widget component #36865
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
Changes from 1 commit
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,23 +36,40 @@ 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 = ref.current?.querySelector("div") as HTMLDivElement; | ||
|
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. We can use
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. PS: We have some usages further down below as well, lets see if the variable can be reused. |
||
|
|
||
| /* | ||
| * 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); | ||
| } | ||
|
|
@@ -63,6 +80,7 @@ function useToolTip(children: React.ReactNode, title?: string) { | |
| }; | ||
|
|
||
| const mouseLeaveHandler = () => { | ||
| setRequiresTooltip(false); | ||
| clearTimeout(timeout); | ||
| }; | ||
|
|
||
|
|
@@ -158,13 +176,23 @@ function LinkWrapper(props: Props) { | |
| ); | ||
| } | ||
|
|
||
| function ButtonWrapper(props: Props) { | ||
| const content = useToolTip(props.children, props.title, true); | ||
|
|
||
| return content; | ||
| } | ||
|
|
||
|
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. 🛠️ Refactor suggestion Consider simplifying the Right now,
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 comment makes sense.
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. @rahulbarwal, excellent! I'm glad to hear that the suggestion makes sense. Simplifying the (^‿^)/ |
||
| function AutoToolTipComponent(props: Props) { | ||
| const content = useToolTip(props.children, props.title); | ||
|
|
||
| if (props.columnType === ColumnTypes.URL && props.title) { | ||
| return <LinkWrapper {...props} />; | ||
| } | ||
|
|
||
| if (props.columnType === ColumnTypes.BUTTON && props.title) { | ||
| return <ButtonWrapper {...props} />; | ||
| } | ||
|
|
||
| return ( | ||
| <ColumnWrapper className={props.className} textColor={props.textColor}> | ||
| <CellWrapper | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,12 @@ import React, { useState } from "react"; | |
|
|
||
| import { ActionWrapper } from "../TableStyledWrappers"; | ||
| import { BaseButton } from "widgets/ButtonWidget/component"; | ||
| import type { ButtonColumnActions } from "widgets/TableWidgetV2/constants"; | ||
| import { | ||
| ColumnTypes, | ||
| type ButtonColumnActions, | ||
| } from "widgets/TableWidgetV2/constants"; | ||
| import styled from "styled-components"; | ||
| import AutoToolTipComponent from "widgets/TableWidgetV2/component/cellComponents/AutoToolTipComponent"; | ||
|
|
||
| const StyledButton = styled(BaseButton)<{ | ||
| compactMode?: string; | ||
|
|
@@ -44,21 +48,28 @@ export function Button(props: ButtonProps) { | |
| e.stopPropagation(); | ||
| }} | ||
| > | ||
| {props.isCellVisible && props.action.isVisible ? ( | ||
| <StyledButton | ||
| borderRadius={props.action.borderRadius} | ||
| boxShadow={props.action.boxShadow} | ||
| buttonColor={props.action.backgroundColor} | ||
| buttonVariant={props.action.variant} | ||
| compactMode={props.compactMode} | ||
| disabled={props.isDisabled} | ||
| iconAlign={props.action.iconAlign} | ||
| iconName={props.action.iconName} | ||
| loading={loading} | ||
| onClick={handleClick} | ||
| text={props.action.label} | ||
| /> | ||
| ) : null} | ||
| {props.isCellVisible && props.action.isVisible | ||
| ? props.action.label && ( | ||
|
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 will be rendered if {props.isCellVisible && props.action.isVisible && props.action.label
?
<.../>
: null} |
||
| <AutoToolTipComponent | ||
| columnType={ColumnTypes.BUTTON} | ||
| title={props.action.label} | ||
| > | ||
| <StyledButton | ||
| borderRadius={props.action.borderRadius} | ||
| boxShadow={props.action.boxShadow} | ||
| buttonColor={props.action.backgroundColor} | ||
| buttonVariant={props.action.variant} | ||
| compactMode={props.compactMode} | ||
| disabled={props.isDisabled} | ||
| iconAlign={props.action.iconAlign} | ||
| iconName={props.action.iconName} | ||
| loading={loading} | ||
| onClick={handleClick} | ||
| text={props.action.label} | ||
| /> | ||
| </AutoToolTipComponent> | ||
| ) | ||
| : null} | ||
| </ActionWrapper> | ||
| ); | ||
| } | ||
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.