passing-through
Version:
Pass properties and modifier functions to child context's which can modify children
119 lines (98 loc) • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Pass = Pass;
exports.Through = Through;
exports.Consumer = exports.Provider = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
/**
* Context used to shared the data.
*
* @private
*/
var _createContext = (0, _react.createContext)({
passed: function passed() {
return {};
},
modify: function modify() {
return {};
}
}),
Provider = _createContext.Provider,
Consumer = _createContext.Consumer;
/**
* Pass properties through context to child components so they can change
* props on the fly.
*
* @param {Object} props The properties of the component.
* @public
*/
exports.Consumer = Consumer;
exports.Provider = Provider;
function Pass(props) {
var children = props.children,
modify = props.modify,
passed = _objectWithoutProperties(props, ["children", "modify"]);
return _react["default"].createElement(Provider, {
value: {
passed: passed,
modify: modify
}
}, children);
}
/**
* Validate that we've received all required properties in order to function.
*
* @type {Object}
* @private
*/
Pass.propTypes = {
children: _propTypes["default"].node.isRequired,
modify: _propTypes["default"].object
};
/**
* Default properties.
*
* @type {Object};
*/
Pass.defaultProps = {
modify: {}
};
/**
* Preferres the passed through properties over their own supplied properties.
*
* @param {Object} props The properties of the component.
* @public
*/
function Through(props) {
return _react["default"].createElement(Consumer, null, function (_ref) {
var modify = _ref.modify,
passed = _ref.passed;
var children = props.children,
attributes = _objectWithoutProperties(props, ["children"]);
var child = _react.Children.only(children);
Object.keys(passed).forEach(function applyModifiers(name) {
var modifiers = modify[name];
if (!Array.isArray(modifiers)) return;
modifiers.forEach(function makeitso(modifier) {
child = modifier(attributes, passed, child) || child;
});
});
return (0, _react.cloneElement)(child, attributes);
});
}
/**
* Validate that we've received all required properties in order to function.
*
* @type {Object}
* @private
*/
Through.propTypes = {
children: _propTypes["default"].node.isRequired
};