1

I've followed the tutorials on ReactiveSearch, and I'm using it with React and a hosted Elastic instance on Appbase.io.

I want the user to see auto suggestions, but then only be able to select from the list of suggestions (so if "foo" isn't in the list of suggestions, the query shouldn't be executed).

This is because I don't want to have a search results page, just for the app to immediately take you to the right page based on the selected value.

I thought I could do this with strictSelection and onValueSelected, but it still is allowing a value like "foo" (which is not an autocomplete value) to go through.

import React, { Component } from "react";
import ReactDOM from "react-dom";

import { ReactiveBase, DataSearch } from "@appbaseio/reactivesearch";

class Main extends Component {
  render() {
    return (
      <div className="flex-col lighter">
        <ReactiveBase
          app="bravos"
          credentials="b8AuX6o06:19f6637f-0a80-48f7-8fe7-9fa0339b7c71"
        >
          <DataSearch
            className=""
            autosuggest={true}
            strictSelection={true}
            componentId="search"
            placeholder="Search Name/Ticker"
            dataField={["symbol", "name"]}
            onValueSelected={value => {
              document.location.href = `./${value}`;
            }}
          />
        </ReactiveBase>
      </div>
    );
  }
}

ReactDOM.render(<Main />, document.getElementById("root"));

Codesandbox link: https://codesandbox.io/embed/wqjpoq25w

  • Please review [how to ask](https://stackoverflow.com/help/how-to-ask) and [MVCE](https://stackoverflow.com/help/mcve) then `edit` to embed relevant code within the body of the question as per SO guidelines. This makes it easier for current users to help you, future users to see if your question will help them without hopping about the web, but also ensures the integrity of Q/A on SO itself - if the link were to become unreachable, this post would become useless. Thank you, and all the best finding a solution. – SherylHohman Sep 11 '18 at 16:13
  • 1
    @SherylHohman of course, updated and thank you. – Daniel Axelsen Sep 11 '18 at 16:51

1 Answers1

0

You've almost gotten it. The key here with strictSelection is to also check the cause of value selection in onValueSelected docs:

onValueSelected is called whenever a suggestion is selected or a search is performed by pressing enter key. It also passes the cause of action and the source object if the cause of action was 'SUGGESTION_SELECT'. The possible causes are:

'SUGGESTION_SELECT'

'ENTER_PRESS'

'CLEAR_VALUE'

This API helps in writing different flows for strictSelection. Here's how you may check for a suggestion selection:

<DataSearch
  ...
  onValueSelected={(value, cause, source) => {
    if (cause === 'SUGGESTION_SELECT') {
      document.location.href = `./${value}`;
    }
  }}
/>

Demo

Community
  • 1
  • 1
Divyanshu Maithani
  • 13,908
  • 2
  • 36
  • 47
  • Thank you! This is perfect. I had tried using SUGGESTION_SELECT, but not correctly, so probably helpful to have this demo out there. Many thanks. – Daniel Axelsen Sep 11 '18 at 16:57
  • The behavior of the search box is almost perfect now, but the last thing that I would want if possible, is to auto-select the top search suggestion on enter. So if you type "app", hit enter, and the top result is AAPL, it queries AAPL (even if you haven't selected it from the suggestions with keys or mouse). – Daniel Axelsen Sep 11 '18 at 17:13
  • For this you would have to create a custom [`ReactiveComponent`](https://opensource.appbase.io/reactive-manual/advanced/reactivecomponent.html) since it's currently not possible to extend the component in that way. I had a proposal [here](https://github.com/appbaseio/reactivesearch/issues/355) to make it easier but it hasn't been worked upon yet. If you would like to you may add your thoughts to it as well :) – Divyanshu Maithani Sep 11 '18 at 17:32
  • Wow, so the auto suggest doesn't seem like a useful feature then, right? I have the code live here - https://bravos.co/ - try typing "aa" and then hitting enter? is there any way to do that and have a useful behavior result? Thanks again. – Daniel Axelsen Sep 12 '18 at 20:04
  • The other way here is to allow modifying prop to the `Downshift` dropdown component which is used internally. Added a issue [here](https://github.com/appbaseio/reactivesearch/issues/516). You may subscribe to it for updates :) – Divyanshu Maithani Sep 13 '18 at 05:40