zent
Version:
一套前端设计语言和基于React的实现
45 lines (44 loc) • 1.94 kB
JavaScript
import { __assign, __extends } from "tslib";
import { jsx as _jsx } from "react/jsx-runtime";
import { Component, Children } from 'react';
import Button from '../button';
import Notify from '../notify';
import { I18nReceiver as Receiver } from '../i18n';
import CopyToClipboard from './ReactCopyToClipboard';
var CopyButton = (function (_super) {
__extends(CopyButton, _super);
function CopyButton() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.onCopyCallback = function (type, callback) {
if (typeof callback === 'string') {
Notify[type](callback);
}
else {
callback();
}
};
_this.onCopy = function (i18n) { return function (text, result) {
var _a = _this.props, onCopySuccess = _a.onCopySuccess, onCopyError = _a.onCopyError;
if (result) {
_this.onCopyCallback('success', onCopySuccess || i18n.success);
}
else {
_this.onCopyCallback('error', onCopyError || i18n.error);
}
}; };
return _this;
}
CopyButton.prototype.render = function () {
var _this = this;
var _a = this.props, text = _a.text, children = _a.children, onClick = _a.onClick;
var txt = typeof text === 'function' ? text() : text;
return (_jsx(Receiver, __assign({ componentName: "CopyButton" }, { children: function (i18n) { return (_jsx(CopyToClipboard, __assign({ text: txt, onCopy: _this.onCopy(i18n) }, { children: children ? (Children.only(children)) : (_jsx(Button, __assign({ onClick: onClick, type: "primary" }, { children: i18n.copy }), void 0)) }), void 0)); } }), void 0));
};
CopyButton.defaultProps = {
onCopySuccess: '',
onCopyError: '',
};
return CopyButton;
}(Component));
export { CopyButton };
export default CopyButton;