@@ -3,8 +3,17 @@ import PropTypes from 'prop-types';
3
3
import { Text , View } from 'react-native' ;
4
4
import { JSONNode } from './Nodes' ;
5
5
import createStylingFromTheme from './createStylingFromTheme' ;
6
+ import { invertTheme } from 'react-base16-styling' ;
6
7
7
8
const 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 ;
8
17
9
18
/* eslint-disable no-param-reassign */
10
19
function checkLegacyTheming ( theme , props ) {
@@ -16,9 +25,9 @@ function checkLegacyTheming(theme, props) {
16
25
getValueStyle : 'valueText' ,
17
26
} ;
18
27
19
- const deprecatedStylingMethods = Object
20
- . keys ( deprecatedStylingMethodsMap )
21
- . filter ( name => props [ name ] ) ;
28
+ const deprecatedStylingMethods = Object . keys (
29
+ deprecatedStylingMethodsMap
30
+ ) . filter ( name => props [ name ] ) ;
22
31
23
32
if ( deprecatedStylingMethods . length > 0 ) {
24
33
if ( typeof theme === 'string' ) {
@@ -27,9 +36,11 @@ function checkLegacyTheming(theme, props) {
27
36
theme = { ...theme } ;
28
37
}
29
38
30
- deprecatedStylingMethods . forEach ( ( name ) => {
39
+ deprecatedStylingMethods . forEach ( name => {
31
40
// 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
+ ) ;
33
44
34
45
theme [ deprecatedStylingMethodsMap [ name ] ] = ( { style } , ...args ) => ( {
35
46
style : {
@@ -43,6 +54,17 @@ function checkLegacyTheming(theme, props) {
43
54
return theme ;
44
55
}
45
56
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
+
46
68
/* eslint-enable no-param-reassign */
47
69
48
70
class JSONTree extends React . Component {
@@ -56,48 +78,67 @@ class JSONTree extends React.Component {
56
78
] ) . isRequired ,
57
79
hideRoot : PropTypes . bool ,
58
80
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
+ ) ,
60
84
postprocessValue : PropTypes . func ,
61
85
sortObjectKeys : PropTypes . oneOfType ( [ PropTypes . func , PropTypes . bool ] ) ,
62
86
theme : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . string ] ) ,
63
87
} ;
64
88
65
89
static defaultProps = {
66
- shouldExpandNode : ( keyName , data , level ) => level === 0 , // expands root by default ,
90
+ shouldExpandNode : expandRootNode ,
67
91
hideRoot : false ,
68
92
keyPath : [ 'root' ] ,
69
- getItemString : ( type , data , itemType , itemString ) => < Text > { itemType } { itemString } </ Text > ,
70
- labelRenderer : ( [ label ] ) => < Text > { label } : </ Text > ,
93
+ getItemString : defaultItemString ,
94
+ labelRenderer : defaultLabelRenderer ,
71
95
valueRenderer : identity ,
72
96
postprocessValue : identity ,
73
- isCustomNode : ( ) => false ,
97
+ isCustomNode : noCustomNode ,
74
98
collectionLimit : 50 ,
75
99
invertTheme : true ,
76
100
sortObjectKeys : true ,
77
101
} ;
78
102
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
+
79
123
render ( ) {
80
124
const {
81
125
data : value ,
82
126
keyPath,
83
127
postprocessValue,
84
128
hideRoot,
85
- theme,
86
- invertTheme,
129
+ theme, // eslint-disable-line no-unused-vars
130
+ invertTheme : _ , // eslint-disable-line no-unused-vars
87
131
...rest
88
132
} = this . props ;
89
133
90
- const styling = createStylingFromTheme ( checkLegacyTheming ( theme , rest ) , invertTheme ) ;
134
+ const { styling } = this . state ;
91
135
92
136
return (
93
137
< View { ...styling ( 'tree' ) } >
94
138
< JSONNode
95
- hideRoot = { hideRoot }
139
+ { ... { postprocessValue , hideRoot, styling , ... rest } }
96
140
keyPath = { hideRoot ? [ ] : keyPath }
97
- postprocessValue = { postprocessValue }
98
- styling = { styling }
99
141
value = { postprocessValue ( value ) }
100
- { ...rest }
101
142
/>
102
143
</ View >
103
144
) ;
0 commit comments