Skip to content
This repository was archived by the owner on May 15, 2022. It is now read-only.

Commit a30a7c8

Browse files
committed
Add more presets to example site
1 parent 506b435 commit a30a7c8

File tree

1 file changed

+75
-47
lines changed

1 file changed

+75
-47
lines changed

example/components/ReactDatGui.js

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,66 +11,87 @@ import DatGui, {
1111
} from 'react-dat-gui';
1212
import 'react-dat-gui/dist/index.css';
1313

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 {
1519
constructor() {
1620
super();
1721

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'
3032
}
3133
};
32-
}
3334

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+
}
3940

40-
generateRandomNumber = () =>
41+
// Update random number for current state
42+
handleButtonClick = () =>
4143
this.setState(prevState => ({
4244
data: { ...prevState.data, random: Math.random() }
4345
}));
4446

47+
// Update current state with changes from controls
48+
handleUpdate = newData =>
49+
this.setState(prevState => ({
50+
data: { ...prevState.data, ...newData }
51+
}));
52+
4553
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;
6455
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' } }
6889
];
6990

7091
return (
71-
<main>
92+
<main style={{ marginRight: '350px' }}>
7293
<h1>
73-
<b style={{ color: `${data.color}` }}>react-dat-gui</b>
94+
<b style={{ color: data.color }}>react-dat-gui</b>
7495
</h1>
7596
<h2>Use the controls and watch your changes happen in real time!</h2>
7697
<section>
@@ -84,14 +105,21 @@ class App extends Component {
84105
<b>Number value:</b> {data.number}
85106
</div>
86107
<div>
87-
<b>Checkbox value:</b> {data.boolean ? 'true' : 'false'}
108+
<b>Checkbox value:</b> {data.boolean.toString()}
88109
</div>
89110
<div>
90111
<b>Select value:</b> {data.select}
91112
</div>
92113
<div>
93114
<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+
/>
95123
</div>
96124
<div>
97125
<b>Click the button for a random number:</b> {data.random}
@@ -116,7 +144,7 @@ class App extends Component {
116144
/>
117145
<DatNumber path="number" label="Number" />
118146
<DatBoolean path="boolean" label="Boolean" />
119-
<DatButton label="Button" onClick={this.handleClick} />
147+
<DatButton label="Button" onClick={this.handleButtonClick} />
120148
<DatSelect
121149
label="Select"
122150
path="select"
@@ -153,4 +181,4 @@ class App extends Component {
153181
}
154182
}
155183

156-
export default App;
184+
export default DatGUI;

0 commit comments

Comments
 (0)