Skip to content

Commit dc56682

Browse files
committed
remove state and use search_value instead. add test for multiple dropdowns
1 parent 024d936 commit dc56682

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

components/dash-core-components/src/fragments/Dropdown.react.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ const Dropdown = props => {
4242
multi,
4343
options,
4444
setProps,
45+
search_value,
4546
style,
4647
loading_state,
4748
value,
4849
} = props;
4950
const [optionsCheck, setOptionsCheck] = useState(null);
50-
const [searchValue, setSearchValue] = useState(null);
5151
const persistentOptions = useRef(null);
5252

5353
if (!persistentOptions || !isEqual(options, persistentOptions.current)) {
@@ -115,14 +115,14 @@ const Dropdown = props => {
115115
[multi]
116116
);
117117

118-
const onInputChange = useCallback(search_value => {
119-
setProps({search_value});
120-
setSearchValue(search_value);
121-
}, []);
118+
const onInputChange = useCallback(
119+
search_value => setProps({search_value}),
120+
[]
121+
);
122122

123123
useEffect(() => {
124124
if (
125-
!searchValue &&
125+
!search_value &&
126126
!isNil(sanitizedOptions) &&
127127
optionsCheck !== sanitizedOptions &&
128128
!isNil(value)

components/dash-core-components/tests/integration/dropdown/test_remove_option.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from dash import Dash, html, dcc, Output, Input
5+
from dash import Dash, html, dcc, Output, Input, State
66
from dash.exceptions import PreventUpdate
77

88

@@ -88,3 +88,64 @@ def on_change(val):
8888
btn.click()
8989

9090
dash_dcc.wait_for_text_to_equal("#value-output", '["MTL"]')
91+
92+
93+
def test_ddro003_remove_option_multiple_dropdowns(dash_dcc):
94+
app = Dash(__name__)
95+
app.layout = html.Div(
96+
[
97+
dcc.Dropdown(
98+
id="available-options",
99+
multi=True,
100+
options=sample_dropdown_options,
101+
value=["MTL", "NYC", "SF"],
102+
),
103+
dcc.Dropdown(
104+
id="chosen",
105+
multi=True,
106+
options=sample_dropdown_options,
107+
value=["NYC", "SF"],
108+
),
109+
html.Button(id="remove-btn", children="Remove"),
110+
html.Button(id="submit-btn", children="Submit"),
111+
html.Div(id="value-output"),
112+
html.Div(id="options-output"),
113+
],
114+
)
115+
116+
@app.callback(
117+
Output("chosen", "options"),
118+
Input("available-options", "value"),
119+
)
120+
def update_options(available_options):
121+
if available_options is None:
122+
return []
123+
else:
124+
return [{"label": i, "value": i} for i in available_options]
125+
126+
@app.callback(
127+
Output("available-options", "options"), [Input("remove-btn", "n_clicks")]
128+
)
129+
def on_click(n_clicks):
130+
if not n_clicks:
131+
raise PreventUpdate
132+
return sample_dropdown_options[:-1]
133+
134+
@app.callback(
135+
[Output("value-output", "children"), Output("options-output", "children")],
136+
Input("submit-btn", "n_clicks"),
137+
State("chosen", "options"),
138+
State("chosen", "value"),
139+
)
140+
def print_value(n_clicks, options, value):
141+
if not n_clicks:
142+
raise PreventUpdate
143+
return [json.dumps(value), json.dumps([i["value"] for i in options])]
144+
145+
dash_dcc.start_server(app)
146+
btn = dash_dcc.wait_for_element("#remove-btn")
147+
btn.click()
148+
btn = dash_dcc.wait_for_element("#submit-btn")
149+
btn.click()
150+
dash_dcc.wait_for_text_to_equal("#value-output", '["NYC"]')
151+
dash_dcc.wait_for_text_to_equal("#options-output", '["MTL", "NYC"]')

0 commit comments

Comments
 (0)