/
example-react.tsx
123 lines (114 loc) · 3.19 KB
/
example-react.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import * as React from 'react';
import { useState } from 'react';
import Temporis from '../index';
type Items = {
name: string;
color: string;
fontSize: string;
};
// needs to be outside of the component in order
// to maintain state outside of re-renders
const temporis = Temporis<Items>();
const initialState: Items = {
name: 'Hello, World!',
color: 'red',
fontSize: '24',
};
temporis.pushOne(initialState);
export default function ExampleReact() {
const [items, setItems] = useState<Items>(initialState);
function pushOne(items: Items) {
temporis.pushOne(items);
setItems(items);
}
function undo() {
temporis.undo();
const current = temporis.getCurrentItem();
if (current) {
setItems(current);
}
}
function redo() {
temporis.redo();
const current = temporis.getCurrentItem();
if (current) {
setItems(current);
}
}
return (
<div className="app">
<div className="controls">
<div className="actions">
<button className="action-btn" onClick={undo}>
← <span>Undo</span>
</button>
<button className="action-btn" onClick={redo}>
→ <span>Redo</span>
</button>
</div>
<div className="selections">
<div className="selection">
<p className="selection-title">Name</p>
<input
name="name"
value={items.name}
onChange={(e) => {
pushOne({
name: e.target.value,
color: items.color,
fontSize: items.fontSize,
});
}}
/>
</div>
<div className="selection">
<p className="selection-title">Color</p>
<select
className="selector-color"
value={items.color}
onChange={(e) => {
pushOne({
name: items.name,
color: e.target.value,
fontSize: items.fontSize,
});
}}>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</div>
<div className="selection">
<p className="selection-title">Font Size</p>
<select
className="selector-font-size"
value={items.fontSize}
onChange={(e) => {
pushOne({
name: items.name,
color: items.color,
fontSize: e.target.value,
});
}}>
<option value="16">16</option>
<option value="18">18</option>
<option value="24">24</option>
<option value="24">28</option>
<option value="36">36</option>
<option value="44">44</option>
</select>
</div>
</div>
</div>
<div className="preview">
<p
style={{
color: items.color,
fontSize: `${items.fontSize}px`,
}}>
{items.name}
</p>
</div>
</div>
);
}