|
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 |
|
5 |
| -from dash import Dash, html, dcc, Output, Input |
| 5 | +from dash import Dash, html, dcc, Output, Input, State |
6 | 6 | from dash.exceptions import PreventUpdate
|
7 | 7 |
|
8 | 8 |
|
@@ -88,3 +88,64 @@ def on_change(val):
|
88 | 88 | btn.click()
|
89 | 89 |
|
90 | 90 | 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