-
Notifications
You must be signed in to change notification settings - Fork 18
/
Body.tsx
292 lines (266 loc) · 7.98 KB
/
Body.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Author: Alexandre Havrileck (Oxyno-zeta)
* Date: 30/10/16
* Licence: See Readme
*/
import _ from "lodash";
import React, { useCallback, useRef, useState } from "react";
// @ts-ignore
import { JsonTree } from "react-editable-json-tree";
import { trySetRefAttr } from "../utils/misc";
const defaultJson = {
error: new Error("error"),
func: () => {
// eslint-disable-next-line no-console
console.log("test");
},
text: "text",
int: 100,
boolean: true,
null: null,
// eslint-disable-next-line object-shorthand
undefined: undefined,
object: {
text: "text",
int: 100,
boolean: true,
},
array: [1, 2, 3, { string: "test" }],
};
type ReadOnlyCallback = (
name: string,
value: unknown,
keyPath: string
) => boolean;
function Body() {
const [json, setJson] = useState(_.cloneDeep(defaultJson));
const [deltaUpdateString, setDeltaUpdateString] = useState("{}");
const [readOnly, setReadOnly] = useState<boolean | ReadOnlyCallback>(false);
const [globalUpdateString, setGlobalUpdateString] = useState("{}");
const [customInput, setCustomInput] = useState(false);
const [minusMenu, setMinusMenu] = useState(false);
const [readOnlyEnable, setReadOnlyEnable] = useState(false);
const [readOnlyFunctionEnable, setReadOnlyFunctionEnable] = useState(false);
const [readOnlyBooleanEnable, setReadOnlyBooleanEnable] = useState(false);
const textareaRef = useRef<HTMLTextAreaElement | null>(null);
const readOnlyBooleanRef = useRef<HTMLInputElement | null>(null);
const readOnlyFunctionRef = useRef<HTMLInputElement | null>(null);
const readOnlyRef = useRef<HTMLInputElement | null>(null);
const customInputRef = useRef<HTMLInputElement | null>(null);
const minusMenuRef = useRef<HTMLInputElement | null>(null);
const onFullyUpdate = useCallback((newJson: object) => {
setGlobalUpdateString(JSON.stringify(newJson, null, 4));
}, []);
const onDeltaUpdate = useCallback((deltaUpdate: object) => {
setDeltaUpdateString(JSON.stringify(deltaUpdate, null, 4));
}, []);
const handleChangeMinusMenu = useCallback(() => {
setMinusMenu(minusMenuRef.current?.checked ?? false);
}, []);
const handleChangeCustomInput = useCallback(() => {
setCustomInput(customInputRef.current?.checked ?? false);
}, []);
const handleSubmit = useCallback(() => {
if (!textareaRef.current) return;
const jsonString = textareaRef.current.value;
if (!jsonString) return;
try {
setJson(JSON.parse(jsonString));
} catch (e) {
// Nothing
// eslint-disable-next-line no-console
console.error(e);
return;
}
// Reset value
textareaRef.current.value = "";
}, []);
const handleResetToDefault = useCallback(() => {
setJson(_.cloneDeep(defaultJson));
}, []);
const handleChangeReadOnlyBoolean = useCallback(() => {
trySetRefAttr(readOnlyFunctionRef, {
disabled: readOnlyBooleanRef.current?.checked ?? false,
});
setReadOnly(
(prevState: any) => readOnlyBooleanRef.current?.checked ?? prevState
);
}, []);
const handleChangeReadOnlyFunction = useCallback(() => {
trySetRefAttr(readOnlyBooleanRef, {
disabled: readOnlyFunctionRef.current?.checked ?? false,
});
let result: ReadOnlyCallback;
if (readOnlyFunctionRef.current?.checked) {
result = (name, value, keyPath) => keyPath[keyPath.length - 1] === "text";
} else {
result = () => false;
}
setReadOnly(result);
}, []);
const handleChangeReadOnly = useCallback(() => {
const checkbox = readOnlyRef.current;
if (!checkbox) return;
setReadOnlyEnable(checkbox.checked);
if (checkbox.checked) {
trySetRefAttr(readOnlyBooleanRef, { disabled: false });
trySetRefAttr(readOnlyFunctionRef, { disabled: false });
if (readOnlyBooleanRef.current?.checked) {
handleChangeReadOnlyBoolean();
} else if (readOnlyFunctionRef.current?.checked) {
handleChangeReadOnlyFunction();
}
} else {
trySetRefAttr(readOnlyBooleanRef, { disabled: true });
trySetRefAttr(readOnlyFunctionRef, { disabled: true });
setReadOnly(false);
}
}, []);
const handleClearGlobalUpdateString = useCallback(() => {
setGlobalUpdateString("{}");
}, []);
const handleClearDeltaUpdateString = useCallback(() => {
setDeltaUpdateString("{}");
}, []);
// Render
const style1 = {
width: "100%",
};
const style2 = {
verticalAlign: "top",
};
const style3 = {
backgroundColor: "#e0e0e0",
border: "1px lightgrey solid",
};
const style4 = {
margin: "0 15px",
minWidth: "200px",
};
const style5 = {
backgroundColor: "black",
color: "yellow",
border: "1px solid green",
};
const customInputElement = customInput ? <input style={style5} /> : undefined;
const minusMenuElement = minusMenu ? (
<button type="button">Remove</button>
) : undefined;
return (
<div>
<div style={style4}>
<span>
<input
type="checkbox"
ref={readOnlyRef}
onChange={handleChangeReadOnly}
/>
Read Only
</span>
<span>
<input
type="checkbox"
ref={readOnlyBooleanRef}
onChange={handleChangeReadOnlyBoolean}
disabled
/>
Read Only Boolean
</span>
<span>
<input
type="checkbox"
ref={readOnlyFunctionRef}
onChange={handleChangeReadOnlyFunction}
disabled
/>
Read Only Function (read only for all 'text' key)
</span>
<span>
<input
type="checkbox"
ref={customInputRef}
onChange={handleChangeCustomInput}
/>
Custom input
</span>
<span>
<input
type="checkbox"
ref={minusMenuRef}
onChange={handleChangeMinusMenu}
/>
Custom minus menu
</span>
</div>
<table style={style1}>
<thead>
<tr>
<th>
<span>Result</span>
</th>
<th>
<span>
Global Update{" "}
<button type="button" onClick={handleClearGlobalUpdateString}>
Clear
</button>
</span>
</th>
<th>
<span>
Delta Update{" "}
<button type="button" onClick={handleClearDeltaUpdateString}>
Clear
</button>
</span>
</th>
<th>
<span>Put Your Json</span>
</th>
</tr>
</thead>
<tbody>
<tr>
<td style={style2}>
<div style={style4}>
<JsonTree
data={json}
onFullyUpdate={onFullyUpdate}
onDeltaUpdate={onDeltaUpdate}
readOnly={readOnly}
inputElement={customInputElement}
minusMenuElement={minusMenuElement}
logger={console}
/>
</div>
</td>
<td style={style2}>
<div style={style4}>
<pre style={style3}>{globalUpdateString}</pre>
</div>
</td>
<td style={style2}>
<div style={style4}>
<pre style={style3}>{deltaUpdateString}</pre>
</div>
</td>
<td style={style2}>
<div style={style4}>
<textarea ref={textareaRef} rows={15} cols={40} />
<div>
<button type="button" onClick={handleSubmit}>
Submit
</button>
<button type="button" onClick={handleResetToDefault}>
Default
</button>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
);
}
export default Body;