首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理Redux-form事件

处理Redux-form事件
EN

Stack Overflow用户
提问于 2017-08-04 21:50:35
回答 1查看 448关注 0票数 1

不太确定如何做到这一点,这在Redux-Form世界中是很新的。我的输入有一个自定义组件,它有一个显示无序列表的onFocus事件和一个隐藏它的onBlur事件。我还将onClick事件附加到项,因为我希望在单击列表项后更改输入字段的值,但单击事件从不触发,因为模糊首先触发。不确定我是否正确地处理了我的代码。下面是我的自定义组件:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-04 22:10:47

使用lodash debounce函数。此函数会在函数调用前添加延迟,因此在嵌套元素单击时可以取消该函数。

添加构造函数:

代码语言:javascript
复制
this.handleBlur = debounce(this.handleBlur, 100);

替换handleClick

代码语言:javascript
复制
handleClick () {
  if(this.handleBlur.cancel){
    this.handleBlur.cancel()
  }
  console.log('here I am')
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45508597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档