用react 构建电子表格(3)---点击事件冲突的处理

选择单元格必然首先发生点击事件,单击和双击可能有冲突,单击的时候,我们要判断是独立的单击还是双击的组成,双击的时候,不能调用2次单击方法。

解决办法就是标志位+定时器,

    /**
     * Handle doubleclicking a Cell.
     */
    doubleClicked = () => {
        // Prevent click and double click to conflict
        clearTimeout(this.timer)
        this.prevent = true;
     执行双击引发的业务
            ......
    }


     /**
     * Handle clicking a Cell.
     */
    clicked = () => {
        // Prevent click and double click to conflict
        this.timer = setTimeout(() => {
            if (!this.prevent) {
               执行单击引发的业务
                    ......
            }
            this.prevent = false  //单击 双击都会执行本行代码
        }, this.delay)
    }

双击的时候,其实还是也会调用单击方法,但只会执行单击中的

最好有cell也能记住自己的位置

cell点击事件的时候,向上传递自己位置信息this.prevent = false

事件:点击单元格,可能需要广播一些事件:

广播事件:

监听事件:componentDidMount中注册监听,componentDidMount : 在第一次渲染后调用,只在客户端。

   componentDidMount(){
        window.document.addEventListener('eventA', this.handleeventA)
    }

事件处理函数:

  /**
   * Used by `componentDid(Un)Mount`, handles the `eventA` event response
   */
  handleEventA = () => {
    事件处理逻辑....
  }

分发事件:

   /**
     * Emits the `eventA` event, used to tell all the other cells to
     * handleeventA
     */
    emitAEvent = () => {
        const eventA = new Event('eventA')
        window.document.dispatchEvent(eventA)
    }

可以在点击时候,调用emitAEvent方法,来广播事件。

shouldComponentUpdate 返回一个布尔值。在组件接收到新的props或者state时被调用。在初始化时或者使用forceUpdate时不被调用。
可以在你确认不需要更新组件时使用。

以下是个老外的代码:

  /**
   * Performance lifesaver as the cell not touched by a change can
   * decide to avoid a rerender
   */
  shouldComponentUpdate(nextProps, nextState) {
    // Has a formula value? could be affected by any change. Update
    if (this.state.value !== '' && this.state.value.slice(0, 1) === '=') {
      return true
    }

componentWillUpdate在组件接收到新的props或者state但还没有render时被调用。在初始化时不会被调用。初始化的时候,不需要执行,只在非初始化的情况下应用。计算表中定义的公式的时候,可以在这当中执行。

ps1:forceUpdate() 

:By default, when your component’s state or props change, your component will re-render. If your render() method depends on some other data, you can tell React that the component needs re-rendering by calling forceUpdate().

ps2;Tutorial: create a Spreadsheet using React

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值