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

fixed #380 console warning with tooltip #418

Merged
merged 5 commits into from
Nov 18, 2024
Merged

Conversation

AnnMarieW
Copy link
Collaborator

closes #380
Fixed by adding a key to the boxProps

dash_duo.wait_for_text_to_equal("#text", "Text")
dash_duo.find_element("#text").click()

time.sleep(.2) # Adjust if needed based on any transition delays
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you really need the explicit delay? Is the find_element below not sufficient?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm, it failed once and I thought it was the issue, but I changed other things as well. I'll give it another go without it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It passed when I ran it locally, but failed here. I'll put the delay back and see if it passes

@@ -15,7 +15,7 @@ interface Props extends TooltipBaseProps, DashBaseProps {
const FloatingTooltip = (props: Props) => {
const { children, boxWrapperProps, setProps, loading_state, ...others } =
props;
const boxProps = { w: "fit-content", ...boxWrapperProps };
const boxProps = { w: "fit-content", key: "tooltip-target", ...boxWrapperProps };
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there ever a case you could get two of these boxes as siblings? It's unclear to me why this is necessary at all given the nesting structure of the react components it generates, Box is the only child of Tooltip.Floating, so why would it need a key? This makes me speculate there may be something funny going on when Mantine renders this... and two siblings with the same key could be worse than no key at all. Two components with tooltips next to each other? Two tooltips on the same component?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I'm concerned about this as well. Do you have any other suggestions? I did try an app with a few tooltips on a page and it worked fine. I'll try it with two tooltips on the same component.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, you can't have 2 tooltips on the same component because the Tootlip children must be a single component:

dmc.Tooltip(
            id="my-tooltip",
            children=dmc.Button("some text", id="my-text"),
            label="Tooltip text",
        ),

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

How about using a random number as a key?

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK - let's leave it then. In principle we could do a random number via useState or some such so that it's retained between renders... but it doesn't seem like this complexity is necessary.

CHANGELOG.md Outdated Show resolved Hide resolved
AnnMarieW and others added 2 commits November 17, 2024 15:55
@AnnMarieW
Copy link
Collaborator Author

I tried it with nested tooltips and it seemed to work

import dash_mantine_components as dmc
from dash import Dash, _dash_renderer, html

_dash_renderer._set_react_version("18.2.0")

app = Dash()

component = dmc.Card([
    dmc.Button("some text", id="my-text"),
    dmc.Space(h=100),
    dmc.Tooltip(
            id="my-tooltip2",
            children=dmc.Button("some text", id="my-text2"),
            label="Tooltip text2",
        ),
    dmc.Space(h=100),
], withBorder=True)

app.layout = dmc.MantineProvider([
        dmc.Tooltip(
            id="my-tooltip",
            children=component,
            label="Tooltip text",
        ),
     ])


if __name__ == '__main__':
    app.run(debug=True)

image

Copy link
Collaborator

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

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

💃

@AnnMarieW
Copy link
Collaborator Author

@alexcjohnson do you think this is really OK, or should we update to use a random number as a key?

@alexcjohnson
Copy link
Collaborator

Yes, it's fine as is. Looking at the React component tree I see stuff like this:
Screenshot 2024-11-17 at 20 34 00
You can see that the Box has a sibling, the OptionalPortal. So I wouldn't think React would have any trouble distinguishing them since they're different types, which is why the warning seems weird to me but whatever, best to placate it. We can clearly tell because of the way Dash constructs the component tree there's no way another tooltip's Box would be a sibling of these components, so we'll never get a key collision with or without a random number and we might as well keep it simple and not run the risk of causing excessive rerenders by unintentionally changing the key.

@AnnMarieW
Copy link
Collaborator Author

ok, mystery solved!
Thanks - I didn't know you could inspect the React component tree like that. Now I have another tool in my dash toolkit 🛠️

@AnnMarieW AnnMarieW merged commit 1ec9ee6 into snehilvj:master Nov 18, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] react warning "Each child in a list should have a unique "key" prop."
2 participants