/
App.js
62 lines (58 loc) · 1.62 KB
/
App.js
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
import React, {useState} from 'react';
import {Form, Field} from 'react-final-form'
import {Undo} from "react-final-form-undo";
function App() {
const [initialValues, setInitialValues] = useState({firstName: 'Initial first name'})
const onSubmit = (values) => {
// save form
alert('Submitted. Undo/Redo will be reset now.')
// initializing form with new values coming from server
// undo/redo queue will be reset now because we are changing initial values
setInitialValues(values)
}
return (
<Form
onSubmit={onSubmit}
initialValues={initialValues}
debug={console.log}
render={({handleSubmit}) => (
<form>
<h2>Simple form with undo/redo functionality</h2>
<div>
<label>First Name</label>
<Field name="firstName" component="input" />
</div>
<div>
<label>Second Name</label>
<Field name="secondName" component="input" />
</div>
<Undo>
{({canUndo, canRedo, undo, redo}) => (
<div>
<button
onClick={undo}
disabled={!canUndo}
>
Undo
</button>
<button
onClick={redo}
disabled={!canRedo}
>
Redo
</button>
</div>
)}
</Undo>
<button
onClick={handleSubmit}
type="submit">
Submit
</button>
</form>
)}
/>
);
}
export default App;