Skip to content

Commit 04afc06

Browse files
authored
Merge pull request #2389 from AnnMarieW/disable_n_clicks
add disable_n_clicks prop to html components
2 parents c60703e + a1b8491 commit 04afc06

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ This project adheres to [Semantic Versioning](https://semver.org/).
44

55
## Unreleased
66

7+
8+
### Added
9+
10+
- [#2389](https://github.com/plotly/dash/pull/2389) Added `disable_n_clicks` prop to all html components to make it possible to remove onclick event listeners
11+
712
### Updated
813

914
- [#2367](https://github.com/plotly/dash/pull/2367) Updated the default `favicon.ico` to the current Plotly logo

components/dash-html-components/scripts/generate-components.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ function generatePropTypes(element, attributes) {
138138
*/
139139
'n_clicks_timestamp': PropTypes.number,
140140
141+
/**
142+
* When True, this will disable the n_clicks prop. Use this to remove
143+
* event listeners that may interfere with screen readers.
144+
*/
145+
'disable_n_clicks': PropTypes.bool,
146+
141147
/**
142148
* A unique identifier for the component, used to improve
143149
* performance by React.js while rendering components
@@ -264,13 +270,17 @@ const ${Component} = (props) => {
264270
dataAttributes['data-dash-is-loading'] = true;
265271
}
266272
273+
/* remove unnecessary onClick event listeners */
274+
const isStatic = props.disable_n_clicks || !props.id;
267275
return (
268276
<${element}
269-
onClick={() => props.setProps({
277+
{...(!isStatic && {onClick:
278+
() => props.setProps({
270279
n_clicks: props.n_clicks + 1,
271280
n_clicks_timestamp: Date.now()
281+
})
272282
})}
273-
{...omit(['n_clicks', 'n_clicks_timestamp', 'loading_state', 'setProps'], props)}
283+
{...omit(['n_clicks', 'n_clicks_timestamp', 'loading_state', 'setProps', 'disable_n_clicks'], props)}
274284
{...dataAttributes}
275285
>
276286
{props.children}

components/dash-html-components/tests/test_integration.py

+32
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,35 @@ def update_output(*args):
105105
assert call_count.value == 3
106106

107107
assert not dash_duo.get_logs()
108+
109+
def test_click_static(dash_duo):
110+
app = Dash(__name__)
111+
112+
app.layout = html.Div(
113+
[
114+
html.Div("no event listener", className="div-1"),
115+
html.Div("event listener", id="div-2", n_clicks=0),
116+
html.Div("no event listener", id="div-3", n_clicks=0, disable_n_clicks=True),
117+
html.Div("event listener", id="div-4", n_clicks=0, disable_n_clicks=False),
118+
html.Div(id="div-output"),
119+
]
120+
)
121+
122+
@app.callback(
123+
Output("div-output", "children"),
124+
Input("div-2", "n_clicks"),
125+
Input("div-3", "n_clicks"),
126+
Input("div-4", "n_clicks"),
127+
prevent_initial_call=True,
128+
)
129+
def update(n2, n3, n4):
130+
return f"{n2}, {n3}, {n4}"
131+
132+
dash_duo.start_server(app)
133+
dash_duo.find_element("#div-2").click()
134+
dash_duo.find_element("#div-3").click()
135+
dash_duo.find_element("#div-4").click()
136+
137+
dash_duo.wait_for_text_to_equal("#div-output", "1, 0, 1")
138+
139+
assert not dash_duo.get_logs()

0 commit comments

Comments
 (0)