react-case-when
Version:
Small utility module for conditional rendering in React
32 lines (20 loc) • 778 B
JavaScript
;
exports.__esModule = true;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _match = require('./match');
var _match2 = _interopRequireDefault(_match);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function Switch(_ref) {
var children = _ref.children;
var matched = null;
_react2.default.Children.forEach(children, function (child) {
var condition = !matched && _react2.default.isValidElement(child) && (0, _match2.default)(child.props.when);
if (condition) {
matched = child;
}
});
return matched ? _react2.default.cloneElement(matched, null, matched.props.children) : matched;
}
exports.default = Switch;
module.exports = exports['default'];