nandanself
nandanself

Reputation: 835

Add action to enter in ember textarea input helper

I have used addon ember-autoresize for my textarea to resize my textarea.

{{textarea type="text" placeholder="Comment" value=comment_text autofocus="autofocus" rows=1 max-rows=4 autoresize=true 
      enter="commentSave"}}

I want to trigger the action when user press enter.But it moves to next line when I press enter.How do I call the action when enter key is pressed in textarea.

Upvotes: 2

Views: 1817

Answers (1)

Daniel
Daniel

Reputation: 18692

Create component called custom-textarea.

in components/custom-textarea.js:

export default Ember.TextArea.extend({
  didRender() {
    this.$().keypress(function(event) {
      if (event.keyCode == 13) {
        event.preventDefault();
      }
    });
  }
});

In template, use custom-textarea instead of textarea:

{{custom-textarea type="text" placeholder="Comment" value=comment_text autofocus="autofocus" rows=1 max-rows=4 autoresize=true 
      enter="commentSave"}}

See WORKING DEMO.

Approach to prevent default behavior taken from this answer.

Upvotes: 3

Related Questions