-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_form.js
418 lines (393 loc) · 14 KB
/
table_form.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import { useState, useEffect } from 'react';
import {
Button,
Space,
Input,
Card,
Popconfirm,
Form,
Checkbox,
AutoComplete,
} from '@arco-design/web-react';
import classNames from 'classnames';
import { nanoid } from 'nanoid';
import fieldTypes from '../data/filed_typs';
/**
* It takes the current fields array, checks if the current index is less than the length of the array,
* and if so, swaps the current index with the next index
* @param props - The props passed to the component
* @param ref - This is a reference to the form element.
* @returns A React component
*/
function TableFormItem(props) {
/**
* If the index of the current field is greater than 0, then swap the current field with the field
* above it
*/
const moveUp = () => {
props.setFields(fields => {
if (props.index > 0) {
const _fields = [...fields];
[_fields[props.index], _fields[props.index - 1]] = [
_fields[props.index - 1],
_fields[props.index],
];
return _fields;
}
return fields;
});
};
/**
* It takes the current fields array, checks if the current index is less than the length of the
* array, and if so, swaps the current index with the next index
*/
const moveDown = () => {
props.setFields(fields => {
if (props.index < fields.length - 1) {
const _fields = [...fields];
[_fields[props.index], _fields[props.index + 1]] = [
_fields[props.index + 1],
_fields[props.index],
];
return _fields;
}
return fields;
});
};
const { field } = props;
const index = `A${props.index}`;
return (
<Card
className={classNames({
dropping:
props.draggingId &&
props.droppingId === props.field.id &&
props.droppingId !== props.draggingId &&
props.index !== props.draggingIndex - 1,
dragging:
props.draggingId && props.draggingId === props.field.id,
'table-form': true,
})}
draggable="true"
onDragStart={() => {
props.onDragStart(props.field.id);
}}
onDragEnd={() => {
props.setDraggingId(null);
props.setDroppingId(null);
}}
onDragOver={e => {
props.setDroppingId(props.field.id);
e.preventDefault();
}}
onDrop={e => {
props.onDrop(props.field.id);
}}
>
<Form.Item hidden field={`${index}.id`} initialValue={field.id}>
<Input />
</Form.Item>
<Space direction="vertical" style={{ width: '100%' }}>
<Space className="table-form-item">
<Form.Item
label="Name"
field={`${index}.name`}
initialValue={field.name}
rules={[
{
required: true,
message: 'Please enter field name',
},
]}
>
<Input allowClear />
</Form.Item>
<Form.Item
label="Type"
field={`${index}.type`}
initialValue={field.type}
rules={[
{
required: true,
message: 'Please choose field type',
},
]}
>
<AutoComplete data={fieldTypes}></AutoComplete>
</Form.Item>
</Space>
<Space className="table-form-item">
<Form.Item
label="Comment"
field={`${index}.note`}
initialValue={field.note || ''}
>
<Input allowClear placeholder="note" />
</Form.Item>
<Form.Item
label="Default"
field={`${index}.dbdefault`}
initialValue={field.dbdefault || ''}
>
<Input allowClear placeholder="default" />
</Form.Item>
</Space>
<Space className="table-form-item">
<Form.Item
noStyle
field={`${index}.pk`}
initialValue={field.pk}
>
<Checkbox defaultChecked={field.pk}>Primary</Checkbox>
</Form.Item>
<Form.Item
noStyle
field={`${index}.unique`}
initialValue={field.unique}
>
<Checkbox defaultChecked={field.unique}>
Unique
</Checkbox>
</Form.Item>
<Form.Item
noStyle
field={`${index}.not_null`}
initialValue={field.not_null}
>
<Checkbox defaultChecked={field.not_null}>
Not Null
</Checkbox>
</Form.Item>
<Form.Item
noStyle
field={`${index}.increment`}
initialValue={field.increment}
>
<Checkbox defaultChecked={field.increment}>
Increment
</Checkbox>
</Form.Item>
</Space>
<Space className="table-form-item">
<Button onClick={moveUp} type="primary" size="small" long>
↑ Move up
</Button>
<Button onClick={moveDown} type="primary" size="small" long>
↓ Move down
</Button>
<Popconfirm
title="Are you sure delete this field?"
onOk={() => {
props.removeItem(props.field.id);
}}
okText="Yes"
cancelText="No"
>
<Button status="danger" size="small" long>
Remove field
</Button>
</Popconfirm>
<Button
onClick={() => props.addItem(props.index)}
type="outline"
status="success"
size="small"
long
>
+ Add field after
</Button>
</Space>
</Space>
</Card>
);
}
/* A forwardRef function that is used to forward the ref to the child component. */
// const TableRefFormItem = forwardRef(TableFormItem);
/**
* It renders a form for editing a table
* @param props - The props passed to the component.
* @returns A TableForm component
*/
export default function TableForm(props) {
const [fields, setFields] = useState(props.table.fields);
const [name, setName] = useState(props.table.name);
const [note, setNote] = useState(props.table.note);
const [form] = Form.useForm();
useEffect(() => {
setFields(props.table.fields);
}, [props.table]);
const save = values => {
const table = {
...props.table,
name,
note,
fields: Object.values(values),
};
delete table.x;
delete table.y;
props.updateTable(table);
props.setCommitting(false);
};
useEffect(() => {
if (props.committing) {
form.submit();
}
}, [props.committing]);
const addItem = index => {
const newState = [...fields];
newState.splice(index + 1, 0, {
id: nanoid(),
name: 'new item' + newState.length,
type: '',
unique: false,
});
setFields(newState);
};
const removeItem = id => {
setFields(state => {
const fields = state.filter(item => item.id !== id);
return fields.length ? fields : [];
});
};
// Drag and drop
const [draggingId, setDraggingId] = useState(false);
const [draggingIndex, setDraggingIndex] = useState(false);
const [droppingId, setDroppingId] = useState(false);
const onDragStart = id => {
setDraggingId(id);
setDraggingIndex(fields.findIndex(item => item.id === id));
};
const onDrop = id => {
setDroppingId(null);
const index = fields.findIndex(item => item.id === id);
const draggingIndex = fields.findIndex(item => item.id === draggingId);
if (index === draggingIndex) {
return setDraggingId(null);
}
if (index === draggingIndex - 1) {
setFields(state => {
const fields = [...state];
[fields[draggingIndex], fields[draggingIndex - 1]] = [
fields[draggingIndex - 1],
fields[draggingIndex],
];
return fields;
});
} else {
setFields(state => {
const _fields = [...state];
const draggingFiled = _fields.splice(draggingIndex, 1)[0];
const index = _fields.findIndex(item => item.id === id);
if (index + 1 < _fields.length) {
_fields.splice(index + 1, 0, draggingFiled);
} else {
_fields.push(draggingFiled);
}
return _fields;
});
}
setDraggingId(null);
};
const unShiftFields = () => {
const draggingIndex = fields.findIndex(item => item.id === draggingId);
setFields(state => {
const _fields = [...state];
const field = _fields.splice(draggingIndex, 1);
_fields.unshift(...field);
return _fields;
});
setDraggingId(null);
setDroppingId(null);
};
return (
<Space direction="vertical" style={{ width: '100%' }}>
<div
className={droppingId === 'root' ? 'dropping' : ''}
onDragOver={e => {
if (draggingIndex !== 0) setDroppingId('root');
e.preventDefault();
}}
onDrop={e => {
unShiftFields();
}}
style={{ display: 'flex', alignItems: 'center' }}
>
<label>Table Name:</label>
<Input
defaultValue={props.table.name}
type="text"
onChange={value => {
setName(value);
}}
style={{ width: 200, margin: '0 8px' }}
/>
<Popconfirm
position="br"
title="Are you sure you want to delete this table?"
okText="Yes"
cancelText="No"
onOk={() => {
props.removeTable(props.table.id);
}}
>
<Button type="outline" status="warning">
Delete table
</Button>
</Popconfirm>
</div>
<div style={{ display: 'flex', alignItems: 'center' }}>
<label>Table Comment:</label>
<Input
defaultValue={props.table.note}
type="text"
onChange={value => setNote(value)}
style={{ width: 460, marginLeft: 8 }}
/>
</div>
<Form
onSubmit={save}
onSubmitFailed={() => {
props.setCommitting(false);
}}
form={form}
labelAlign="left"
requiredSymbol={false}
labelCol={{ span: 6 }}
wrapperCol={{ span: 18 }}
onValuesChange={(changedValues, allValues) => {
if (!props.formChange) props.setFormChange(true);
}}
scrollToFirstError
>
{fields.map((field, index) => (
<TableFormItem
field={field}
key={field.id}
index={index}
addItem={addItem}
removeItem={removeItem}
setFields={setFields}
onDragStart={onDragStart}
onDrop={onDrop}
draggingIndex={draggingIndex}
draggingId={draggingId}
droppingId={droppingId}
setDroppingId={setDroppingId}
setDraggingId={setDraggingId}
/>
))}
{fields.length === 0 && (
<Button
onClick={() => addItem(0)}
type="outline"
status="success"
size="small"
long
>
+ Add field
</Button>
)}
</Form>
</Space>
);
}