Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit a75f38e

Browse files
Shammamah HossainMarc-Andre-Rivet
Shammamah Hossain
andauthored
Ensure non-clearable dropdowns are actually not clearable. (#731)
* Fix clearable bug for single-option dropdowns. * Fix clearable bug for multi-option dropdowns. * Update CHANGELOG. * Return if selected option is nil and dropdown is not clearable. * Use backspaceRemoves and deleteRemoves. Co-authored-by: Marc-André Rivet <[email protected]>
1 parent 9bea97b commit a75f38e

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1919
- Added responsiveness on graph parent element resize (previously only worked on window.resize)
2020
- Added new `dash-graph--pending` class to dcc.Graph, present while resizing, (re-)rendering, loading
2121

22+
### Fixed
23+
- [#731](https://github.com/plotly/dash-core-components/pull/731) Fixed bug where non-clearable dropdowns could still be cleared by typing backspace
24+
2225
### Changed
2326
- [#723](https://github.com/plotly/dash-core-components/pull/723) Changed npm package content to allow source code inclusion from other projects
2427
- [#725](https://github.com/plotly/dash-core-components/pull/725) Improve async graph performance by parallelizing resource fetching instead of fetching sequentially

src/fragments/Dropdown.react.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default class Dropdown extends Component {
4646
render() {
4747
const {
4848
id,
49+
clearable,
4950
multi,
5051
options,
5152
setProps,
@@ -92,6 +93,8 @@ export default class Dropdown extends Component {
9293
}
9394
}}
9495
onInputChange={search_value => setProps({search_value})}
96+
backspaceRemoves={clearable}
97+
deleteRemoves={clearable}
9598
{...omit(['setProps', 'value'], this.props)}
9699
/>
97100
</div>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import dash
2+
from dash.dependencies import Input, Output
3+
import dash_core_components as dcc
4+
import dash_html_components as html
5+
6+
from selenium.webdriver.common.keys import Keys
7+
8+
9+
def test_ddcf001_clearable_false_single(dash_duo):
10+
app = dash.Dash(__name__)
11+
app.layout = html.Div([
12+
dcc.Dropdown(
13+
id='my-unclearable-dropdown',
14+
options=[
15+
{'label': 'New York City', 'value': 'NYC'},
16+
{'label': 'Montreal', 'value': 'MTL'},
17+
{'label': 'San Francisco', 'value': 'SF'},
18+
],
19+
value='MTL',
20+
clearable=False
21+
),
22+
html.Div(
23+
id='dropdown-value',
24+
style={'height': '10px', 'width': '10px'}
25+
)
26+
])
27+
28+
@app.callback(
29+
Output('dropdown-value', 'children'),
30+
[Input('my-unclearable-dropdown', 'value')]
31+
)
32+
def update_value(val):
33+
return val
34+
35+
dash_duo.start_server(app)
36+
37+
dropdown = dash_duo.find_element('#my-unclearable-dropdown input')
38+
dropdown.send_keys(Keys.BACKSPACE)
39+
dash_duo.find_element('#dropdown-value').click()
40+
41+
assert len(dash_duo.find_element('#dropdown-value').text) > 0
42+
43+
44+
def test_ddcf002_clearable_false_multi(dash_duo):
45+
app = dash.Dash(__name__)
46+
app.layout = html.Div([
47+
dcc.Dropdown(
48+
id='my-unclearable-dropdown',
49+
options=[
50+
{'label': 'New York City', 'value': 'NYC'},
51+
{'label': 'Montreal', 'value': 'MTL'},
52+
{'label': 'San Francisco', 'value': 'SF'},
53+
],
54+
value=['MTL', 'SF'],
55+
multi=True,
56+
clearable=False
57+
),
58+
html.Div(
59+
id='dropdown-value',
60+
style={'height': '10px', 'width': '10px'}
61+
)
62+
])
63+
64+
@app.callback(
65+
Output('dropdown-value', 'children'),
66+
[Input('my-unclearable-dropdown', 'value')]
67+
)
68+
def update_value(val):
69+
return ', '.join(val)
70+
71+
dash_duo.start_server(app)
72+
73+
dropdown = dash_duo.find_element('#my-unclearable-dropdown input')
74+
dropdown.send_keys(Keys.BACKSPACE)
75+
dropdown.send_keys(Keys.BACKSPACE)
76+
dash_duo.find_element('#dropdown-value').click()
77+
78+
assert len(dash_duo.find_element('#dropdown-value').text) > 0

0 commit comments

Comments
 (0)