index.tsx
21 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
import { QIXIAO_TOKEN } from '@/libs/token';
import { SYSTEM_WIDGETS } from '@/packages/qx-form-generator/src/utils/common';
import QxIcon from '@/packages/qx-icon';
import { RelMoreModal, RelSingleModal } from '@/pages/app-view/form';
import { EllipsisOutlined, SearchOutlined } from '@ant-design/icons';
import { QxSearch, QxTable } from '@qx/view-render';
import type { QxTableCellAPISchema } from '@qx/view-render/dist/table';
import {
Button,
Dropdown,
Empty,
Input,
Menu,
message,
Popover,
Tooltip,
Tree,
} from 'antd';
import _ from 'lodash';
import { customAlphabet } from 'nanoid';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { QxButton } from '@qx/view-render';
import './index.less';
export * from '@qx/view-render';
/*TODO 待细化*/
interface QxRuntimeTableProps {
appCode?: string;
funCode?: string;
viewCode?: string;
tableRef?: any;
tableLoading?: boolean;
dataSource: any[];
onClickBtn?: (code: any, data: any) => void;
total?: number;
searchApi?: any;
onSelect?: () => void;
onSelectAll?: () => void;
selectKeys?: string[];
mode?: any;
max?: number;
sort?: any[];
onChange?: (pagination: any, filters: any, sorter: any) => void;
[propName: string]: any;
bodyHeight?: number
}
const QxRuntimeTable: React.FC<QxRuntimeTableProps> = (props) => {
const {
appCode,
funCode,
viewCode,
tableRef,
tableLoading,
dataSource,
onChange = () => {},
onClickBtn = () => {},
total,
searchApi,
...otherProps
} = props;
const [rowData, setRowData] = useState<any>();
const [cellSchema, setCellSchema] = useState<any>();
const [clickInfo, setClickInfo] = useState<any>();
const [random, setRandom] = useState<string>(); //随机数
const relMoreModalRef = useRef({
open: (values: any) => {
console.log(values);
},
close: () => {},
});
const relSingleModalRef = useRef({
open: (values: any) => {
console.log(values);
},
close: () => {},
});
function getId() {
return customAlphabet('abcdefghijklmnopqistuvwxyz', 6)();
}
const handleCellClick = (record: any, schemaData: QxTableCellAPISchema, info?: any) => {
// console.log(record,schemaData,info)
setRowData(record); //点击行的数据
setCellSchema(schemaData); //列的schema
setClickInfo(info || '') // 点击标签的data_title id
setRandom(getId());
};
useEffect(() => {
if (cellSchema?.link) {
//详情链接 标题、描述
onClickBtn({ code: 'VIEW' }, rowData);
} else {
//1、关联记录以标签显示的时候,直接弹出该条数据详情
//2、关联记录以表格显示的时候,弹出关联记录表格
if (cellSchema?.widget === 'relSelector') {
if (cellSchema?.mode === 'TABLE' && (rowData[cellSchema?.dataIndex] || []).length) {
//关联记录 表格形式
relMoreModalRef.current.open({
appCode: appCode,
funCode: funCode,
viewCode: viewCode,
fieldName: cellSchema?.dataIndex,
dataId: rowData?.id, //当前行的id
idArr: rowData[cellSchema?.dataIndex], //当前行 用户所点击列的关联记录id数组
modalTitle: cellSchema?.title,
widget: cellSchema?.widget, //关联表的标识
});
} else {
//关联记录 标签形式
// const info = dataSource.filter((o: any) => o.id === rowData?.id)[0][
// cellSchema?.dataIndex + '_info_'
// ];
relSingleModalRef.current.open({
// 后端数据结构调整 这里去掉了relAppCode和relFuncode 改为在获取详情schema时返回
// appCode: cellSchema.relAppCode,
// funCode: cellSchema.relFunCode,
params: { appCode, funCode, fieldName: cellSchema?.dataIndex },
fieldName: cellSchema?.dataIndex,
dataId: rowData?.id, //当前行的id
relFormId: clickInfo?.id, //表的id
modalTitle: clickInfo?.data_title, //表名
});
}
} else if (cellSchema?.widget === 'subform' && cellSchema?.type === 'TABLE') {
//子表
relMoreModalRef.current.open({
appCode: appCode,
funCode: funCode,
viewCode: viewCode,
fieldName: cellSchema?.dataIndex,
dataId: rowData?.id, //当前行的id
modalTitle: cellSchema?.title,
widget: cellSchema?.widget, //子表的标识
});
}
}
}, [rowData, cellSchema, random]);
return (
<>
<QxTable
cRef={tableRef}
{...otherProps}
loading={tableLoading}
dataSource={dataSource}
onClickBtn={onClickBtn}
onCellClick={handleCellClick}
onChange={onChange}
onSelect={props.onSelect}
onSelectAll={props.onSelectAll}
selectKeys={props.selectKeys}
total={total}
mode={props.mode}
max={props.max}
sort={props?.sort}
onPage={props?.onPage}
QIXIAO_TOKEN={QIXIAO_TOKEN}
/>
<RelMoreModal cRef={relMoreModalRef} />
<RelSingleModal cRef={relSingleModalRef} />
</>
);
};
interface QxRuntimeTreeProps {
appCode?: string;
funCode?: string;
viewCode?: string;
tableLoading?: boolean;
dataSource: any;
onClickBtn: (code: object, data: any) => {};
searchApi: any;
onSelect?: () => {};
onChange?: (pagination: any, filters: any, sorter: any) => void;
[propName: string]: any;
treeSchema: any;
onTreeDrop: (nodeId: string, parent: string, next: string) => void;
}
const QxRuntimeTree: React.FC<QxRuntimeTreeProps> = (props) => {
const { treeSchema, dataSource, onClickBtn, onTreeDrop } = props;
const [expandedKeys, setExpandedKeys] = useState<any[]>([]); //默认展开的节点
const [keyword, setKeyword] = useState<string>('');
const [treeData, setTreeData] = useState<any[]>([]);
const [data, setData] = useState<any>([]);
// 拖拽节点 相关代码
// 根据parentNodeId得到parentNodeType
const parentNode = (parentNodeData: any, parentNodeId: string) => {
for (let i = 0; i < parentNodeData.length; i++) {
if (parentNodeId == parentNodeData[i].code) {
return parentNodeData[i].type;
}
if (parentNodeData[i].children) {
const res: any = parentNode(parentNodeData[i].children, parentNodeId);
if (res) {
return res;
}
}
}
};
const handleDrop = (val: any) => {
const { node, dragNode, dropToGap } = val;
const dragNodeId = dragNode.code;
let nextNodeId = node.__next;
let parentNodeId = node.__pId ? node.__pId : '*';
if (!dropToGap) {
parentNodeId = node.code ? node.code : '*';
nextNodeId = (node.children[0] && node.children[0].code) || null;
}
const parentNodeType = parentNode(dataSource, parentNodeId); // 根据parentNodeId得到parentNodeType
if (parentNodeType === 'PAGE') {
message.warning('不能移动至页面下');
return;
}
if (parentNodeId === '*' || dragNodeId === nextNodeId) {
return;
}
onTreeDrop(dragNodeId, parentNodeId, nextNodeId);
};
let codeArr: any = [];
/**
* 获取需要展开的节点
* @param arr nodesData
* @param expandLevel 需要展开的级别默认二级
* @param pId
*/
const loopTreeNode = (nodesData: any[], expandLevel: number) => {
let nextExpendLevel = expandLevel;
// if (expandLevel > 0) {
nextExpendLevel -= 1;
// }
// @ts-ignore
nodesData.forEach((item, index) => {
if (nextExpendLevel >= 0) {
codeArr.push(item.code);
}
item.key = item.code;
item.__next = nodesData[index + 1] ? nodesData[index + 1].code : null;
if (!item.children) {
item.isLeaf = true;
} else {
loopTreeNode(item.children, nextExpendLevel);
}
});
};
const getExpandedArr = (nodesData: any[]) => {
nodesData.forEach((item) => {
codeArr.push(item.code);
if (item.children) {
getExpandedArr(item.children);
}
});
};
const generateTreeData = useCallback((_data: any[], _keywords?: string): any[] => {
const _treeNode: any[] = [];
_data.map((item) => {
if (typeof item.visible === 'boolean' && !item.visible) {
return;
}
const _item: any = {
...item,
children: [],
};
if (item.children) {
_item.children = generateTreeData(item.children, _keywords);
}
_treeNode.push(_item);
});
return _treeNode;
}, []);
const filter = (word: string) => {
setKeyword(word);
const traverse = function (node: any) {
const childNodes = node.children || [];
node.visible = node.name.indexOf(word) > -1;
childNodes.forEach((child: any) => {
child.visible = child.name.indexOf(word) > -1;
traverse(child);
});
if (!node.visible && childNodes.length) {
node.visible = childNodes.some((child: any) => child.visible);
}
};
if (data) {
const _data = _.cloneDeep(data);
if (word != '') {
// @ts-ignore
_data.forEach((item) => {
traverse(item);
});
}
setTreeData(generateTreeData(_data, word));
}
};
// 搜索框事件
const handleChange = (e: { type: string; target: { value: string } }) => {
const keywordText = e.target.value.replace(/(^\s*)|(\s*$)/g, '');
if (e.type === 'click' && e.target.value === '' && keyword !== '') {
// 清空
setData(dataSource);
setTreeData(dataSource);
} else {
if (keywordText) {
filter(keywordText);
} else {
setData(dataSource);
setTreeData(dataSource);
}
}
};
const handleSearch = (e: React.KeyboardEvent<HTMLInputElement>) => {
// @ts-ignore
const keywordText = e.target.value.replace(/(^\s*)|(\s*$)/g, '');
if (keywordText) {
filter(keywordText);
} else {
setData(dataSource);
setTreeData(dataSource);
}
};
useEffect(() => {
if (!dataSource || !treeSchema || !dataSource.length) {
return;
}
setData(dataSource);
setTreeData(dataSource);
const config = treeSchema?.tree || {};
if (!config?.expandLevel && config?.expandLevel === 0) {
codeArr = [];
getExpandedArr(dataSource || []);
setExpandedKeys(codeArr);
} else {
codeArr = [];
loopTreeNode(dataSource || [], config?.expandLevel);
setExpandedKeys(codeArr);
}
}, [dataSource, treeSchema]);
const getBtnDom = (val: any, filterData: any, schema: any) => {
if (!filterData?.extract?.[val.flag]) {
return null;
}
let icon = val.icon;
let name = val.name;
if (schema?.tree?.style === 'text') {
icon = '';
}
if (icon && (schema?.tree?.style === 'icon' || !schema?.tree?.style)) {
name = '';
}
let btnElem;
if (val.name.length > 4) {
btnElem = (
<QxButton
key={val.code}
color={val.color}
icon={icon}
type={'link'}
size={'small'}
disabled={val.disabled}
tooltip={val.tooltip || (name && val.name)}
onClick={() => {
onClickBtn({ code: val.code, ...val }, filterData);
}}
>
{name ? name.substring(0, 4) + '...' : name}
</QxButton>
);
} else {
btnElem = (
<QxButton
key={val.code}
color={val.color}
icon={icon}
type={'link'}
size={'small'}
disabled={val.disabled}
tooltip={val.tooltip}
onClick={() => {
onClickBtn({ code: val.code, ...val }, filterData);
}}
>
{name}
</QxButton>
);
}
if (schema?.tree?.style === 'icon' || !schema?.tree?.style) {
return (
<Tooltip key={val.code} title={val.name}>
{btnElem}
</Tooltip>
);
} else {
return btnElem;
}
};
// 渲染节点
const renderTitle = (nodeData: any) => {
const btnList = (treeSchema?.tree?.action?.renderData?.buttons || []).filter((item: any) => {
if (!!nodeData?.extract?.[item.flag]) {
return item;
}
});
// 超过 6 个则放入更多操作
const MAX = 6;
const isOverMax = btnList?.length > MAX + 1;
const isEqualMax = btnList?.length === MAX + 1;
return (
<>
{nodeData?.level > 1 && (
<QxIcon
type={'icon-pailie'}
style={{
fontSize: 18,
color: '#999',
marginRight: '8px',
}}
/>
)}
{nodeData.name && nodeData.name.length > 30 ? (
<Tooltip key={nodeData.code} title={nodeData.name}>
<span>{nodeData.name.substring(0, 30) + '...'}</span>
</Tooltip>
) : (
<span>{nodeData.name}</span>
)}
<div className={'qx-tree-btn-list'}>
{_.take(btnList, isEqualMax ? MAX + 1 : MAX).map((val: any) => {
return getBtnDom(val, nodeData, treeSchema);
})}
{isOverMax && (
<Dropdown
overlayClassName="qx-tree-drop-down"
placement="bottomRight"
overlay={
<Menu>
{_.drop(btnList, MAX).map((val: any) => {
return (
<Menu.Item key={val.code}>{getBtnDom(val, nodeData, treeSchema)}</Menu.Item>
);
})}
</Menu>
}
getPopupContainer={(trigger: any) => trigger.parentNode}
>
<Button className={'qx-tree-drop-down-btn'} type="text">
<EllipsisOutlined />
</Button>
</Dropdown>
)}
</div>
</>
);
};
return (
<div className={'qx-tree-list'} style={{ padding: '20px', backgroundColor: '#fff' }}>
{data && data.length > 0 && treeSchema?.tree?.searchSwitch && (
<Input
className={'qx-selector-sub-search'}
style={{
borderBottom: '1px solid #f0f0f0',
}}
placeholder={'请输入关键字,按回车键搜索'}
allowClear
prefix={<SearchOutlined style={{ marginTop: '4px' }} />}
onChange={(e) => {
handleChange(e);
}}
onPressEnter={(e) => {
handleSearch(e);
}}
/>
)}
<>
{treeData && treeData.length > 0 ? (
<Tree
treeData={treeData}
fieldNames={{
title: 'name',
key: 'code',
children: 'children',
}}
titleRender={(nodeData) => renderTitle(nodeData)}
blockNode={true}
onDrop={handleDrop}
draggable={
treeSchema?.tree?.dragSwitch
? {
nodeDraggable: (node: any) => {
if (node.pid === '*') {
return false;
}
return true;
},
}
: false
}
selectable
defaultExpandedKeys={expandedKeys}
expandedKeys={expandedKeys}
checkStrictly={true}
// @ts-ignore
onExpand={(keys) => setExpandedKeys(keys)}
/>
) : (
<Empty className={'qx-tree-list__empty'} image={Empty.PRESENTED_IMAGE_SIMPLE} />
)}
</>
</div>
);
};
const QxSearchForm = (props: any) => {
const widgets = { ...SYSTEM_WIDGETS, ...(props.widgets || {}) };
return <QxSearch {...props} widgets={widgets} />;
};
// 快捷查询 功能相关代码
interface QxQuickSearchTabsProps {
list: any[];
tabActiveCode: string | undefined;
onChange: (value: any) => void;
}
// scroll状态
export enum SCROLL {
start,
middle,
end,
}
const QxQuickSearchTabs: React.FC<QxQuickSearchTabsProps> = (props: any) => {
const { list, tabActiveCode, onChange } = props;
const [tabsIcon, setTabsIcon] = useState<boolean>(false);
const [scroll, setScroll] = useState<SCROLL>(SCROLL.start);
/* useEffect(() => {
if (!list || !list.length) {
return;
}
onChange(list[0].code);
}, [list]);*/
const getQuickSearchScroll = () => {
const tabsDom: any = document?.getElementById('quickSearchTabs');
if (tabsDom?.offsetWidth - tabsDom?.scrollWidth < 0) {
setTabsIcon(true);
const translate = tabsDom.style?.transform;
const translateNum = translate.split('(').pop().split('p').shift();
if (Number(translateNum) == 0) {
setScroll(SCROLL.start);
} else if (Math.abs(Number(translateNum)) > 0) {
if (
Math.abs(Number(translateNum)) == Math.abs(tabsDom?.offsetWidth - tabsDom?.scrollWidth)
) {
setScroll(SCROLL.end);
} else {
setScroll(SCROLL.middle);
}
}
} else {
setTabsIcon(false);
}
};
const handleClick = (changedCode: any, index: number, e: any) => {
const tabItemLeft = e.currentTarget.offsetLeft - 20;
const tabItemWidth = e.currentTarget.clientWidth;
const tabsDom: any = document?.getElementById('quickSearchTabs');
const translate = tabsDom.style?.transform;
const translateNum = Math.abs(Number(translate.split('(').pop().split('p').shift() || 0));
let widget = translateNum + tabsDom.clientWidth - (tabItemLeft + tabItemWidth);
if (widget > 0) {
if (translateNum === 0) {
tabsDom.style.cssText = 'transform: translate(0px,0px);';
} else {
widget = index > 0 ? tabItemLeft + 20 : tabItemLeft;
if (translateNum > tabItemLeft) {
tabsDom.style.cssText = 'transform: translate(' + -widget + 'px,0px);';
}
}
} else if (widget < 0) {
widget = translateNum + Math.abs(widget);
tabsDom.style.cssText = 'transform: translate(' + -widget + 'px,0px);';
}
if (changedCode === tabActiveCode) return;
onChange(changedCode);
};
const scrollPrev = () => {
const tabsDom: any = document?.getElementById('quickSearchTabs');
const translate = tabsDom.style?.transform;
const translateNum = Math.abs(Number(translate.split('(').pop().split('p').shift() || 0));
let widget = translateNum - tabsDom?.offsetWidth;
if (widget < 0) {
widget = 0;
setScroll(SCROLL.start);
} else {
setScroll(SCROLL.middle);
}
tabsDom.style.cssText = 'transform: translate(' + -widget + 'px,0px);';
};
const scrollNext = () => {
const tabsDom: any = document?.getElementById('quickSearchTabs');
const translate = tabsDom.style?.transform;
const translateNum = Math.abs(Number(translate.split('(').pop().split('p').shift() || 0));
let widget = translateNum + tabsDom?.offsetWidth;
if (widget > Math.abs(tabsDom?.offsetWidth - tabsDom?.scrollWidth)) {
widget = Math.abs(tabsDom?.offsetWidth - tabsDom?.scrollWidth);
setScroll(SCROLL.end);
} else {
setScroll(SCROLL.middle);
}
tabsDom.style.cssText = 'transform: translate(' + -widget + 'px,0px);';
};
useEffect(() => {
getQuickSearchScroll();
});
useEffect(() => {
if (!tabActiveCode) {
return;
}
const idx = list.findIndex((val: any) => val.code === tabActiveCode);
const tabsDom: any = document?.getElementById('quickSearchTabs');
if (idx === 0) {
tabsDom.style.cssText = 'transform: translate(0px,0px);';
}
}, [tabActiveCode]);
return (
<div
className={'qx-view-quick-search-container'}
style={{
padding: tabsIcon ? '0 20px' : 0,
}}
>
{tabsIcon && (
<Button
className={'left-button'}
type="text"
icon={<QxIcon type={'icon-tag-left-translate'} style={{ fontSize: 10 }} />}
disabled={scroll == SCROLL.start}
onClick={scrollPrev}
/>
)}
<div id="quickSearchTabs" className="qx-view-quick-search">
{list.map((pane: any, index: number) =>
pane.title && pane.title.length > 6 ? (
<Popover key={pane.code} content={pane.title} title={null}>
<div
key={pane.code}
className={`qx-view-quick-search__item ${
tabActiveCode === pane.code ? 'selected' : null
}`}
onClick={(e) => handleClick(pane.code, index, e)}
>
{pane.title.substring(0, 5) + '...'}
</div>
</Popover>
) : (
<div
key={pane.code}
className={`qx-view-quick-search__item ${
tabActiveCode === pane.code ? 'selected' : null
}`}
onClick={(e) => handleClick(pane.code, index, e)}
>
{pane.title}
</div>
),
)}
</div>
{tabsIcon && (
<Button
className={'right-button'}
type="text"
icon={<QxIcon type={'icon-tag-right-translate'} style={{ fontSize: 10 }} />}
disabled={scroll == SCROLL.end}
onClick={scrollNext}
/>
)}
</div>
);
};
export { QxSearchForm, QxRuntimeTable, QxRuntimeTree, QxQuickSearchTabs };