Skip to content

Commit

Permalink
Merge branch 'dev' into fix-2368
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
  • Loading branch information
T4rk1n committed Jan 23, 2023
2 parents c2f3faf + 04afc06 commit 92ef512
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).

## Unreleased

### Added

- [#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

## Fixed

- [#2388](https://github.com/plotly/dash/pull/2388) Fix [#2368](https://github.com/plotly/dash/issues/2368) ordering or Pattern Matching ALL after update to the subtree.
Expand Down
14 changes: 12 additions & 2 deletions components/dash-html-components/scripts/generate-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ function generatePropTypes(element, attributes) {
*/
'n_clicks_timestamp': PropTypes.number,
/**
* When True, this will disable the n_clicks prop. Use this to remove
* event listeners that may interfere with screen readers.
*/
'disable_n_clicks': PropTypes.bool,
/**
* A unique identifier for the component, used to improve
* performance by React.js while rendering components
Expand Down Expand Up @@ -264,13 +270,17 @@ const ${Component} = (props) => {
dataAttributes['data-dash-is-loading'] = true;
}
/* remove unnecessary onClick event listeners */
const isStatic = props.disable_n_clicks || !props.id;
return (
<${element}
onClick={() => props.setProps({
{...(!isStatic && {onClick:
() => props.setProps({
n_clicks: props.n_clicks + 1,
n_clicks_timestamp: Date.now()
})
})}
{...omit(['n_clicks', 'n_clicks_timestamp', 'loading_state', 'setProps'], props)}
{...omit(['n_clicks', 'n_clicks_timestamp', 'loading_state', 'setProps', 'disable_n_clicks'], props)}
{...dataAttributes}
>
{props.children}
Expand Down
32 changes: 32 additions & 0 deletions components/dash-html-components/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,35 @@ def update_output(*args):
assert call_count.value == 3

assert not dash_duo.get_logs()

def test_click_static(dash_duo):
app = Dash(__name__)

app.layout = html.Div(
[
html.Div("no event listener", className="div-1"),
html.Div("event listener", id="div-2", n_clicks=0),
html.Div("no event listener", id="div-3", n_clicks=0, disable_n_clicks=True),
html.Div("event listener", id="div-4", n_clicks=0, disable_n_clicks=False),
html.Div(id="div-output"),
]
)

@app.callback(
Output("div-output", "children"),
Input("div-2", "n_clicks"),
Input("div-3", "n_clicks"),
Input("div-4", "n_clicks"),
prevent_initial_call=True,
)
def update(n2, n3, n4):
return f"{n2}, {n3}, {n4}"

dash_duo.start_server(app)
dash_duo.find_element("#div-2").click()
dash_duo.find_element("#div-3").click()
dash_duo.find_element("#div-4").click()

dash_duo.wait_for_text_to_equal("#div-output", "1, 0, 1")

assert not dash_duo.get_logs()

0 comments on commit 92ef512

Please sign in to comment.