@@ -11,66 +11,87 @@ import DatGui, {
11
11
} from 'react-dat-gui' ;
12
12
import 'react-dat-gui/dist/index.css' ;
13
13
14
- class App extends Component {
14
+ /**
15
+ * Demonstrates presets that extend the default preset (initial state)
16
+ * as well as presets which extend the current state
17
+ */
18
+ class DatGUI extends Component {
15
19
constructor ( ) {
16
20
super ( ) ;
17
21
18
- this . state = {
19
- data : {
20
- string : 'Hello World' ,
21
- minMaxNumber : 66 ,
22
- number : 80 ,
23
- boolean : true ,
24
- select : 'one' ,
25
- color : '#2FA1D6' ,
26
- random : null ,
27
- nested : {
28
- string : 'Goodbye Cruel World'
29
- }
22
+ const initialDatState = {
23
+ string : 'Hello World' ,
24
+ minMaxNumber : 66 ,
25
+ number : 80 ,
26
+ boolean : true ,
27
+ select : 'one' ,
28
+ color : '#2FA1D6' ,
29
+ random : Math . random ( ) ,
30
+ nested : {
31
+ string : 'Nested Hello World'
30
32
}
31
33
} ;
32
- }
33
34
34
- componentDidMount = ( ) => this . generateRandomNumber ( ) ;
35
-
36
- handleClick = ( ) => this . generateRandomNumber ( ) ;
37
-
38
- handleUpdate = data => this . setState ( { data } ) ;
35
+ this . state = {
36
+ data : initialDatState ,
37
+ defaultData : initialDatState
38
+ } ;
39
+ }
39
40
40
- generateRandomNumber = ( ) =>
41
+ // Update random number for current state
42
+ handleButtonClick = ( ) =>
41
43
this . setState ( prevState => ( {
42
44
data : { ...prevState . data , random : Math . random ( ) }
43
45
} ) ) ;
44
46
47
+ // Update current state with changes from controls
48
+ handleUpdate = newData =>
49
+ this . setState ( prevState => ( {
50
+ data : { ...prevState . data , ...newData }
51
+ } ) ) ;
52
+
45
53
render ( ) {
46
- const { data } = this . state ;
47
- const swatchStyle = {
48
- width : '10px' ,
49
- height : '10px' ,
50
- display : 'inline-block'
51
- } ;
52
- const presetA = {
53
- string : 'Preset A' ,
54
- minMaxNumber : 33 ,
55
- number : 40 ,
56
- boolean : false ,
57
- select : 'one' ,
58
- color : '#e61d5f' ,
59
- random : Math . random ( ) ,
60
- nested : {
61
- string : 'Sup'
62
- }
63
- } ;
54
+ const { data, defaultData } = this . state ;
64
55
const presets = [
65
- { A : { ...data , ...presetA } } ,
66
- { B : { ...data , string : 'Preset B' } } ,
67
- { C : { ...data , string : 'Preset C' } }
56
+ // Preset A doesn't extend any other presets
57
+ {
58
+ A : {
59
+ string : 'Preset A' ,
60
+ minMaxNumber : 33 ,
61
+ number : 40 ,
62
+ boolean : false ,
63
+ select : 'one' ,
64
+ color : '#e61d5f' ,
65
+ random : Math . random ( ) ,
66
+ nested : {
67
+ string : 'Nested Preset A'
68
+ }
69
+ }
70
+ } ,
71
+ {
72
+ B : {
73
+ string : 'Preset B' ,
74
+ minMaxNumber : 12 ,
75
+ number : 68 ,
76
+ boolean : true ,
77
+ select : 'three' ,
78
+ color : '#2FD654' ,
79
+ random : Math . random ( ) ,
80
+ nested : {
81
+ string : 'Nested Preset B'
82
+ }
83
+ }
84
+ } ,
85
+ // Preset C extends the default preset
86
+ { 'C (extends Default)' : { ...defaultData , string : 'Preset C' } } ,
87
+ // Preset D extends the current state
88
+ { 'D (extends current state)' : { ...data , string : 'Preset D' } }
68
89
] ;
69
90
70
91
return (
71
- < main >
92
+ < main style = { { marginRight : '350px' } } >
72
93
< h1 >
73
- < b style = { { color : ` ${ data . color } ` } } > react-dat-gui</ b >
94
+ < b style = { { color : data . color } } > react-dat-gui</ b >
74
95
</ h1 >
75
96
< h2 > Use the controls and watch your changes happen in real time!</ h2 >
76
97
< section >
@@ -84,14 +105,21 @@ class App extends Component {
84
105
< b > Number value:</ b > { data . number }
85
106
</ div >
86
107
< div >
87
- < b > Checkbox value:</ b > { data . boolean ? 'true' : 'false' }
108
+ < b > Checkbox value:</ b > { data . boolean . toString ( ) }
88
109
</ div >
89
110
< div >
90
111
< b > Select value:</ b > { data . select }
91
112
</ div >
92
113
< div >
93
114
< b > Picked color:</ b > { ' ' }
94
- < div style = { { ...swatchStyle , backgroundColor : `${ data . color } ` } } />
115
+ < div
116
+ style = { {
117
+ width : '10px' ,
118
+ height : '10px' ,
119
+ display : 'inline-block' ,
120
+ backgroundColor : data . color
121
+ } }
122
+ />
95
123
</ div >
96
124
< div >
97
125
< b > Click the button for a random number:</ b > { data . random }
@@ -116,7 +144,7 @@ class App extends Component {
116
144
/>
117
145
< DatNumber path = "number" label = "Number" />
118
146
< DatBoolean path = "boolean" label = "Boolean" />
119
- < DatButton label = "Button" onClick = { this . handleClick } />
147
+ < DatButton label = "Button" onClick = { this . handleButtonClick } />
120
148
< DatSelect
121
149
label = "Select"
122
150
path = "select"
@@ -153,4 +181,4 @@ class App extends Component {
153
181
}
154
182
}
155
183
156
- export default App ;
184
+ export default DatGUI ;
0 commit comments