UNPKG

7.75 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _react = require('react');
8
9var _react2 = _interopRequireDefault(_react);
10
11var _clipboard = require('clipboard');
12
13var _clipboard2 = _interopRequireDefault(_clipboard);
14
15var _classnames = require('classnames');
16
17var _classnames2 = _interopRequireDefault(_classnames);
18
19var _beeIcon = require('bee-icon');
20
21var _beeIcon2 = _interopRequireDefault(_beeIcon);
22
23var _reactDom = require('react-dom');
24
25var _reactDom2 = _interopRequireDefault(_reactDom);
26
27var _beeTooltip = require('bee-tooltip');
28
29var _beeTooltip2 = _interopRequireDefault(_beeTooltip);
30
31var _propTypes = require('prop-types');
32
33var _propTypes2 = _interopRequireDefault(_propTypes);
34
35var _tool = require('bee-locale/build/tool');
36
37var _i18n = require('./i18n.js');
38
39var _i18n2 = _interopRequireDefault(_i18n);
40
41var _beeModal = require('bee-modal');
42
43var _beeModal2 = _interopRequireDefault(_beeModal);
44
45var _beeFormControl = require('bee-form-control');
46
47var _beeFormControl2 = _interopRequireDefault(_beeFormControl);
48
49var _beeButton = require('bee-button');
50
51var _beeButton2 = _interopRequireDefault(_beeButton);
52
53function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
54
55function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
56
57function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
58
59function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
60
61function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
62
63//text和target都写的时候,target无效。 text的cut改为copy。
64// target可以传css3选择器
65var propTypes = {
66 action: _propTypes2["default"].oneOf(['copy', 'cut', null]),
67 text: _propTypes2["default"].string,
68 success: _propTypes2["default"].func,
69 error: _propTypes2["default"].func,
70 locale: _propTypes2["default"].object
71};
72var defaultProps = {
73 action: 'copy',
74 text: '',
75 target: '',
76 success: function success() {},
77 error: function error() {},
78 locale: {}
79};
80
81var Clipboard = function (_Component) {
82 _inherits(Clipboard, _Component);
83
84 function Clipboard(props, context) {
85 _classCallCheck(this, Clipboard);
86
87 var _this = _possibleConstructorReturn(this, _Component.call(this, props, context));
88
89 _this.blur = function () {
90 _this.setState({
91 currect: false,
92 ready: false
93 });
94 };
95
96 _this.close = function () {
97 _this.setState({
98 modalShow: false
99 });
100 };
101
102 _this.state = {
103 currect: false,
104 html: '',
105 ready: false,
106 id: 'id' + Math.round(Math.random() * 1000 + 1) + new Date().getTime(),
107 modalShow: false
108 };
109 return _this;
110 }
111
112 Clipboard.prototype.componentWillMount = function componentWillMount() {
113 var self = this;
114 var _props = this.props,
115 success = _props.success,
116 error = _props.error;
117
118
119 var id = this.state.id;
120 var cb = new _clipboard2["default"]('#' + id);
121 cb.on('success', function (e) {
122 self.setState({
123 currect: true,
124 ready: true
125 });
126 e.clearSelection();
127 if (success instanceof Function) success();
128 });
129 cb.on('error', function (e) {
130 self.setState({
131 modalShow: true,
132 html: e.text
133 });
134 _reactDom2["default"].findDOMNode(self.refs.text).select();
135 if (error instanceof Function) error();
136 });
137 };
138
139 Clipboard.prototype.render = function render() {
140 var _props2 = this.props,
141 action = _props2.action,
142 text = _props2.text,
143 target = _props2.target;
144
145 if (text) action = 'copy';
146
147 var locale = (0, _tool.getComponentLocale)(this.props, this.context, 'Clipboard', function () {
148 return _i18n2["default"];
149 });
150 var tootipContent = locale[action];
151 if (this.state.ready) {
152 tootipContent = locale[action + 'Ready'];
153 }
154
155 return _react2["default"].createElement(
156 _beeTooltip2["default"],
157 { className: 'u-clipboard-tooltip',
158 positionTop: '20px',
159 overlay: tootipContent,
160 placement: 'top' },
161 _react2["default"].createElement(
162 'span',
163 {
164 onMouseOut: this.blur,
165 className: 'u-clipboard',
166 id: this.state.id,
167 'data-clipboard-action': action,
168 'data-clipboard-target': target,
169 'data-clipboard-text': text },
170 this.props.children ? this.props.children : _react2["default"].createElement(_beeIcon2["default"], {
171 className: (0, _classnames2["default"])({
172 'uf-correct': this.state.currect,
173 'uf-copy': !this.state.currect
174 })
175 }),
176 _react2["default"].createElement(
177 _beeModal2["default"],
178 { show: this.state.modalShow, onHide: this.close },
179 _react2["default"].createElement(
180 _beeModal2["default"].Header,
181 { closeButton: true },
182 _react2["default"].createElement(
183 _beeModal2["default"].Title,
184 null,
185 ' Ctrl+C ',
186 locale['copyToClipboard'],
187 ' '
188 )
189 ),
190 _react2["default"].createElement(
191 _beeModal2["default"].Body,
192 null,
193 _react2["default"].createElement(_beeFormControl2["default"], { ref: 'text', type: 'text', readOnly: true, value: this.state.html })
194 ),
195 _react2["default"].createElement(
196 _beeModal2["default"].Footer,
197 null,
198 _react2["default"].createElement(
199 _beeButton2["default"],
200 { onClick: this.close },
201 ' ',
202 locale['close'],
203 ' '
204 )
205 )
206 )
207 )
208 );
209 };
210
211 return Clipboard;
212}(_react.Component);
213
214;
215Clipboard.propTypes = propTypes;
216Clipboard.defaultProps = defaultProps;
217exports["default"] = Clipboard;
218module.exports = exports['default'];
\No newline at end of file