Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select text to copy without triggering click event in reactjs

Tags:

reactjs

events

I am using react-table. I have defined onRowClick() function for a column. Here select text should highlight the text and clicking have to redirect to another page. Now when I try to select the text, its getting redirected. How to select text without triggering click event?

Following is my onRowClick function:

onRowClick = (state, rowInfo, columnInfo) => {
  return {
    onClick: (e, handleOriginal) => {
      if (columnInfo.id) {
        this.props.history.push(`/downloads/${rowInfo.original.id}`);
      } else if (handleOriginal) {
        handleOriginal();
      }
    }
  };
}

The following is my react-table component:

<ReactTable
  manual
  getTdProps = {this.onRowClick}
  data = {results}
  onFetchData = {this.onFetchData}
  sortable = {false}
  showPagination = {false}
  noDataText = 'no data found'
  columns = {[
   {
     Header: 'Id',
     maxWidth: 50,
     accessor: "id",
     Cell: (props) => <span className="btn-link pointer">{props.value} </span>
   },
   {
     Header: 'Processed on',
     maxWidth: 165,
     accessor: "created_at",
     Cell: (props) => <span> {this.getDateTime(props.value)} </span>
   }
  ]
/>

Clicking on id column should redirect to the details page. Selecting text should select the id text.

like image 644
Beu Avatar asked Jun 28 '19 05:06

Beu


People also ask

How do I make a button clickable in react?

The first one uses navigator.clipboard.writeText and the useState hook. The second one uses a third-party library. The React app we are going to build is simple. It contains a text field and a button. When the text field is empty, the button is disabled. When you type something into the text field, the button will become clickable.

How to copy text from one page to the clipboard using React?

Create react app First, we will have a simple react application. For that use the following command to set up the startup react application. 2. Design a page To design a page, we will use the textarea for entering the text and button for copy to clipboard. App.js 3. Implement logic for copy text to the clipboard

How to cut something in react and typescript?

You can cut something in several ways: Right click to display the context mene then select “Cut” from it. To have a better understanding of these events in the React and TypeScript world, please see the example below. The sample app we are going to build contains an input element and a textarea. The text in the input element can be copied or cut:

What are the oncopy and oncut events in react and typescript?

This article is about the onCopy, onCut, and onPaste events in React and TypeScript. We’ll cover the fundamentals of these three events then examine a complete example of using them in practice. The onCopy event fires when the user copies an element or the content of an element such as text, images.


1 Answers

I think onclick cannot be prevented but your desired result can be obtained by using Window.getSelection() method.

The Window.getSelection() method returns a Selection object representing the range of text selected by the user or the current position of the caret.

By using this method you can get the selected text and then you can calculate its length as:

window.getSelection().toString()

And then you can modify your onRowClick method as given below:

onRowClick = (state, rowInfo, columnInfo) => {
    return {
        onClick: (e, handleOriginal) => {
            let selection = window.getSelection().toString();
            if(selection.length <= 0) {
                if (columnInfo.id && selection.length > 0) {
                    console.log("columnInfo.id", columnInfo.id);
                    this.props.history.push(`/downloads/${rowInfo.original.id}`);
                } else if (handleOriginal) {
                    handleOriginal();
                    console.log("columnInfo.id", "nothing");
                }
            }
        }
    };
};

I have created a working demo.

like image 195
Triyugi Narayan Mani Avatar answered Nov 15 '22 10:11

Triyugi Narayan Mani