gpt4 book ai didi

javascript - 语义 UI 搜索 - 下拉选择不填充输入

转载 作者:行者123 更新时间:2023-11-29 10:28:27 25 4
gpt4 key购买 nike

我正在使用 Semantic UI React 构建一个输入组件。我希望它在获得焦点时打开下拉菜单,而不是默认行为,即在用户更改搜索字符串时显示结果。我正在使用他们网站上提供的 Prop here .

这是我的一些相关代码:

constructor(props) {
super(props);
this.handleResultSelect = this.handleResultSelect.bind(this);
this.handleFocusSearch = this.handleFocusSearch.bind(this);
this.handleBlurSearch = this.handleBlurSearch.bind(this);
this.state = ({
results: [{
"title": "Roob, Cummerata and Watsica"
},
{
"title": "Stanton, Kessler and Walsh"
},
{
"title": "Boyle, Schuppe and Renner"
}],
value: '',
open: false,
});
}

handleBlurSearch () {
this.setState({
open: false,
focused: false,
});
}

handleFocusSearch () {
this.setState({
open: true,
focused: true,
});
}

handleResultSelect(e, {result}) {
this.setState({ value: result.title });
}

render() {
var searchProps = {
input: <input className='custom-form-field' placeholder={this.props.placeholder}/>,
open: this.state.open,
onFocus: this.handleFocusSearch,
onBlur: this.handleBlurSearch,
results: this.state.results,
onResultSelect: this.handleResultSelect,
value: this.state.value,
};

return (
<SemanticUI.Search {...searchProps} />
);
}

但是,在选择结果时,结果标题值未设置在输入值中。此外,在调试时,我发现 handleResultSelect 甚至没有被调用。

我的第一个猜测是 onBlur 导致结果下拉列表关闭,结果选择事件被忽略。不过我不确定;我对 React 和语义还很陌生。

我们欢迎任何解决这个问题的帮助。

最佳答案

尝试将 value 属性添加到 searchProps。此外,onBlur 事件和 onResultSelect 相互冲突,因此我使用 lodash.debounce 函数添加了延迟。

所以,像这样

class SearchExampe extends React.Component {
constructor(props) {
super(props);
this.handleResultSelect = this.handleResultSelect.bind(this);
this.handleFocusSearch = this.handleFocusSearch.bind(this);
this.handleBlurSearch = this.handleBlurSearch.bind(this);
this.handleSearchChange = this.handleSearchChange.bind(this);

this.state = {
results: [
{
title: "Roob, Cummerata and Watsica"
},
{
title: "Stanton, Kessler and Walsh"
},
{
title: "Boyle, Schuppe and Renner"
}
],
value: " ",
open: true
};
}

handleSearchChange(e) {
this.setState({ value: e.target.value });
}

handleBlurSearch() {
this.setState({
open: false,
focused: false
});
}

handleFocusSearch() {
this.setState({
open: true,
focused: true
});
}

handleResultSelect(e) {
this.setState({ value: e.target.value, open: false });
}

render() {
var searchProps = {
input: <input className="custom-form-field" onChange={this.handleSearchChange} placeholder="placeholder" />,
open: this.state.open,
onFocus: this.handleFocusSearch,
onBlur: _.debounce(this.handleBlurSearch, 100, {}),
results: this.state.results,
onResultSelect: this.handleResultSelect,
value: this.state.value
};

return <Search {...searchProps} />;
}
}

关于javascript - 语义 UI 搜索 - 下拉选择不填充输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52412650/

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