Skip to content

Commit

Permalink
bugfixed: The bug that sometimes copy command no succeed.
Browse files Browse the repository at this point in the history
  • Loading branch information
hiyangguo committed Jul 18, 2017
1 parent b64e66a commit 7ff261c
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -12,8 +12,10 @@

## Clipboard.copy
If you don't want to use `Clipboard`, you can execute this function to copy text in clipboard.

```
@param {String} text - Text to be copied to clipboard.
@param {Element} prevElem - The elements before the fake elements,defalut value is document.body.firstElementChild.
@return {Boolean} - Whether copy success.
```

Expand Down
1 change: 1 addition & 0 deletions example/md/props.md
Expand Up @@ -10,6 +10,7 @@
If you don't want to use `Clipboard`, you can execute this function to copy text in clipboard.
```
@param {String} text - Text to be copied to clipboard.
@param {Element} prevElem - The elements before the fake elements,defalut value is document.body.firstElementChild.
@return {Boolean} - Whether copy success.
```

Expand Down
6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "rsuite-clipboard",
"version": "1.1.2",
"version": "1.1.3",
"description": "",
"main": "lib/main.js",
"scripts": {
Expand All @@ -23,7 +23,8 @@
},
"homepage": "https://github.com/rsuite/rsuite-clipboard#readme",
"peerDependencies": {
"react": ">=0.14.0 || ^15.0.0"
"react": ">=0.14.0 || ^15.0.0",
"react-dom": "^0.14.8"
},
"devDependencies": {
"babel-cli": "^6.16.0",
Expand Down Expand Up @@ -54,7 +55,6 @@
"markdown-loader": "^0.1.7",
"markdownloader": "^1.0.5",
"react": "^0.14.8",
"react-dom": "^0.14.8",
"react-markdown": "^2.4.2",
"react-notification-system": "^0.2.13",
"react-router": "^3.0.0",
Expand Down
10 changes: 5 additions & 5 deletions src/component/Clipboard.js
Expand Up @@ -12,10 +12,11 @@ class Clipboard extends Component {

constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
}

handleCopy(text) {
return copy(text);
handleCopy(text, prevDom) {
return copy(text, prevDom);
}

onClick(event) {
Expand All @@ -27,8 +28,7 @@ class Clipboard extends Component {

const elem = React.Children.only(children);
const { onClick } = elem.props;
const result = this.handleCopy(text);

const result = this.handleCopy(text, event.target);
onCopy && onCopy(text, result);
onClick && onClick(event);
}
Expand All @@ -44,7 +44,7 @@ class Clipboard extends Component {

return React.cloneElement(elem, {
...props,
onClick: this.onClick.bind(this)
onClick: this.onClick
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/copy.js
@@ -1,6 +1,6 @@
import { execCommandCopy, select } from './index';

export default function copy(text) {
export default function copy(text, prevElem = document.body.firstChild) {
const isRTL = document.documentElement.getAttribute('dir') === 'rtl';

const fakeElem = document.createElement('textarea');
Expand All @@ -23,7 +23,7 @@ export default function copy(text) {

fakeElem.setAttribute('readonly', '');
fakeElem.value = text;
document.body.appendChild(fakeElem);
prevElem && prevElem.after(fakeElem);
select(fakeElem);
const result = execCommandCopy();
fakeElem.remove();
Expand Down

0 comments on commit 7ff261c

Please sign in to comment.