Skip to content

Fix clientside pattern matching with a dot in the id. #3168

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
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 8 additions & 3 deletions dash/dash-renderer/src/actions/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,9 +661,14 @@ function getTriggeredId(triggered: string[]): string | object | undefined {
// for regular callbacks, takes the first triggered prop_id, e.g. "btn.n_clicks" and returns "btn"
// for pattern matching callback, e.g. '{"index":0, "type":"btn"}' and returns {index:0, type: "btn"}'
if (triggered && triggered.length) {
let componentId = triggered[0].split('.')[0];
if (componentId.startsWith('{')) {
componentId = JSON.parse(componentId);
const trig = triggered[0];
let componentId;
if (trig.startsWith('{')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

worth stripping whitespace from triggered[0] before this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, we create this composite, there is no whitespace possible.

componentId = JSON.parse(
trig.substring(0, trig.lastIndexOf('}') + 1)
);
} else {
componentId = trig.split('.')[0];
}
return componentId;
}
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/clientside/test_clientside.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from dash import Dash, Input, Output, State, ClientsideFunction, ALL, html, dcc
from selenium.webdriver.common.keys import Keys

from dash.dependencies import MATCH


def test_clsd001_simple_clientside_serverside_callback(dash_duo):
app = Dash(__name__, assets_folder="assets")
Expand Down Expand Up @@ -905,3 +907,33 @@ def update_output(value):
dash_duo.find_element("#input").send_keys("hello world")
dash_duo.wait_for_text_to_equal("#output-serverside", 'Server says "hello world"')
dash_duo.wait_for_text_to_equal("#output-clientside", 'Client says "hello world"')


def test_clsd022_clientside_pattern_matching_dots(dash_duo):
# Test for bug https://github.com/plotly/dash/issues/3163
# Allow dict id to contains a dot in the dict when using clientside callback.
app = Dash()
app.layout = html.Div(
[
html.Button("click", id={"type": "input", "index": 3.1}),
html.Div(id={"type": "output", "index": 3.1}, className="output"),
]
)

app.clientside_callback(
"""
function(n_clicks) {
return `clicked ${n_clicks}`;
}
""",
Output({"type": "output", "index": MATCH}, "children"),
Input({"type": "input", "index": MATCH}, "n_clicks"),
prevent_initial_call=True,
)

dash_duo.start_server(app)

dash_duo.find_element("button").click()
dash_duo.wait_for_text_to_equal(".output", "clicked 1")

assert dash_duo.get_logs() == []