gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:57:30 24 4
gpt4 key购买 nike

我已经遵循了ReactiveSearch上的教程,并且将它与React以及Appbase.io上的托管Elastic实例一起使用。

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

这是因为我不想有一个搜索结果页面,只是为了让应用程序立即根据所选值将您带到正确的页面。

我以为可以使用 strictSelection onValueSelected 来做到这一点,但它仍然允许像“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链接: https://codesandbox.io/embed/wqjpoq25w

最佳答案

您已经快懂了。此处strictSelection的关键是还要检查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'


该API有助于为 strictSelection编写不同的流程。您可以按照以下方法检查建议的选择:
<DataSearch
...
onValueSelected={(value, cause, source) => {
if (cause === 'SUGGESTION_SELECT') {
document.location.href = `./${value}`;
}
}}
/>
Demo

关于reactjs - ReactiveSearch-无法从自动完成中强制选择(例如strictSelection)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52265808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com