@@ -2,8 +2,8 @@ import {Component, createElement, PropTypes} from 'react';
22import hoistStatics from 'hoist-non-react-statics' ;
33import { connect } from 'react-redux' ;
44
5- import { getMergedComponents } from './selector' ;
6- import { removeComponent } from './action' ;
5+ import { getComponents } from './selector' ;
6+ import { removeComponent , updateComponent } from './action' ;
77import { componentsShape , renderMapShape , getDisplayName } from './util' ;
88
99/**
@@ -43,80 +43,36 @@ export default ({scope, ...defaultProps} = {}) => (WrappedComponent) => {
4343
4444 render ( ) {
4545 const { components, renderMap} = this . props . ___relocationState___ ;
46- const { removeComponent} = this . props . ___relocationDispatch___ ;
46+ const {
47+ removeComponent,
48+ updateComponent,
49+ } = this . props . ___relocationDispatch___ ;
4750
4851 const inRenderMap = ( component ) =>
4952 typeof renderMap [ component . type ] === 'function' ;
5053
51- const assignRender = ( component ) => ( {
52- ...component ,
53- render : renderMap [ component . type ] ,
54- } ) ;
55-
56- const assignScope = ( component ) => ( { ...component , scope} ) ;
57-
58- const assignRemoveHandler = ( component ) => {
59- let removeHandler = null ;
60-
61- if ( typeof component . remove === 'function' ) {
62- // The component object remove property is already a function.
63- // We don't want to override this behavior.
64- removeHandler = component . remove ;
65- } else if ( component . remove === undefined || component . remove ) {
66- // The component object does not have a `remove` property, or it has
67- // a truthy value that is not a function. Either case indicates that
68- // it should use the default remove handler.
69- removeHandler = ( ) => removeComponent ( component . id ) ;
70- }
71-
72- let pathRemoveHandler = null ;
73-
74- if ( typeof component . removePath === 'string' ) {
75- // Create a function that will change the history state when removing
76- // the component.
77- pathRemoveHandler = ( ) => this . navigateToPath ( component . removePath ) ;
78- }
79-
80- if ( pathRemoveHandler && removeHandler ) {
81- // A remove handler function and a
82- return {
83- ...component ,
84- remove : ( ) => {
85- pathRemoveHandler ( ) ;
86- return removeHandler ( ) ;
87- } ,
88- } ;
89- }
90-
91- if ( pathRemoveHandler && ! removeHandler ) {
92- return {
93- ...component ,
94- remove : pathRemoveHandler ,
95- } ;
96- }
97-
98- if ( ! pathRemoveHandler && removeHandler !== component . remove ) {
99- return {
100- ...component ,
101- remove : removeHandler ,
102- } ;
54+ const assign = ( component ) => {
55+ const result = {
56+ ...component ,
57+ render : renderMap [ component . type ] ,
58+ update : ( props ) => updateComponent ( component . id , props ) ,
59+ remove : typeof component . removePath === 'string'
60+ ? ( ) => this . navigateToPath ( component . removePath )
61+ : ( ) => removeComponent ( component . id ) ,
62+ } ;
63+
64+ if ( scope ) {
65+ result . scope = scope ;
10366 }
10467
105- // `!pathRemoveHandler && removeHandler === component.remove` is true.
106- // This means `remove` was set and `removePath` was not set on the
107- // component object. No modification is necessary.
108- return component ;
68+ return result ;
10969 } ;
11070
11171 const currentComponents = components
11272 // Remove components not included in the render function map.
11373 . filter ( inRenderMap )
114- // Assign render functions.
115- . map ( assignRender )
116- // Assign scope, if configured.
117- . map ( scope ? assignScope : ( component ) => component )
118- // Assign remove handler functions.
119- . map ( assignRemoveHandler ) ;
74+ // Assign render update and remove functions and scope if it is defined.
75+ . map ( assign ) ;
12076
12177 /* eslint-disable no-unused-vars */
12278 const {
@@ -155,7 +111,7 @@ export default ({scope, ...defaultProps} = {}) => (WrappedComponent) => {
155111 // Put everything in a ___relocationState___ namespace to avoid possible
156112 // conflict with existing props.
157113 ___relocationState___ : {
158- components : getMergedComponents ( state , selectorProps ) ,
114+ components : getComponents ( state , selectorProps ) ,
159115 renderMap : components ,
160116 } ,
161117 } ;
@@ -166,6 +122,7 @@ export default ({scope, ...defaultProps} = {}) => (WrappedComponent) => {
166122 // possible conflict with existing props.
167123 ___relocationDispatch___ : {
168124 removeComponent : ( id ) => dispatch ( removeComponent ( id ) ) ,
125+ updateComponent : ( id , props ) => dispatch ( updateComponent ( id , props ) ) ,
169126 } ,
170127 } ) ;
171128
0 commit comments