Skip to content

Commit a00407d

Browse files
authored
Merge pull request #3168 from plotly/fix/dot-in-dict
Fix clientside pattern matching with a dot in the id.
2 parents 2bd3258 + 0c83a4b commit a00407d

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

dash/dash-renderer/src/actions/callbacks.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -661,9 +661,14 @@ function getTriggeredId(triggered: string[]): string | object | undefined {
661661
// for regular callbacks, takes the first triggered prop_id, e.g. "btn.n_clicks" and returns "btn"
662662
// for pattern matching callback, e.g. '{"index":0, "type":"btn"}' and returns {index:0, type: "btn"}'
663663
if (triggered && triggered.length) {
664-
let componentId = triggered[0].split('.')[0];
665-
if (componentId.startsWith('{')) {
666-
componentId = JSON.parse(componentId);
664+
const trig = triggered[0];
665+
let componentId;
666+
if (trig.startsWith('{')) {
667+
componentId = JSON.parse(
668+
trig.substring(0, trig.lastIndexOf('}') + 1)
669+
);
670+
} else {
671+
componentId = trig.split('.')[0];
667672
}
668673
return componentId;
669674
}

tests/integration/clientside/test_clientside.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from dash import Dash, Input, Output, State, ClientsideFunction, ALL, html, dcc
55
from selenium.webdriver.common.keys import Keys
66

7+
from dash.dependencies import MATCH
8+
79

810
def test_clsd001_simple_clientside_serverside_callback(dash_duo):
911
app = Dash(__name__, assets_folder="assets")
@@ -905,3 +907,33 @@ def update_output(value):
905907
dash_duo.find_element("#input").send_keys("hello world")
906908
dash_duo.wait_for_text_to_equal("#output-serverside", 'Server says "hello world"')
907909
dash_duo.wait_for_text_to_equal("#output-clientside", 'Client says "hello world"')
910+
911+
912+
def test_clsd022_clientside_pattern_matching_dots(dash_duo):
913+
# Test for bug https://github.com/plotly/dash/issues/3163
914+
# Allow dict id to contains a dot in the dict when using clientside callback.
915+
app = Dash()
916+
app.layout = html.Div(
917+
[
918+
html.Button("click", id={"type": "input", "index": 3.1}),
919+
html.Div(id={"type": "output", "index": 3.1}, className="output"),
920+
]
921+
)
922+
923+
app.clientside_callback(
924+
"""
925+
function(n_clicks) {
926+
return `clicked ${n_clicks}`;
927+
}
928+
""",
929+
Output({"type": "output", "index": MATCH}, "children"),
930+
Input({"type": "input", "index": MATCH}, "n_clicks"),
931+
prevent_initial_call=True,
932+
)
933+
934+
dash_duo.start_server(app)
935+
936+
dash_duo.find_element("button").click()
937+
dash_duo.wait_for_text_to_equal(".output", "clicked 1")
938+
939+
assert dash_duo.get_logs() == []

0 commit comments

Comments
 (0)