2

I am trying to implement a searchable dropdown (using Semantic UI React library) that is populated by matching results from the server. The user is presented with with an input box. As soon as they start typing a few characters, a request is made to a backend restful endpoint which returns matching search results. These results are displayed as values in the dropdown.

This was quite easy with plain Semantic UI (explained at https://semantic-ui.com/modules/dropdown.html#match-search-query-on-server).

But how can I achieve this with the React version of the library?

I can't figure out from the documentation (https://react.semantic-ui.com/modules/dropdown/#usage-remote).

Can someone provide me with an example?

1 Answer 1

6

I was able to code it as follows:

class MyDd extends React.Component {
  state = {options: []}

  onSearchChange = (e, value) => {
    axios.get(`/api/match?query=${value.searchQuery}`)
     .then((response) => {
       this.setState({options: response.data})
    })

  }

  render() {
    return (
      <Dropdown
        onSearchChange={this.onSearchChange}
        search
        selection
        options={this.state.options}
      />
    )
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I also needed to set search in the Dropdown to identity (options => options) as it otherwise tried to filter my options based on their text property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.