class Clipboard extends PureComponent{ static defaultProps = { onSuccess: () => { message.success('复制成功'); }, onError: () => { message.error('当前浏览器不支持此功能,请手动复制内容'); }, tooltipProps: {} } componentDidMound() { const clipboard = new ClipboardJS(ReactDOM.findDOMNode(this)); const { onSuccess, onError } = this.props; clipboard.on('success', () => { onSuccess(clipboard); }); clipboard.on('error', () => { onError(clipboard); }); this.clipboard = clipboard; } componentWillUnmound() { this.clipboard && this.clipboard.destroy(); } render() { const { children, component, text, target, tooltip, tooltipProps, onSuccess, onError, icon, ...restProps, } = this.props; const commonProps = { 'data-clipboard-target': target, 'data-clipboard-text': text, } const compProps = { ...restProps, ...commonProps, } let renderComponent; switch(component) { case 'button': renderComponent = <Button {...compProps}>{children}</Button>; break; case 'icon': renderComponent = <TextBtn {...commonProps}>{icon}</TextBtn>; break; case 'text': renderComponent = <TextBtn {...compProps}>{children}</TextBtn>; break; default: renderComponent = React.cloneElement(React.Children.only(children), { ...commonProps, ...(children.props || {}), }); } return tooltip ? ( <Tooltip {...tooltipProps} title={tooltip}>{renderComponent}</Tooltip> ): renderComponent }}