sc-react-ions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
138 lines (110 loc) • 6.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _clipboard = require('clipboard');
var _clipboard2 = _interopRequireDefault(_clipboard);
var _Tooltip = require('../Tooltip');
var _Tooltip2 = _interopRequireDefault(_Tooltip);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _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; }
function _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) : subClass.__proto__ = superClass; }
var EnhanceWithCopy = function EnhanceWithCopy(WrappedComponent) {
var _class, _temp2;
return _temp2 = _class = function (_React$Component) {
_inherits(_class, _React$Component);
function _class() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, _class);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = _class.__proto__ || Object.getPrototypeOf(_class)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
copied: false,
showTooltip: false
}, _this.shouldComponentUpdate = function (nextProps, nextState) {
if (nextState.copied !== _this.state.copied) return true;
if (nextState.showTooltip !== _this.state.showTooltip) return true;
return false;
}, _this.componentDidMount = function () {
// When the component mounts configure the clipboard
_this.activateCopyToClipboard();
}, _this.componentWillUnmount = function () {
// When the component unmounts destroy the clipboard
_this._clipboard.destroy();
}, _this.getValue = function (trigger) {
// Set the local trigger variable
_this._trigger = trigger;
// If a custom getValue method is provided use it to get the value
if (_this.props.getValue) {
return _this.props.getValue(trigger);
}
switch (trigger.nodeName) {
case 'INPUT':
return trigger.value;
default:
return trigger.textContent;
}
}, _this.focusTriggerElement = function () {
// If the trigger element has a focus method call it
if (typeof _this._trigger.focus === 'function') {
_this._trigger.focus();
}
}, _this.activateCopyToClipboard = function () {
// Create the clipboard using the ID prop
_this._clipboard = new _clipboard2.default('#' + _this.props.id, { text: _this.getValue });
_this._clipboard.on('success', function () {
_this.handleCopy();
});
}, _this.handleCopy = function () {
// Set the copied state to true
_this.setState({ copied: true }, function () {
// The input gets blurred every time the component is updated so we need to
// focus the trigger element if it needs to be focused (input or textarea)
_this.focusTriggerElement();
setTimeout(function () {
// Set the copied state to false
_this.setState({ copied: false }, function () {
// The input gets blurred every time the component is updated so we need to
// focus the trigger element if it needs to be focused (input or textarea)
_this.focusTriggerElement();
});
}, 1800);
});
}, _this.handleMouseEnterLeave = function (event) {
// Set the showTooltip state to true on mouse enter so the tooltip doesn't disappear when its content changes
// Set the showTooltip state to false on mouse leave so the tooltip disappears when the mouse leaves the element
_this.setState({ showTooltip: event.type === 'mouseenter' });
}, _this.render = function () {
var _this$props = _this.props,
tooltipPlacement = _this$props.tooltipPlacement,
otherProps = _objectWithoutProperties(_this$props, ['tooltipPlacement']);
var tooltipText = _this.state.copied ? 'Copied!' : 'Click to copy';
var tooltipShow = _this.state.copied || _this.state.showTooltip;
return _react2.default.createElement(
'div',
{ onMouseEnter: _this.handleMouseEnterLeave, onMouseLeave: _this.handleMouseEnterLeave },
_react2.default.createElement(
_Tooltip2.default,
{ content: tooltipText, tooltipPlacement: tooltipPlacement, show: tooltipShow },
_react2.default.createElement(WrappedComponent, otherProps)
)
);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
return _class;
}(_react2.default.Component), _class.propTypes = {
getValue: _propTypes.func,
id: _propTypes.string.isRequired,
tooltipPlacement: _propTypes.string
}, _class.defaultProps = {
tooltipPlacement: 'top'
}, _temp2;
};
exports.default = EnhanceWithCopy;