'use strict'; exports.__esModule = true; var _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; }; exports.default = createUncontrollable; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _invariant = require('invariant'); var _invariant2 = _interopRequireDefault(_invariant); var _utils = require('./utils'); var utils = _interopRequireWildcard(_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)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function createUncontrollable(mixins, set) { return uncontrollable; function uncontrollable(Component, controlledValues) { var methods = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2]; var displayName = Component.displayName || Component.name || 'Component', basePropTypes = utils.getType(Component).propTypes, isCompositeComponent = utils.isReactComponent(Component), controlledProps = Object.keys(controlledValues), propTypes; var OMIT_PROPS = ['valueLink', 'checkedLink'].concat(controlledProps.map(utils.defaultKey)); propTypes = utils.uncontrolledPropTypes(controlledValues, basePropTypes, displayName); (0, _invariant2.default)(isCompositeComponent || !methods.length, '[uncontrollable] stateless function components cannot pass through methods ' + 'because they have no associated instances. Check component: ' + displayName + ', ' + 'attempting to pass through methods: ' + methods.join(', ')); methods = utils.transform(methods, function (obj, method) { obj[method] = function () { var _refs$inner; return (_refs$inner = this.refs.inner)[method].apply(_refs$inner, arguments); }; }, {}); var component = _react2.default.createClass(_extends({ displayName: 'Uncontrolled(' + displayName + ')', mixins: mixins, propTypes: propTypes }, methods, { componentWillMount: function componentWillMount() { var _this = this; var props = this.props; this._values = {}; controlledProps.forEach(function (key) { _this._values[key] = props[utils.defaultKey(key)]; }); }, /** * If a prop switches from controlled to Uncontrolled * reset its value to the defaultValue */ componentWillReceiveProps: function componentWillReceiveProps(nextProps) { var _this2 = this; var props = this.props; controlledProps.forEach(function (key) { if (utils.getValue(nextProps, key) === undefined && utils.getValue(props, key) !== undefined) { _this2._values[key] = nextProps[utils.defaultKey(key)]; } }); }, getControlledInstance: function getControlledInstance() { return this.refs.inner; }, render: function render() { var _this3 = this; var newProps = {}, props = omitProps(this.props); utils.each(controlledValues, function (handle, propName) { var linkPropName = utils.getLinkName(propName), prop = _this3.props[propName]; if (linkPropName && !isProp(_this3.props, propName) && isProp(_this3.props, linkPropName)) { prop = _this3.props[linkPropName].value; } newProps[propName] = prop !== undefined ? prop : _this3._values[propName]; newProps[handle] = setAndNotify.bind(_this3, propName); }); newProps = _extends({}, props, newProps, { ref: isCompositeComponent ? 'inner' : null }); return _react2.default.createElement(Component, newProps); } })); component.ControlledComponent = Component; /** * useful when wrapping a Component and you want to control * everything */ component.deferControlTo = function (newComponent) { var additions = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; var nextMethods = arguments[2]; return uncontrollable(newComponent, _extends({}, controlledValues, additions), nextMethods); }; return component; function setAndNotify(propName, value) { var linkName = utils.getLinkName(propName), handler = this.props[controlledValues[propName]]; if (linkName && isProp(this.props, linkName) && !handler) { handler = this.props[linkName].requestChange; } for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { args[_key - 2] = arguments[_key]; } set(this, propName, handler, value, args); } function isProp(props, prop) { return props[prop] !== undefined; } function omitProps(props) { var result = {}; utils.each(props, function (value, key) { if (OMIT_PROPS.indexOf(key) === -1) result[key] = value; }); return result; } } } module.exports = exports['default'];