22
33import { gte } from 'semver' ;
44import {
5- ElementTypeClassOrFunction ,
5+ ElementTypeClass ,
6+ ElementTypeFunction ,
67 ElementTypeContext ,
78 ElementTypeForwardRef ,
89 ElementTypeMemo ,
@@ -12,7 +13,7 @@ import {
1213 ElementTypeSuspense ,
1314} from 'src/devtools/types' ;
1415import { getDisplayName , utfEncodeString } from '../utils' ;
15- import { cleanForBridge } from './utils' ;
16+ import { cleanForBridge , copyWithSet , setInObject } from './utils' ;
1617import {
1718 __DEBUG__ ,
1819 TREE_OPERATION_ADD ,
@@ -193,6 +194,8 @@ export function attach(
193194 DEPRECATED_PLACEHOLDER_SYMBOL_STRING ,
194195 } = ReactSymbols ;
195196
197+ const { overrideProps } = renderer ;
198+
196199 const debug = ( name : string , fiber : Fiber , parentFiber : ?Fiber ) : void => {
197200 if ( __DEBUG__ ) {
198201 const fiberData = getDataForFiber ( fiber ) ;
@@ -295,13 +298,19 @@ export function attach(
295298
296299 switch ( tag ) {
297300 case ClassComponent :
298- case FunctionComponent :
299301 case IncompleteClassComponent :
302+ fiberData = {
303+ displayName : getDisplayName ( resolvedType ) ,
304+ key,
305+ type : ElementTypeClass ,
306+ } ;
307+ break ;
308+ case FunctionComponent :
300309 case IndeterminateComponent :
301310 fiberData = {
302311 displayName : getDisplayName ( resolvedType ) ,
303312 key,
304- type : ElementTypeClassOrFunction ,
313+ type : ElementTypeFunction ,
305314 } ;
306315 break ;
307316 case ForwardRef :
@@ -1072,7 +1081,7 @@ export function attach(
10721081 tag === IncompleteClassComponent ||
10731082 tag === IndeterminateComponent
10741083 ) {
1075- if ( stateNode && Object . keys ( stateNode . context ) . length > 0 ) {
1084+ if ( stateNode && stateNode . context != null ) {
10761085 context = stateNode . context ;
10771086 }
10781087 } else if (
@@ -1135,7 +1144,7 @@ export function attach(
11351144 id,
11361145
11371146 // Does the current renderer support editable props/state/hooks?
1138- canEditValues : false , // TODO
1147+ canEditFunctionProps : typeof overrideProps === 'function' ,
11391148
11401149 // Inspectable properties.
11411150 // TODO Review sanitization approach for the below inspectable values.
@@ -1156,6 +1165,49 @@ export function attach(
11561165 } ;
11571166 }
11581167
1168+ function setInProps ( id : number , path : Array < string | number > , value : any ) {
1169+ const fiber = findCurrentFiberUsingSlowPath ( idToFiberMap . get ( id ) ) ;
1170+ if ( fiber !== null ) {
1171+ const instance = fiber . stateNode ;
1172+ if ( instance === null ) {
1173+ if ( typeof overrideProps === 'function' ) {
1174+ overrideProps ( fiber , path , value ) ;
1175+ }
1176+ } else {
1177+ fiber . pendingProps = copyWithSet ( instance . props , path , value ) ;
1178+ instance . forceUpdate ( ) ;
1179+ }
1180+ }
1181+ }
1182+
1183+ function setInState ( id : number , path : Array < string | number > , value : any ) {
1184+ const fiber = findCurrentFiberUsingSlowPath ( idToFiberMap . get ( id ) ) ;
1185+ if ( fiber !== null ) {
1186+ const instance = fiber . stateNode ;
1187+ setInObject ( instance . state , path , value ) ;
1188+ instance . forceUpdate ( ) ;
1189+ }
1190+ }
1191+
1192+ function setInContext ( id : number , path : Array < string | number > , value : any ) {
1193+ // To simplify hydration and display of primative context values (e.g. number, string)
1194+ // the inspectElement() method wraps context in a {value: ...} object.
1195+ // We need to remove the first part of the path (the "value") before continuing.
1196+ path = path . slice ( 1 ) ;
1197+
1198+ const fiber = findCurrentFiberUsingSlowPath ( idToFiberMap . get ( id ) ) ;
1199+ if ( fiber !== null ) {
1200+ const instance = fiber . stateNode ;
1201+ if ( path . length === 0 ) {
1202+ // Simple context value
1203+ instance . context = value ;
1204+ } else {
1205+ setInObject ( instance . context , path , value ) ;
1206+ }
1207+ instance . forceUpdate ( ) ;
1208+ }
1209+ }
1210+
11591211 return {
11601212 getFiberIDFromNative,
11611213 getNativeFromReactElement,
@@ -1164,7 +1216,10 @@ export function attach(
11641216 inspectElement,
11651217 selectElement,
11661218 cleanup,
1167- walkTree,
11681219 renderer,
1220+ setInContext,
1221+ setInProps,
1222+ setInState,
1223+ walkTree,
11691224 } ;
11701225}
0 commit comments