我正在使用语义UI React构建输入组件。我希望它在焦点对准时打开下拉列表,而不是默认行为,即当用户更改搜索字符串时显示结果。我正在使用他们的网站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和Semantic的新手。
欢迎大家提供帮助。
答案 0 :(得分:2)
尝试将值prop添加到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} />;
}
}
答案 1 :(得分:1)
我非常感谢R. Wright的回答,但是下拉模糊中添加的200ms延迟不符合UX标准。因此,我对javascript的blur进行了更深入的研究,发现它具有一个relatedTarget
属性,该属性可用于查看单击了哪个元素。
请注意,这某种程度上仅适用于具有tabindex
属性的DOM元素,因此我还必须修改Semantic Search的结果渲染器,以使每个结果都具有属性tabindex=0
。另外,可以覆盖tabindex
应用于元素的默认焦点CSS。
以此为基础,我编辑了handleBlur
,将open: true
设置为_.contains(event.relatedTarget.classList, 'title')
。
这是我的一些相关代码:
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 (event) {
let open = _.contains(event.relatedTarget.classList, 'title');
this.setState({
open: open,
focused: false,
});
}
handleFocusSearch () {
this.setState({
open: true,
focused: true,
});
}
handleResultSelect(e, {result}) {
this.setState({ value: result.title, open: false });
}
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,
};
return (
<SemanticUI.Search {...searchProps} />
);
}
答案 2 :(得分:0)
另一个选项,查看“下拉列表”组件。下拉列表中的“搜索选择”示例可能会显示您尝试创建的行为。
https://react.semantic-ui.com/modules/dropdown/#types-search-selection