2

I try to compare entered key code on change event of input but when I try it, I only see Proxy object instead of event handler object. I couldn't find any solution. In another component I achieved it but in this component I can't.

My handler function is :

  handleChange(event){
    this.setState({
      searchValue : this.searchInput.value
    });
    if(this.searchInput.value.length>2&&!this.state.recommendation&&this.focused){
      this.setState({
        recommendation:true
      });
    } else if(this.searchInput.value.length<3&&this.state.recommendation&&this.focused) {
      this.setState({
        recommendation:false
      });
      return;
    }
    console.log(event) //event is returned as Proxy object
    if(event.keyCode === 13){
      this.context.router.history.push(`/yardim/arama/${this.searchInput.value}`);
      return;
    }

    if(this.searchInput.value.length>2){
      this.bouncer.handle();
    }
  }

and render function is :

render(){
    return(
      <div id="faq-search-box">
        {this.state.recommendation}
        <i className="icon hb-icon-search_icon"></i>
        <input id="faq-search-input" type="text" placeholder="Yardım konusu ara..." value={this.state.searchValue} ref={input=>{this.searchInput=input;}}  onChange={this.handleChange.bind(this)} onFocus={this.onFocus} onBlur={this.onBlur}  />
        <div className="clearfix"></div>
        { this.state.recommendation &&
        <div id="faq-recommendation-box">
          {this.props.FAQRecomendations&&
          <ul>
            {this.props.FAQRecomendations.map((faq)=>
              <li key={faq.id}><a onMouseDown={(e)=>{this.onMouseDown(`/yardim/${faq.slug}#${faq.id}`,e)}} title={faq.name}>{faq.name}</a></li>
            )}
          </ul>
          }
        </div>
        }

      </div>
    )
  }

What is the problem? How can I Access the event handler?

Kamuran Sönecek
  • 3,293
  • 2
  • 30
  • 57

2 Answers2

5

The solution which I found is that. The problems is occured because of SyntheticEvent.

There is an object on Proxy object as called nativeEvent.

Thanks of it I achieved the keyCode.

For Example:

... 
    if(event.nativeEvent.keyCode === 13){
          this.context.router.history.push(`/yardim/arama/${this.searchInput.value}`);
          return;
        }
...
Kamuran Sönecek
  • 3,293
  • 2
  • 30
  • 57
1

Please try to listen onKeyDown event. Here is the demo.

class Demo extends React.Component {
   constructor() {
      super()
      this.state = {
        message:''
      }
   }
   
   onKeyDown(e) {
    if (e.keyCode === 13) {
     console.log('Enter Key Pressed');
    }
  }
  
  handleChange(e) {
    this.setState({
     message: e.target.value
    });
  }
  
  render() {
    return <div>
         <input
              value={this.state.message}
              onChange={this.handleChange.bind(this)}
              onKeyDown={this.onKeyDown}
            />
        </div>;
  }
}

ReactDOM.render(
  <Demo />,
  document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"/>
Dev
  • 3,922
  • 3
  • 24
  • 44