ParameterModal.tsx
23.6 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/* eslint-disable array-callback-return */
/* eslint-disable eqeqeq */
import {
SnippetsOutlined,
} from '@ant-design/icons';
import {
Button,
Col,
DatePicker,
Form,
Input,
InputNumber,
Modal,
Row,
Select,
Space,
Table,
Divider,
message,
} from 'antd';
import type { FormInstance } from 'antd/es/form';
import React, {
useCallback,
useEffect,
useState,
} from 'react';
import JSONEditor from './codeMirror';
import './style.less';
import { QxFieldSetter, QxFormSelect } from '@qx/common';
import { formatEnum } from './constant';
import {
typeTranslateFieIdtype,
typeTranslateWidget,
typeTranslateGrouptype,
// typeTranslateItemtype,
} from './constant';
// import { Controlled as CodeMirror } from 'react-codemirror2'
import { cloneDeep } from 'lodash-es';
import moment from 'dayjs';
import { QxBaseIcon } from '@qx/common';
import Icon from '@ant-design/icons';
import { SubNodeIcon } from './sub-node-icon';
type ValueOptionProps = {
key: number | string;
value: string;
};
const valueOptions = [
{ key: 'STRING', title: '文本' },
{ key: 'NUMBER', title: '数字' },
{ key: 'TIME', title: '日期' },
{ key: 'BOOL', title: '布尔' },
{ key: 'OBJECT', title: '对象' },
{ key: 'ARRAY', title: '数组' },
/*{ key: 'ANNEX', title: '附件' },
{ key: 'PIC', title: '图片' },*/
{ key: 'FORM', title: '表单' },
{ key: 'USER', title: '人员' },
{ key: 'ORG', title: '部门' },
];
const timeFormat = [
{ label: '年', value: 'YEAR' },
{ label: '年-月', value: 'YEAR_MONTH' },
{ label: '年-月-日', value: 'YEAR_DATE' },
{ label: '年-月-日 时', value: 'YEAR_HOUR' },
{ label: '年-月-日 时:分', value: 'YEAR_MIN' },
{ label: '年-月-日 时:分:秒', value: 'YEAR_SEC' },
];
interface ParameterSettingProps {
nodeItem?: any;
inputKey: any;
data: any;
treeData: any;
visible: boolean;
handleCancel: (val: any) => void;
handleChangeField: (val?: any, data?: any) => void;
handleChange: (e?: any, data?: any, code?: string) => void;
handleDrop: (val: any) => void;
handleAddTree: (val: any) => void;
handleAdd: (val: any) => void;
handleDelete: (val: any) => void;
request?: any;
nodeType?: string | 'START' | 'END';
appId?: string; // 当前应用ID
}
const EditableContext = React.createContext<FormInstance<any> | null>(null);
const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
console.log('onChange', e);
e.persist();
const { value } = e.target;
console.log('onChange value', value);
// if (!value) {
// setExpandedKeys(undefined);
// setAutoExpandParent(false);
// setSearchValue(value);
// return;
// }
// const newExpandedKeys = (list || [])
// .map((item) => {
// if (item.title.indexOf(value) > -1 || item.code.indexOf(value) > -1) {
// return getParentKey(item.id, treeData);
// }
// return null;
// })
// .filter((item, i, self) => item && self.indexOf(item) === i);
// setExpandedKeys(newExpandedKeys as React.Key[]);
// setSearchValue(value);
// setAutoExpandParent(true);
};
const ParameterModal: React.FC<ParameterSettingProps> = (props) => {
const {
visible,
data,
treeData,
handleCancel,
handleDrop,
handleAdd,
handleAddTree,
handleDelete,
handleChange,
handleChangeField,
inputKey,
nodeType,
} = props;
// const [form] = Form.useForm()
//判断数组是否只有一个子节点
const checkShowAdd = (_data: any) => {
const _newTreeData = cloneDeep(treeData);
if (!!_data.pid) {
const pidIndex = _newTreeData.findIndex(
(item: any) => item.id === _data.pid,
);
if (_newTreeData[pidIndex].type == 'ARRAY') {
return false;
}
}
return true;
};
const getValueOptions = (item: any): ValueOptionProps[] | undefined => {
const widget = typeTranslateWidget(item.type);
if (widget === 'userSelector') {
return [];
// return [{key: 'MYSELF', value: "当前用户"}]
}
if (widget === 'orgSelector') {
// return [{key: 'MY_ORG', value: "当前人所在部门"}]
return [];
}
if (widget === 'qxDatetime') {
return [{ key: 'NOW', value: '当前时间' }];
}
return undefined;
};
const checkShowTree = (_data: any) => {
if (_data.type == 'ARRAY') {
if (_data.child && _data.child.length > 0) {
return false;
}
}
return true;
};
const columns = [
{
title: (
<div style={{ display: 'flex', flexDirection: 'row' }}>
<span style={{ color: 'red' }}>*</span>
<span>参数编码</span>
</div>
),
dataIndex: 'code',
width: '12%',
key: 'code',
render: (text, record) => (
< Form.Item
name={'code' + record.id}
style={{ margin: 0 }}
// initialValue={record.code}
>
<Input
defaultValue={record.code}
bordered={false}
disabled={record.disabled} onBlur={(e) => handleChange(e, record, 'code')} />
</Form.Item >
),
},
{
title: (
<div style={{ display: 'flex', flexDirection: 'row' }}>
<span style={{ color: 'red' }}>*</span>
<span>参数名</span>
</div>
),
dataIndex: 'title',
width: '12%',
key: 'title',
render: (text, record) => (
<Form.Item
name={'title' + record.id}
style={{ margin: 0 }}
// initialValue={record.title}
>
<Input
defaultValue={record.title}
bordered={false}
disabled={record.disabled} onBlur={(e) => handleChange(e, record, 'title')} />
</Form.Item>
),
},
{
title: (
<div style={{ display: 'flex', flexDirection: 'row' }}>
<span style={{ color: 'red' }}>*</span>
<span>参数类型</span>
</div>
),
dataIndex: 'type',
width: '8%',
key: 'type',
render: (text, record) =>
<Form.Item
style={{ margin: 0 }}
name={'type' + record.id}
// initialValue={record.type}
>
<Select
bordered={false}
disabled={record.disabled}
defaultValue={record.type}
onSelect={(e) => handleChange(e, record, 'type')} >
{
valueOptions.map((item) => {
return <Select.Option key={item.key} value={item.key}>{item.title}</Select.Option>;
})
}
</Select>
</Form.Item>
},
{
title: '默认值',
dataIndex: 'valueMapping',
width: '13%',
key: 'valueMapping',
render: (text, record) =>
<div className="editable-cell ">
<QxFieldSetter
value={record.valuesObj || []}
params={{ appCode: 'appCode', useId: true }}
valueOptions={getValueOptions(record)}
field={record.type}
widget={typeTranslateWidget(record.type)}
fieldType={typeTranslateFieIdtype(record.type)}
fieldGroupType={typeTranslateGrouptype(record.type)}
isMixValue={typeTranslateFieIdtype(record.type) === 'STRING'}
isMultiple={false}
colsTree={props.nodeItem}
showFieldOpt={!(nodeType === 'START')}
// onChange={debounce((val) => handleField(val), 700)}
onChange={(val) => handleChangeField(val, record)}
/>
</div>
},
{
title: '参数说明',
dataIndex: 'description',
key: 'description',
render: (text, record) => (
<Form.Item
name={'description' + record.id}
style={{ margin: 0 }}
// initialValue={text}
>
<Input
key={inputKey}
defaultValue={text}
bordered={false}
disabled={record.disabled} onBlur={(e) => handleChange(e, record, 'description')} />
</Form.Item>
),
},
{
title: '操作',
key: 'action',
width: '8%',
render: (text, record) => {
const isShowAdd = (record && checkShowAdd(record));
const isShowTree = (record && checkShowTree(record));
return (
<Space size="small">
{isShowAdd && (
<Button
size={'small'}
type="link"
disabled={record.disabled}
icon={<QxBaseIcon style={{ fontSize: 16 }} type={'qx-icon-plus'} />}
onClick={() => handleAdd(record.pid)}
/>
)}
{(record.type === 'OBJECT' || record.type === 'ARRAY') &&
isShowTree && (
<Button
size={'small'}
type="link"
disabled={record.disabled}
icon={<Icon style={{ fontSize: 16 }} component={SubNodeIcon} />}
onClick={() => handleAddTree(record.id)}
/>
)}
<Button
size={'small'}
type="link"
className="btn-high-del"
disabled={record.disabled}
icon={<QxBaseIcon style={{ fontSize: 16 }} className={'title-btn-del-btn'} type={'qx-icon-delete'} />}
onClick={() => handleDelete(record)}
/>
</Space>
);
},
},
];
const range = {
title: '取值范围',
dataIndex: 'qxProps',
editable: true,
key: 'qxProps',
render: (qxProps: any, record: any) => {
switch (record.type) {
case 'STRING':
return (
<div
key={record.id}
className="editable-cell"
>
<Form.Item
name={'qxProps-min'}
style={{ margin: 0 }}
>
<InputNumber
bordered={false}
min={0}
precision={0}
onBlur={(e) => handleChange(e, record, 'qxProps-min')}
defaultValue={qxProps.min}
placeholder="最小长度"
/>
</Form.Item>
<div className='range-cell'>-</div>
<Form.Item
name={'qxProps-max'}
style={{ margin: 0 }}
>
<InputNumber
min={0}
bordered={false}
precision={0}
defaultValue={qxProps.max}
onBlur={(e) => handleChange(e, record, 'qxProps-max')}
placeholder="最大长度"
/>
</Form.Item>
</div>
);
case 'NUMBER':
return (
<div
key={record.id}
className="editable-cell"
>
<div className='range-cell'>
<Form.Item
name={'qxProps-min'}
style={{ margin: 0 }}
>
<InputNumber
precision={qxProps.precision || 0}
bordered={false}
size='small'
defaultValue={qxProps.min}
onBlur={(e) => handleChange(e, record, 'qxProps-min')}
style={{ width: '110px' }}
placeholder="最小值"
/>
</Form.Item>
-
<Form.Item
style={{ margin: 0 }}
name={'qxProps-max'}
>
<InputNumber
bordered={false}
size='small'
precision={qxProps.precision || 0}
defaultValue={qxProps.max}
onBlur={(e) => handleChange(e, record, 'qxProps-max')}
style={{ width: '110px' }}
placeholder="最大值"
/>
</Form.Item>
<Divider type="vertical" style={{ height: '16px' }} />
</div>
<div className='range-cell'>
<Form.Item
style={{ margin: 0 }}
name={'qxProps-precision'}
>
<InputNumber
bordered={false}
size='small'
precision={0}
min={0}
max={8}
defaultValue={qxProps.precision}
onBlur={(e) => handleChange(e, record, 'qxProps-precision')}
placeholder="小数位数"
/>
</Form.Item>
</div>
</div>
);
case 'TIME':
return (
<div
key={record.id}
className="editable-cell"
>
<div className='range-cell'>
<DatePicker
bordered={false}
size='small'
defaultValue={
qxProps?.min
? moment(qxProps.min)
: undefined
}
format={formatEnum[qxProps?.format]}
style={{ width: '110px' }}
showTime
onChange={(e) => handleChange(e, record, 'qxProps-min')}
placeholder="开始日期"
/>
-
<DatePicker
size='small'
bordered={false}
defaultValue={
qxProps?.min
? moment(qxProps.max)
: undefined
}
format={formatEnum[qxProps?.format]}
style={{ width: '110px' }}
showTime
onChange={(e) => handleChange(e, record, 'qxProps-max')}
placeholder="结束日期"
/>
<Divider type="vertical" style={{ height: '16px' }} />
</div>
<div className='range-cell'>
<Select
size='small'
style={{ width: '90px' }}
bordered={false}
placeholder="格式"
popupMatchSelectWidth={false}
defaultValue={qxProps.format}
onSelect={(e) =>
handleChange(e, record, 'qxProps-format')
}
>
{timeFormat.map((item) => {
return (
<Select.Option
style={{ width: '200px' }}
key={item.value}
value={item.value}
>
{item.label}
</Select.Option>
);
})}
</Select>
</div>
</div>
);
case 'FORM':
case 'USER':
case 'ORG':
return (
<div
className='editable-cell'
key={record.id}
>
<QxFormSelect
value={qxProps}
appId={props?.appId}
placeholder={'选择表单'}
popupOnBody={true}
onChange={(val: any) => {
handleChange(val, record, 'qxProps');
}}
disabled={['USER', 'ORG'].includes(record.type)}
request={props?.request}
/>
</div>
);
default:
}
},
};
const require = {
title: '必填',
dataIndex: 'required',
editable: true,
key: 'required',
width: '5%',
render: (text, record) => (
<Form.Item
name={'required' + record.id}
style={{ margin: 0 }}
>
<Select
defaultValue={text || false}
bordered={false}
disabled={record.disabled}
onSelect={(e) => props.handleChange(e, record, 'required')}
>
<Select.Option value={true}>是</Select.Option>
<Select.Option value={false}>否</Select.Option>
</Select>
</Form.Item >
),
};
if (nodeType !== 'END') {
columns.splice(3, 0, range, require)
}
// const isDeveloper = window.sessionStorage.getItem('DEVELOPER_MODE') === '1';
const [isShowJson, setIsShowJson] = useState<any>(false);
const [midData, setMidData] = useState<any>([]);
useEffect(() => {
if (visible) {
if (data.length == 0) {
handleAdd('');
}
}
}, [visible]);
//展开 所有子节点
const generateTreeData = useCallback(
(_data: any[], _keywords?: string): any[] => {
const _treeNode: any[] = [];
(_data || []).map((item) => {
if (item.pid == _keywords) {
const child = generateTreeData(_data, item.id);
if (
child?.length &&
(item?.type == 'OBJECT' || item?.type == 'ARRAY')
) {
item.child = child;
}
_treeNode.push(item);
}
});
return _treeNode;
},
[],
);
//每行拖拽渲染
const onRow = () => ({
draggable: true,
style: { cursor: 'move' },
onDragStart: (ev: any) => {
ev.persist();
ev.dataTransfer.effectAllowed = 'move';
ev.dataTransfer.setData('text', ev.target.getAttribute('data-row-key'));
},
onDragEnter: (ev: any) => {
ev.persist();
let nodes;
switch (ev.target.tagName) {
case 'TD':
nodes = ev.target.parentNode.childNodes;
break;
case 'DIV':
nodes = ev.target.parentNode.parentNode.childNodes;
break;
default:
nodes = [];
}
nodes?.forEach(
(item: any) => (item.style.borderTop = '2px solid #1764FF'),
);
},
onDragLeave: (ev: any) => {
ev.persist();
let nodes;
switch (ev.target.tagName) {
case 'TD':
nodes = ev.target.parentNode.childNodes;
break;
case 'DIV':
nodes = ev.target.parentNode.parentNode.childNodes;
break;
default:
nodes = [];
}
nodes?.forEach((item: any) => (item.style.borderTop = ''));
},
onDrop: (ev: any) => {
ev.preventDefault();
ev.stopPropagation();
const dragId = ev.dataTransfer.getData('text');
let dropCol;
switch (ev.target.tagName) {
case 'TD':
dropCol = ev.target.parentNode;
break;
case 'DIV':
dropCol = ev.target.parentNode.parentNode;
break;
default:
dropCol = ev.target;
}
// dropCol.parentNode.insertBefore(dragCol, dropCol); // DOM操作
const dropId =
dropCol.getAttribute('data-row-key') ||
dropCol.getAttribute('id')?.slice(4);
const _newTreeData = cloneDeep(treeData);
const dragIndex = _newTreeData.findIndex(
(item: any) => item.id === dragId,
); //要drop的index
const dropIndex = _newTreeData.findIndex(
(item: any) => item.id === dropId,
); // 要drop到目标的index
if (dragIndex == dropIndex || !dragId || !dropId) {
dropCol.childNodes.forEach((item: any) => (item.style.borderTop = ''));
dropCol.parentNode.childNodes.forEach(
(item: any) => (item.style.borderTop = ''),
);
return;
} else {
if (!!_newTreeData[dropIndex].pid) {
const pidIndex = _newTreeData.findIndex(
(item: any) => item.id === _newTreeData[dropIndex].pid,
);
if (_newTreeData[pidIndex].type == 'ARRAY') {
message.warning('数组有且只有一个子节点,请重试!');
dropCol.childNodes.forEach(
(item: any) => (item.style.borderTop = ''),
);
dropCol.parentNode.childNodes.forEach(
(item: any) => (item.style.borderTop = ''),
);
return;
}
_newTreeData[dragIndex].pid = _newTreeData[dropIndex].pid;
} else {
_newTreeData[dragIndex].pid = '';
}
const todoData = _newTreeData.splice(dragIndex, 1); //获取要drop的item
_newTreeData.splice(dropIndex, 0, todoData[0]); // 插入
}
const _newData = generateTreeData(_newTreeData, '');
handleDrop(_newData);
dropCol.childNodes.forEach((item: any) => (item.style.borderTop = ''));
dropCol.parentNode.childNodes.forEach(
(item: any) => (item.style.borderTop = ''),
);
},
onDragOver: (ev: any) => ev.preventDefault(),
});
const onCancel = () => {
setIsShowJson(false);
handleCancel(inputKey + 1);
};
const submit = () => {
try {
JSON.parse(midData);
handleDrop(JSON.parse(midData));
message.success('保存成功');
setTimeout(() => {
onCancel();
}, 0);
} catch (error) {
message.error('JSON转成数据有错误请重试!');
return;
}
};
const onOpen = () => {
setMidData(JSON.stringify(data, null, 4));
setIsShowJson(true);
};
const onJsonChange = (_data: any) => {
setMidData(_data);
};
return (
<>
<Modal
title="高级参数设计器"
open={visible}
destroyOnClose
footer={null}
// onOk={handleCancel}
onCancel={onCancel}
width={'100%'}
>
{isShowJson ? (
<Row>
<Col span={12}>
<Button
type="link"
icon={<SnippetsOutlined />}
onClick={() => setIsShowJson(false)}
>
{'表单参数设置'}
</Button>
</Col>
</Row>
) : (
<Row>
<Col span={12}>
<Button type="link" icon={<QxBaseIcon style={{ fontSize: 16 }} type={'qx-icon-flow-code'} />} onClick={() => onOpen()}>
JSON参数设置
</Button>
</Col>
<Col
className="border-none"
span={12}
style={{
display: 'flex',
flexDirection: 'row-reverse',
marginBottom: '10px',
textAlign: 'center',
}}
>
<Input
className={'qx-parameter-setting--search'}
placeholder="按名称搜索"
prefix={<QxBaseIcon type={'icon-app-search-line'} />}
onChange={onChange}
style={{ width: 219 }}
/>
</Col>
</Row>
)}
{
isShowJson ? (
<>
<JSONEditor value={midData} onChange={onJsonChange} />
<Col
className="border-none"
span={24}
style={{
display: 'flex',
flexDirection: 'row-reverse',
marginBottom: '10px',
textAlign: 'center',
}}
>
<Space>
<Button onClick={() => onCancel()}>{'取消'}</Button>
<Button type="primary" onClick={() => submit()}>
{'确定'}
</Button>
</Space>
</Col>
</>
) : (
<Table
// components={components}
expandable={{
defaultExpandAllRows: true,
childrenColumnName: 'child'
}}
onRow={() => onRow()}
scroll={{ x: 960 }}
// rowClassName={() => 'editable-row'}
bordered
pagination={false}
size={'small'}
// @ts-ignore
columns={columns}
dataSource={data}
rowKey={'id'}
/>
)
}
</Modal>
</>
);
};
export default ParameterModal;