react-stripe-elements
Version:
React components for Stripe.js and Stripe Elements
161 lines (120 loc) • 5.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _isEqual = require('../utils/isEqual');
var _isEqual2 = _interopRequireDefault(_isEqual);
var _Elements = require('./Elements');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
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; }
var noop = function noop() {};
var _extractOptions = function _extractOptions(props) {
var id = props.id,
className = props.className,
onChange = props.onChange,
onFocus = props.onFocus,
onBlur = props.onBlur,
onReady = props.onReady,
options = _objectWithoutProperties(props, ['id', 'className', 'onChange', 'onFocus', 'onBlur', 'onReady']);
return options;
};
var capitalized = function capitalized(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};
var Element = function Element(type) {
var _class, _temp;
var hocOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _temp = _class = function (_React$Component) {
_inherits(_class, _React$Component);
function _class(props, context) {
_classCallCheck(this, _class);
var _this = _possibleConstructorReturn(this, _React$Component.call(this, props, context));
_this.handleRef = function (ref) {
_this._ref = ref;
};
_this._element = null;
var options = _extractOptions(_this.props);
// We keep track of the extracted options on this._options to avoid re-rendering.
// (We would unnecessarily re-render if we were tracking them with state.)
_this._options = options;
return _this;
}
_class.prototype.componentDidMount = function componentDidMount() {
var _this2 = this;
this.context.addElementsLoadListener(function (elements) {
if (!_this2._ref) {
return;
}
var element = elements.create(type, _this2._options);
_this2._element = element;
_this2._setupEventListeners(element);
element.mount(_this2._ref);
// Register Element for automatic token / source / paymentMethod creation
_this2.context.registerElement(element, hocOptions.impliedTokenType, hocOptions.impliedSourceType, hocOptions.impliedPaymentMethodType);
});
};
_class.prototype.componentDidUpdate = function componentDidUpdate() {
var options = _extractOptions(this.props);
if (Object.keys(options).length !== 0 && !(0, _isEqual2.default)(options, this._options)) {
this._options = options;
if (this._element) {
this._element.update(options);
}
}
};
_class.prototype.componentWillUnmount = function componentWillUnmount() {
if (this._element) {
var element = this._element;
element.destroy();
this.context.unregisterElement(element);
}
};
_class.prototype._setupEventListeners = function _setupEventListeners(element) {
var _this3 = this;
element.on('ready', function () {
_this3.props.onReady(_this3._element);
});
element.on('change', function (change) {
_this3.props.onChange(change);
});
element.on('blur', function () {
var _props;
return (_props = _this3.props).onBlur.apply(_props, arguments);
});
element.on('focus', function () {
var _props2;
return (_props2 = _this3.props).onFocus.apply(_props2, arguments);
});
};
_class.prototype.render = function render() {
return _react2.default.createElement('div', {
id: this.props.id,
className: this.props.className,
ref: this.handleRef
});
};
return _class;
}(_react2.default.Component), _class.propTypes = {
id: _propTypes2.default.string,
className: _propTypes2.default.string,
onChange: _propTypes2.default.func,
onBlur: _propTypes2.default.func,
onFocus: _propTypes2.default.func,
onReady: _propTypes2.default.func
}, _class.defaultProps = {
id: undefined,
className: undefined,
onChange: noop,
onBlur: noop,
onFocus: noop,
onReady: noop
}, _class.contextTypes = _Elements.elementContextTypes, _class.displayName = capitalized(type) + 'Element', _temp;
};
exports.default = Element;