简体   繁体   English

ReactiveSearch-无法从自动完成中强制选择(例如strictSelection)吗?

[英]ReactiveSearch - Can't Force a Selection (e.g. strictSelection) from the autoComplete?

I've followed the tutorials on ReactiveSearch, and I'm using it with React and a hosted Elastic instance on Appbase.io. 我已经遵循了ReactiveSearch上的教程,并且将它与React以及Appbase.io上的托管Elastic实例一起使用。

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). 我希望用户看到自动建议,但是只能从建议列表中进行选择(因此,如果“ foo”不在建议列表中,则不应执行查询)。

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. 我以为我可以使用strictSelectiononValueSelected做到这一点 ,但它仍然允许像“ foo”这样的值(这不是自动完成值)通过。

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 Codesandbox链接: https ://codesandbox.io/embed/wqjpoq25w

You've almost gotten it. 您已经快懂了。 The key here with strictSelection is to also check the cause of value selection in onValueSelected docs : strictSelection的关键是还要检查onValueSelected docs中值选择的原因:

onValueSelected is called whenever a suggestion is selected or a search is performed by pressing enter key. 每当选择建议或通过按Enter键执行搜索时,都会调用onValueSelected It also passes the cause of action and the source object if the cause of action was 'SUGGESTION_SELECT'. 如果原因是“ SUGGESTION_SELECT”,它也会传递原因和源对象。 The possible causes are: 可能的原因是:

'SUGGESTION_SELECT' 'SUGGESTION_SELECT'

'ENTER_PRESS' ENTER_PRESS

'CLEAR_VALUE' “ CLEAR_VALUE”

This API helps in writing different flows for strictSelection . 该API有助于为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 演示版

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 IDE 中的文本区域中反应自动完成(例如 VS Code、Atom) - React autocomplete in a textarea like in an IDE (e.g. VS Code, Atom) ReactiveSearch仅自动完成 - ReactiveSearch only autocomplete “选择框”(例如,在Finder中选择多个项目)的专有名词是什么? (反应) - What is the proper term for “selection boxes” (e.g. selecting multiple items in Finder)? (React) 可以使路由器的路由带有多个选项参数,其中(例如)两个都需要,或者都不是 - Can react router have a route with multiple option parameters where (e.g.) both are required, or none Redux-i18n在React组件之外访问t()函数,例如在动作创建者中 - Redux-i18n accessing t() function outside React components, e.g. in action creators 从例如IF语句访问时,React无法读取未定义的属性 - React cannot read property of undefined when accessing from e.g. IF statement 以简写形式呈现用户输入的字节数(例如“kB”、“MB”、“GB”等) - Render number of bytes from user input in short notation (e.g. 'kB', 'MB', 'GB', etc) 来自 SCSS 的 Flexbox 在 Firefox 上正常工作,但在其他浏览器(例如 Chrome、Edge 等)上不能正常工作 - Flexbox from SCSS working correctly on Firefox but not on other browsers (e.g. Chrome, Edge, …) 无论如何使用 npm start 从带前缀的 URL(例如 /client/static)服务 /static ? - Anyway to serve /static from a prefixed URL (e.g. /client/static) with npm start? React + flux:可以动作返回一个值吗? (例如最后创建的id) - React + flux: can action return a value? (e.g. last created id)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM