-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProfileEditingExample.js
115 lines (108 loc) · 2.88 KB
/
ProfileEditingExample.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
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
import React from "react";
import usePatch from "use-patch";
/** @type {React.CSSProperties} */
const inputGroupStyles = {
margin: "0 0 1em",
};
/** @type {React.CSSProperties} */
const inputStyles = {
display: "block",
width: "100%",
boxSizing: "border-box",
};
const ProfileEditingExample = () => {
const profile = { firstName: "Jeff", lastName: "Adams" };
const profilePatch = usePatch(profile);
return (
<div>
<h3>Origin</h3>
<pre>{JSON.stringify(profile, null, 2)}</pre>
<form>
<h3>Editor</h3>
<div style={inputGroupStyles}>
<label>
First name
<input
value={profilePatch.value.firstName}
onChange={(e) =>
profilePatch.apply({ firstName: e.target.value })
}
style={inputStyles}
/>
</label>
</div>
<div style={inputGroupStyles}>
<label>
Last name
<input
value={profilePatch.value.lastName}
onChange={(e) => profilePatch.apply({ lastName: e.target.value })}
style={inputStyles}
/>
</label>
</div>
<div style={inputGroupStyles}>
<label>
Age
<input
value={profilePatch.value.age || ""}
onChange={(e) =>
profilePatch.apply({ age: +e.target.value || undefined })
}
style={inputStyles}
/>
</label>
</div>
</form>
<h3>The diff</h3>
<pre>{JSON.stringify(profilePatch.diff, null, 2)}</pre>
</div>
);
};
export default ProfileEditingExample;
export const code = `const ProfileEditingExample = () => {
const profile = { firstName: "Jeff", lastName: "Adams" };
const profilePatch = usePatch(profile);
return (
<div>
<h3>Origin</h3>
<pre>{JSON.stringify(profile, null, 2)}</pre>
<form>
<h3>Editor</h3>
<div>
<label>
First name
<input
value={profilePatch.value.firstName}
onChange={(e) =>
profilePatch.apply({ firstName: e.target.value })
}
/>
</label>
</div>
<div>
<label>
Last name
<input
value={profilePatch.value.lastName}
onChange={(e) => profilePatch.apply({ lastName: e.target.value })}
/>
</label>
</div>
<div>
<label>
Age
<input
value={profilePatch.value.age || ""}
onChange={(e) =>
profilePatch.apply({ age: +e.target.value || undefined })
}
/>
</label>
</div>
</form>
<h3>The diff</h3>
<pre>{JSON.stringify(profilePatch.diff, null, 2)}</pre>
</div>
);
};`;