@@ -3,8 +3,17 @@ import PropTypes from 'prop-types';
33import { Text , View } from 'react-native' ;
44import { JSONNode } from './Nodes' ;
55import createStylingFromTheme from './createStylingFromTheme' ;
6+ import { invertTheme } from 'react-base16-styling' ;
67
78const identity = value => value ;
9+ const expandRootNode = ( _keyName , _data , level ) => level === 0 ;
10+ const defaultItemString = ( _type , _data , itemType , itemString ) => (
11+ < Text >
12+ { itemType } { itemString }
13+ </ Text >
14+ ) ;
15+ const defaultLabelRenderer = ( [ label ] ) => < Text > { label } :</ Text > ;
16+ const noCustomNode = ( ) => false ;
817
918/* eslint-disable no-param-reassign */
1019function checkLegacyTheming ( theme , props ) {
@@ -16,9 +25,9 @@ function checkLegacyTheming(theme, props) {
1625 getValueStyle : 'valueText' ,
1726 } ;
1827
19- const deprecatedStylingMethods = Object
20- . keys ( deprecatedStylingMethodsMap )
21- . filter ( name => props [ name ] ) ;
28+ const deprecatedStylingMethods = Object . keys (
29+ deprecatedStylingMethodsMap
30+ ) . filter ( name => props [ name ] ) ;
2231
2332 if ( deprecatedStylingMethods . length > 0 ) {
2433 if ( typeof theme === 'string' ) {
@@ -27,9 +36,11 @@ function checkLegacyTheming(theme, props) {
2736 theme = { ...theme } ;
2837 }
2938
30- deprecatedStylingMethods . forEach ( ( name ) => {
39+ deprecatedStylingMethods . forEach ( name => {
3140 // eslint-disable-next-line no-console
32- console . error ( `Styling method "${ name } " is deprecated, use the "theme" property instead` ) ;
41+ console . error (
42+ `Styling method "${ name } " is deprecated, use the "theme" property instead`
43+ ) ;
3344
3445 theme [ deprecatedStylingMethodsMap [ name ] ] = ( { style } , ...args ) => ( {
3546 style : {
@@ -43,6 +54,17 @@ function checkLegacyTheming(theme, props) {
4354 return theme ;
4455}
4556
57+ function getStateFromProps ( props ) {
58+ let theme = checkLegacyTheming ( props . theme , props ) ;
59+ if ( props . invertTheme ) {
60+ theme = invertTheme ( theme ) ;
61+ }
62+ 63+ return {
64+ styling : createStylingFromTheme ( theme ) ,
65+ } ;
66+ }
67+ 4668/* eslint-enable no-param-reassign */
4769
4870class JSONTree extends React . Component {
@@ -56,48 +78,67 @@ class JSONTree extends React.Component {
5678 ] ) . isRequired ,
5779 hideRoot : PropTypes . bool ,
5880 invertTheme : PropTypes . bool ,
59- keyPath : PropTypes . arrayOf ( PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] ) ) ,
81+ keyPath : PropTypes . arrayOf (
82+ PropTypes . oneOfType ( [ PropTypes . string , PropTypes . number ] )
83+ ) ,
6084 postprocessValue : PropTypes . func ,
6185 sortObjectKeys : PropTypes . oneOfType ( [ PropTypes . func , PropTypes . bool ] ) ,
6286 theme : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . string ] ) ,
6387 } ;
6488
6589 static defaultProps = {
66- shouldExpandNode : ( keyName , data , level ) => level === 0 , // expands root by default ,
90+ shouldExpandNode : expandRootNode ,
6791 hideRoot : false ,
6892 keyPath : [ 'root' ] ,
69- getItemString : ( type , data , itemType , itemString ) => < Text > { itemType } { itemString } </ Text > ,
70- labelRenderer : ( [ label ] ) => < Text > { label } : </ Text > ,
93+ getItemString : defaultItemString ,
94+ labelRenderer : defaultLabelRenderer ,
7195 valueRenderer : identity ,
7296 postprocessValue : identity ,
73- isCustomNode : ( ) => false ,
97+ isCustomNode : noCustomNode ,
7498 collectionLimit : 50 ,
7599 invertTheme : true ,
76100 sortObjectKeys : true ,
77101 } ;
78102
103+ constructor ( props ) {
104+ super ( props ) ;
105+ this . state = getStateFromProps ( props ) ;
106+ }
107+ 108+ static getDerivedStateFromProps ( props , state ) {
109+ if ( [ 'theme' , 'invertTheme' ] . find ( k => props [ k ] !== state [ k ] ) ) {
110+ return getStateFromProps ( props ) ;
111+ }
112+ return null ;
113+ }
114+ 115+ shouldComponentUpdate ( nextProps ) {
116+ return ! ! Object . keys ( nextProps ) . find ( k =>
117+ k === 'keyPath'
118+ ? nextProps [ k ] . join ( '/' ) !== this . props [ k ] . join ( '/' )
119+ : nextProps [ k ] !== this . props [ k ]
120+ ) ;
121+ }
122+ 79123 render ( ) {
80124 const {
81125 data : value ,
82126 keyPath,
83127 postprocessValue,
84128 hideRoot,
85- theme,
86- invertTheme,
129+ theme, // eslint-disable-line no-unused-vars
130+ invertTheme : _ , // eslint-disable-line no-unused-vars
87131 ...rest
88132 } = this . props ;
89133
90- const styling = createStylingFromTheme ( checkLegacyTheming ( theme , rest ) , invertTheme ) ;
134+ const { styling } = this . state ;
91135
92136 return (
93137 < View { ...styling ( 'tree' ) } >
94138 < JSONNode
95- hideRoot = { hideRoot }
139+ { ... { postprocessValue , hideRoot, styling , ... rest } }
96140 keyPath = { hideRoot ? [ ] : keyPath }
97- postprocessValue = { postprocessValue }
98- styling = { styling }
99141 value = { postprocessValue ( value ) }
100- { ...rest }
101142 />
102143 </ View >
103144 ) ;
0 commit comments