Prevent default onpaste
Asked Answered
Z

1

6

I am trying to build a custom paste event in React. I have a problem though that if I use the React events the event.preventDefault() is run after the default action already occurred.

Here is the code:

render() {
return (
  <div
  className='compositionText'
  onPasteCapture={this.handlePaste}>
  </div>
);
}

I have succeeded in doing the same with DOM event listeners:

componentDidMount() {
this.getDOMNode().addEventListener('paste', this.handlePaste, true);
},

Can anyone tell me why the first solution doesn't work and how I can achieve this in the React way?

UPDATE: I want to add that I am using the Trix editor within that div, if that changes anything.

Zworykin answered 23/12, 2015 at 22:32 Comment(1)
It looks like it maybe an issue with the Trix editor, I was able to get a working version of onPasteCapture here, jsfiddle.net/Pyloid/69z2wepo/25126 Open dev tools and paste, content of the capture gets logged and paste is prevented.Marathi
M
5

Looks like it might be an issue with the Trix editor as I was able to get a working version of onPasteCapture here, https://jsfiddle.net/Pyloid/69z2wepo/25126/

var Hello = React.createClass({
    handlePaste: function(e) {
        e.preventDefault();
      console.log(e);
    },

        render: function() {
        return <input type="text" onPasteCapture={this.handlePaste} />;
    }
});

ReactDOM.render(
    <Hello name="World" />,
  document.getElementById('container')
);

Open the control panel and see the content of the capture logged out.

Marathi answered 25/12, 2015 at 21:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.