1. class Clipboard extends PureComponent{
    2. static defaultProps = {
    3. onSuccess: () => {
    4. message.success('复制成功');
    5. },
    6. onError: () => {
    7. message.error('当前浏览器不支持此功能,请手动复制内容');
    8. },
    9. tooltipProps: {}
    10. }
    11. componentDidMound() {
    12. const clipboard = new ClipboardJS(ReactDOM.findDOMNode(this));
    13. const { onSuccess, onError } = this.props;
    14. clipboard.on('success', () => {
    15. onSuccess(clipboard);
    16. });
    17. clipboard.on('error', () => {
    18. onError(clipboard);
    19. });
    20. this.clipboard = clipboard;
    21. }
    22. componentWillUnmound() {
    23. this.clipboard && this.clipboard.destroy();
    24. }
    25. render() {
    26. const {
    27. children,
    28. component,
    29. text,
    30. target,
    31. tooltip,
    32. tooltipProps,
    33. onSuccess,
    34. onError,
    35. icon,
    36. ...restProps,
    37. } = this.props;
    38. const commonProps = {
    39. 'data-clipboard-target': target,
    40. 'data-clipboard-text': text,
    41. }
    42. const compProps = {
    43. ...restProps,
    44. ...commonProps,
    45. }
    46. let renderComponent;
    47. switch(component) {
    48. case 'button':
    49. renderComponent = <Button {...compProps}>{children}</Button>;
    50. break;
    51. case 'icon':
    52. renderComponent = <TextBtn {...commonProps}>{icon}</TextBtn>;
    53. break;
    54. case 'text':
    55. renderComponent = <TextBtn {...compProps}>{children}</TextBtn>;
    56. break;
    57. default:
    58. renderComponent = React.cloneElement(React.Children.only(children), {
    59. ...commonProps,
    60. ...(children.props || {}),
    61. });
    62. }
    63. return tooltip ? (
    64. <Tooltip {...tooltipProps} title={tooltip}>{renderComponent}</Tooltip>
    65. ): renderComponent
    66. }
    67. }