不太确定如何做到这一点,这在Redux-Form世界中是很新的。我的输入有一个自定义组件,它有一个显示无序列表的onFocus事件和一个隐藏它的onBlur事件。我还将onClick事件附加到项,因为我希望在单击列表项后更改输入字段的值,但单击事件从不触发,因为模糊首先触发。不确定我是否正确地处理了我的代码。下面是我的自定义组件:
import React, { Component } from 'react'
import pageStyles from '../../../styles.scss'
class MyInput extends Component {
constructor(props) {
super(props);
this.state = {
dropdownIsVisible: false,
dropdownCssClass: 'dropdown'
}
this.handleFocus = this.handleFocus.bind(this);
this.handleBlur = this.handleBlur.bind(this);
this.handleClick = this.handleClick.bind(this);
}
handleFocus () {
this.setState({
dropdownIsVisible: true,
dropdownCssClass: ['dropdown', pageStyles.dropdown].join(' ')
})
}
handleBlur () {
this.setState({
dropdownIsVisible: false,
dropdownCssClass: 'dropdown'
})
}
handleClick () {
console.log('here I am')
}
render() {
const {
input: {
name,
value,
onFocus
},
label,
hasOptions,
options,
cssClass,
meta: {
touched,
error
}
} = this.props
if(options) {
var listItems = options.map((item) =>
<li key={ item.id } onClick={ this.handleClick }>{ item.name }</li>
)
}
return (
<div className={ cssClass }>
<label>{ label }</label>
<input name={ name } id={ name } type="text" onFocus={ this.handleFocus } onBlur={ this.handleBlur } autoComplete="off" />
{ hasOptions === true ? <ul className={ this.state.dropdownCssClass }>{ listItems }</ul> : null }
{ touched && error && <span className="error">{ error }</span> }
</div>
)
}
}
export default MyInput发布于 2017-08-04 22:10:47
使用lodash debounce函数。此函数会在函数调用前添加延迟,因此在嵌套元素单击时可以取消该函数。
添加构造函数:
this.handleBlur = debounce(this.handleBlur, 100);替换handleClick
handleClick () {
if(this.handleBlur.cancel){
this.handleBlur.cancel()
}
console.log('here I am')
}https://stackoverflow.com/questions/45508597
复制相似问题