-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathRouteRenderingMixin.js
53 lines (42 loc) · 1.7 KB
/
RouteRenderingMixin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"use strict";
var React = require('react');
var PropTypes = require('prop-types');
var assign = Object.assign || require('object-assign');
/**
* Mixin for routers which implements the simplest rendering strategy.
*/
var RouteRenderingMixin = {
propTypes: {
childProps: PropTypes.object
},
// Props passed at the `childProps` key are passed to all handlers.
getChildProps: function() {
var childProps = this.props.childProps || {};
// Merge up from parents, with inner props taking priority.
var parent = this.getParentRouter();
if (parent) {
childProps = assign({}, parent.getChildProps(), childProps);
}
return childProps;
},
renderRouteHandler: function() {
var match = this.getMatch();
if (!match.route) {
throw new Error("React-router-component: No route matched! Did you define a NotFound route?");
}
var handler = match.getHandler();
var isDOMElement = typeof handler.type === 'string';
// If this is a DOM element, don't send these props. This won't prevent all
// warnings in 15.2.0, but it will catch a lot of them.
var matchProps = isDOMElement ? null : this.getMatchProps();
var outProps = assign({ref: match.route.ref}, this.getChildProps(), matchProps);
// If we were passed an element, we need to clone it before passing it along.
if (React.isValidElement(handler)) {
// Be sure to keep the props that were already set on the handler.
// Otherwise, a handler like <div className="foo">bar</div> would have its className lost.
return React.cloneElement(handler, assign(outProps, handler.props));
}
return React.createElement(handler, outProps);
}
};
module.exports = RouteRenderingMixin;