-
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
Merged
yatinappsmith
merged 8 commits into
appsmithorg:release
from
skjameela:fix/button-tooltip-in-table_widgetV2
Oct 30, 2024
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9b27dfc
fix:- added elipsis and tooltip to the button content in table widget…
skjameela 48d92df
resolved review comments
skjameela 92a26c9
resolved review comments for button tooltip
skjameela 8ffde6e
resolved the prettier and linting issues
skjameela 8f9d8d8
resolved review comments and added test cases
skjameela 4798350
removed duplicate test cases
skjameela fa303f9
Resolved merge conflict in AutoTooltipComponent.test.tsx
skjameela 36bfa53
resolved linting and prettier issues
skjameela File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,45 +36,66 @@ 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 mouseEnterHandler = () => { | ||
| const element = ref.current?.querySelector("div") as HTMLDivElement; | ||
|
|
||
| /* | ||
| * Using setTimeout to simulate hoverOpenDelay of the tooltip | ||
| * during initial render | ||
| */ | ||
| timeout = setTimeout(() => { | ||
| if (element && element.offsetWidth < element.scrollWidth) { | ||
| setRequiresTooltip(true); | ||
| } else { | ||
| setRequiresTooltip(false); | ||
| } | ||
|
|
||
| ref.current?.removeEventListener("mouseenter", mouseEnterHandler); | ||
| ref.current?.removeEventListener("mouseleave", mouseLeaveHandler); | ||
| }, TOOLTIP_OPEN_DELAY); | ||
| }; | ||
|
|
||
| const mouseLeaveHandler = () => { | ||
| clearTimeout(timeout); | ||
| }; | ||
|
|
||
| ref.current?.addEventListener("mouseenter", mouseEnterHandler); | ||
| ref.current?.addEventListener("mouseleave", mouseLeaveHandler); | ||
|
|
||
| return () => { | ||
| ref.current?.removeEventListener("mouseenter", mouseEnterHandler); | ||
| ref.current?.removeEventListener("mouseleave", mouseLeaveHandler); | ||
| clearTimeout(timeout); | ||
| }; | ||
| }, [children]); | ||
| useEffect( | ||
| function setupMouseHandlers() { | ||
| let timeout: ReturnType<typeof setTimeout>; | ||
| const currentRef = ref.current; | ||
|
|
||
| if (!currentRef) return; | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
| currentRef?.removeEventListener("mouseenter", mouseEnterHandler); | ||
| currentRef?.removeEventListener("mouseleave", mouseLeaveHandler); | ||
| }, TOOLTIP_OPEN_DELAY); | ||
| }; | ||
|
|
||
| const mouseLeaveHandler = () => { | ||
| setRequiresTooltip(false); | ||
| clearTimeout(timeout); | ||
| }; | ||
|
|
||
| currentRef?.addEventListener("mouseenter", mouseEnterHandler); | ||
| currentRef?.addEventListener("mouseleave", mouseLeaveHandler); | ||
|
|
||
| return () => { | ||
| currentRef?.removeEventListener("mouseenter", mouseEnterHandler); | ||
| currentRef?.removeEventListener("mouseleave", mouseLeaveHandler); | ||
| clearTimeout(timeout); | ||
| }; | ||
| }, | ||
| [children, isButton, ref], | ||
| ); | ||
|
|
||
| return requiresTooltip && children ? ( | ||
| <Tooltip | ||
|
|
@@ -160,11 +181,16 @@ function LinkWrapper(props: Props) { | |
|
|
||
| function AutoToolTipComponent(props: Props) { | ||
| const content = useToolTip(props.children, props.title); | ||
| const buttonContent = useToolTip(props.children, props.title, true); | ||
|
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. Isn't
This way, in one statement, you can get content for all the col types. |
||
|
|
||
| if (props.columnType === ColumnTypes.URL && props.title) { | ||
| return <LinkWrapper {...props} />; | ||
| } | ||
|
|
||
| if (props.columnType === ColumnTypes.BUTTON && props.title) { | ||
| return buttonContent; | ||
| } | ||
|
|
||
| return ( | ||
| <ColumnWrapper className={props.className} textColor={props.textColor}> | ||
| <CellWrapper | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.