5

I want to handle keyPressDown (enter) on inputfield, the function kinda works but I'm not able to type anything anymore.

handleSubmit: function (e) {
    e.preventDefault();
    if (e.keyCode === 13) { console.log('hit enter'); }
    else { return false; }
  },
render: function(){
    return (
   <div className="addTask">
   <form className="questionsTask" autoComplete="off">
   <ol className="questions">
    <li>
    <input id="taskName" name="taskName" type="text" placeholder="What's your next task?" onChange={this.handleTask.name} onKeyDown={this.handleSubmit}/>
   </li> ....

1 Answer 1

6

You need to handle the onChange. Do something like:

var TextInput = React.createClass({
    getInitialState: function() {
        return {text: ''};
    },
    inputSubmit: function() {
        console.log(this.refs.userInput.getDOMNode().value);
        this.setState({text: ''});
    },
    handleChange: function(e) {
        this.setState({text: e.target.value});
    },
    handleKeyDown: function(e) {
        if (e.keyCode === 13 ) {
            return this.inputSubmit();
        }
    },
    render: function() {
        return (<input value={this.state.text} ref="userInput" onChange={this.handleChange} onKeyDown={this.handleKeyDown}/>);
    }
});

via @spicyj (http://jsfiddle.net/spicyj/HdR6E/)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.