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

Add search_value property to Dropdown #660

Merged
merged 11 commits into from
Sep 26, 2019
6 changes: 6 additions & 0 deletions src/components/Dropdown.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default class Dropdown extends Component {
setProps({value});
}
}}
onInputChange={search_value => setProps({search_value})}
{...omit(['setProps', 'value'], this.props)}
/>
</div>
Expand Down Expand Up @@ -195,6 +196,11 @@ Dropdown.propTypes = {
*/
searchable: PropTypes.bool,

/**
* The value typed in the DropDown for searching.
*/
search_value: PropTypes.string,

/**
* Dash-assigned callback that gets fired when the input changes
*/
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/dropdown/test_search_value.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from multiprocessing import Value
import dash
from dash.dependencies import Input, Output, State
import dash.testing.wait as wait
import dash_core_components as dcc
import dash_html_components as html


def test_ddsv001_search_value(dash_duo):
app = dash.Dash(__name__)
app.layout = html.Div(
[dcc.Dropdown(id="dropdown", search_value="something"), html.Div(id="output")]
)

call_count = Value("i", 0)

@app.callback(
Output("output", "children"), inputs=[Input("dropdown", "search_value")]
)
def update_output(search_value):
call_count.value += 1
return 'search_value="{}"'.format(search_value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I missed this earlier - I think the test should use the same structure as the example app you gave, which is updating options from search_value in the same component. The reason this is important is (a) that's the primary use case for this prop, and (b) it will cause the dropdown to rerender, and we need to ensure that when it does the correct options are visible. I can certainly imagine errors in the component where on render it loses focus, or for some other reason it stops displaying the option list.

(BTW nota big deal but you can probably get rid of all the call_count stuff, that doesn't really matter for this test, we only care about what's going on within the browser)


dash_duo.start_server(app)

# Get the inner input used for search value.
input_ = dash_duo.find_element("#dropdown input")

# callback gets called with initial input
wait.until(lambda: call_count.value == 1, timeout=1)

dash_duo.wait_for_text_to_equal("#output", 'search_value="something"')

input_.send_keys("x")
wait.until(lambda: call_count.value == 2, timeout=1)
dash_duo.wait_for_text_to_equal("#output", 'search_value="x"')
1 change: 1 addition & 0 deletions tests/unit/Dropdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Props can be set properly', () => {
multi: false,
placeholder: 'pick something',
searchable: true,
search_value: "hello",
style: {backgroundColor: 'hotpink'},
loading_state: {
is_loading: false,
Expand Down