162 lines
6.0 KiB
JavaScript
162 lines
6.0 KiB
JavaScript
|
|
"use strict";
|
||
|
|
|
||
|
|
exports.__esModule = true;
|
||
|
|
exports.default = uncontrollable;
|
||
|
|
|
||
|
|
var _react = _interopRequireDefault(require("react"));
|
||
|
|
|
||
|
|
var _invariant = _interopRequireDefault(require("invariant"));
|
||
|
|
|
||
|
|
var Utils = _interopRequireWildcard(require("./utils"));
|
||
|
|
|
||
|
|
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
||
|
|
|
||
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
|
||
|
|
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
||
|
|
|
||
|
|
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
|
||
|
|
|
||
|
|
function uncontrollable(Component, controlledValues, methods) {
|
||
|
|
if (methods === void 0) {
|
||
|
|
methods = [];
|
||
|
|
}
|
||
|
|
|
||
|
|
var displayName = Component.displayName || Component.name || 'Component';
|
||
|
|
var isCompositeComponent = Utils.isReactComponent(Component);
|
||
|
|
var controlledProps = Object.keys(controlledValues);
|
||
|
|
var PROPS_TO_OMIT = controlledProps.map(Utils.defaultKey);
|
||
|
|
!(isCompositeComponent || !methods.length) ? process.env.NODE_ENV !== "production" ? (0, _invariant.default)(false, '[uncontrollable] stateless function components cannot pass through methods ' + 'because they have no associated instances. Check component: ' + displayName + ', ' + 'attempting to pass through methods: ' + methods.join(', ')) : invariant(false) : void 0;
|
||
|
|
|
||
|
|
var UncontrolledComponent =
|
||
|
|
/*#__PURE__*/
|
||
|
|
function (_React$Component) {
|
||
|
|
_inheritsLoose(UncontrolledComponent, _React$Component);
|
||
|
|
|
||
|
|
function UncontrolledComponent() {
|
||
|
|
var _this;
|
||
|
|
|
||
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
||
|
|
args[_key] = arguments[_key];
|
||
|
|
}
|
||
|
|
|
||
|
|
_this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
|
||
|
|
_this.handlers = Object.create(null);
|
||
|
|
controlledProps.forEach(function (propName) {
|
||
|
|
var handlerName = controlledValues[propName];
|
||
|
|
|
||
|
|
var handleChange = function handleChange(value) {
|
||
|
|
if (_this.props[handlerName]) {
|
||
|
|
var _this$props;
|
||
|
|
|
||
|
|
_this._notifying = true;
|
||
|
|
|
||
|
|
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
||
|
|
args[_key2 - 1] = arguments[_key2];
|
||
|
|
}
|
||
|
|
|
||
|
|
(_this$props = _this.props)[handlerName].apply(_this$props, [value].concat(args));
|
||
|
|
|
||
|
|
_this._notifying = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
_this._values[propName] = value;
|
||
|
|
if (!_this.unmounted) _this.forceUpdate();
|
||
|
|
};
|
||
|
|
|
||
|
|
_this.handlers[handlerName] = handleChange;
|
||
|
|
});
|
||
|
|
if (isCompositeComponent) _this.attachRef = function (ref) {
|
||
|
|
_this.inner = ref;
|
||
|
|
};
|
||
|
|
return _this;
|
||
|
|
}
|
||
|
|
|
||
|
|
var _proto = UncontrolledComponent.prototype;
|
||
|
|
|
||
|
|
_proto.shouldComponentUpdate = function shouldComponentUpdate() {
|
||
|
|
//let the forceUpdate trigger the update
|
||
|
|
return !this._notifying;
|
||
|
|
};
|
||
|
|
|
||
|
|
_proto.componentWillMount = function componentWillMount() {
|
||
|
|
var _this2 = this;
|
||
|
|
|
||
|
|
var props = this.props;
|
||
|
|
this._values = Object.create(null);
|
||
|
|
controlledProps.forEach(function (key) {
|
||
|
|
_this2._values[key] = props[Utils.defaultKey(key)];
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
_proto.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
|
||
|
|
var _this3 = this;
|
||
|
|
|
||
|
|
var props = this.props;
|
||
|
|
controlledProps.forEach(function (key) {
|
||
|
|
/**
|
||
|
|
* If a prop switches from controlled to Uncontrolled
|
||
|
|
* reset its value to the defaultValue
|
||
|
|
*/
|
||
|
|
if (!Utils.isProp(nextProps, key) && Utils.isProp(props, key)) {
|
||
|
|
_this3._values[key] = nextProps[Utils.defaultKey(key)];
|
||
|
|
}
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
_proto.componentWillUnmount = function componentWillUnmount() {
|
||
|
|
this.unmounted = true;
|
||
|
|
};
|
||
|
|
|
||
|
|
_proto.getControlledInstance = function getControlledInstance() {
|
||
|
|
return this.inner;
|
||
|
|
};
|
||
|
|
|
||
|
|
_proto.render = function render() {
|
||
|
|
var _this4 = this;
|
||
|
|
|
||
|
|
var props = _extends({}, this.props);
|
||
|
|
|
||
|
|
PROPS_TO_OMIT.forEach(function (prop) {
|
||
|
|
delete props[prop];
|
||
|
|
});
|
||
|
|
var newProps = {};
|
||
|
|
controlledProps.forEach(function (propName) {
|
||
|
|
var propValue = _this4.props[propName];
|
||
|
|
newProps[propName] = propValue !== undefined ? propValue : _this4._values[propName];
|
||
|
|
});
|
||
|
|
return _react.default.createElement(Component, _extends({}, props, newProps, this.handlers, {
|
||
|
|
ref: this.attachRef
|
||
|
|
}));
|
||
|
|
};
|
||
|
|
|
||
|
|
return UncontrolledComponent;
|
||
|
|
}(_react.default.Component);
|
||
|
|
|
||
|
|
UncontrolledComponent.displayName = "Uncontrolled(" + displayName + ")";
|
||
|
|
UncontrolledComponent.propTypes = Utils.uncontrolledPropTypes(controlledValues, displayName);
|
||
|
|
methods.forEach(function (method) {
|
||
|
|
UncontrolledComponent.prototype[method] = function $proxiedMethod() {
|
||
|
|
var _inner;
|
||
|
|
|
||
|
|
return (_inner = this.inner)[method].apply(_inner, arguments);
|
||
|
|
};
|
||
|
|
});
|
||
|
|
UncontrolledComponent.ControlledComponent = Component;
|
||
|
|
/**
|
||
|
|
* useful when wrapping a Component and you want to control
|
||
|
|
* everything
|
||
|
|
*/
|
||
|
|
|
||
|
|
UncontrolledComponent.deferControlTo = function (newComponent, additions, nextMethods) {
|
||
|
|
if (additions === void 0) {
|
||
|
|
additions = {};
|
||
|
|
}
|
||
|
|
|
||
|
|
return uncontrollable(newComponent, _extends({}, controlledValues, additions), nextMethods);
|
||
|
|
};
|
||
|
|
|
||
|
|
return UncontrolledComponent;
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = exports["default"];
|