Commit 2dc87cfe42f9085f1360dd990cd6043e19bcd37f
Merge remote-tracking branch 'origin/feature/dataflow' into feature/dataflow
Showing
4 changed files
with
152 additions
and
55 deletions
src/qx-field/context.ts
0 → 100644
1 | +import { createContext } from 'react' | |
2 | + | |
3 | +export interface QxFieldExtract { | |
4 | + widget: string; | |
5 | + $base?: boolean; | |
6 | + allowSelect?: boolean; | |
7 | + fieldKey: string; | |
8 | + fieldType: string; | |
9 | + fieldGroupType?: string; | |
10 | + relId?: string; | |
11 | + [key: string]: any; | |
12 | +} | |
13 | + | |
14 | +export interface QxField { | |
15 | + name: string; | |
16 | + code: string; | |
17 | + extract: QxFieldExtract; | |
18 | +} | |
19 | + | |
20 | +export interface FieldContextType { | |
21 | + fields: QxField[]; | |
22 | + setFields: (fields: QxField[]) => void | |
23 | +} | |
24 | + | |
25 | +export const FieldContext = createContext<FieldContextType>({} as FieldContextType) | ... | ... |
src/qx-field/src/provider/index.tsx
0 → 100644
1 | +import React, { useState } from 'react'; | |
2 | +import { FieldContext, QxField } from '../../context'; | |
3 | + | |
4 | +export const QxFieldProvider: React.FC<QxFieldProviderProps> = (props) => { | |
5 | + const [fields, setFields] = useState<QxField[]>([]); | |
6 | + | |
7 | + return ( | |
8 | + <FieldContext.Provider value={{ fields, setFields }}> | |
9 | + {props.children} | |
10 | + </FieldContext.Provider> | |
11 | + ); | |
12 | +}; | |
13 | + | |
14 | +interface QxFieldProviderProps { | |
15 | + children?: React.ReactNode; | |
16 | +} | ... | ... |
... | ... | @@ -2,7 +2,7 @@ import { QxWidgetIcon } from '@qx/common'; |
2 | 2 | import { Collapse, Dropdown, Empty, Tag } from 'antd'; |
3 | 3 | import cls from 'classnames'; |
4 | 4 | import { cloneDeep } from 'lodash-es'; |
5 | -import React, { useEffect, useState } from 'react'; | |
5 | +import React, { useEffect, useMemo, useState } from 'react'; | |
6 | 6 | import { FieldBaseType } from '../qx-base-condition'; |
7 | 7 | import { QxBaseIcon } from '../qx-base-icon'; |
8 | 8 | import { request } from '../utils'; |
... | ... | @@ -99,60 +99,75 @@ export const useNodeFieldDisplay = ({ |
99 | 99 | const [inputDisplay, setInputDisplay] = useState<React.ReactNode>(); |
100 | 100 | const [optionalNodes, setOptionalNodes] = useState<INode[]>([]); // 根据 fieldType 过滤后的 nodes |
101 | 101 | |
102 | - const getId = (val?: string) => { | |
103 | - if (!val) return; | |
102 | + const getIds = (val?: string) => { | |
103 | + if (!val) return []; | |
104 | + let newVal = val; | |
104 | 105 | const startIndex = val.indexOf('|') + 1; |
105 | 106 | const endIndex = val.indexOf('}'); |
106 | 107 | if (startIndex < endIndex) { |
107 | - return val.substring(startIndex, endIndex); | |
108 | + newVal = val.substring(startIndex, endIndex); | |
108 | 109 | } |
109 | - return null; | |
110 | - }; | |
111 | - | |
112 | - const getDisplayConfig = (value: string, nodes: INode[] = optionalNodes) => { | |
113 | - const itemId = getId(value); | |
114 | - if (!itemId) return []; | |
115 | - let displayConfig: any[] = []; | |
116 | - let n = true; | |
117 | - let index = 0; | |
118 | - while (n && index <= 20) { | |
119 | - displayConfig = []; | |
120 | - // eslint-disable-next-line @typescript-eslint/no-use-before-define | |
121 | - const curNode = cloneDeep(nodes[index]) || {}; | |
122 | - displayConfig.push({ | |
123 | - title: curNode.name, | |
124 | - icon: curNode.icon, | |
125 | - ...curNode, | |
126 | - }); | |
127 | - // eslint-disable-next-line @typescript-eslint/no-use-before-define | |
128 | - recursionNodeResult(curNode.data?.result); | |
129 | - index++; | |
130 | - } | |
131 | - | |
132 | - function recursionNodeResult(result: FiledType[], idx = 1) { | |
133 | - if (Array.isArray(result)) { | |
134 | - for (let i = 0; i < result.length; i++) { | |
135 | - const item = result[i]; | |
136 | - if (!n) return; | |
137 | 110 | |
138 | - displayConfig.splice(idx, 1, item); | |
139 | - displayConfig = displayConfig.slice(0, idx + 1); | |
111 | + const nodeId = val.split('|')[0].substring(2); | |
140 | 112 | |
141 | - if (itemId === item.id) { | |
142 | - n = false; | |
143 | - return; | |
144 | - } | |
145 | - | |
146 | - if (Array.isArray(item.child)) { | |
147 | - recursionNodeResult(item.child, idx + 1); | |
148 | - } | |
149 | - } | |
113 | + return [nodeId, ...newVal.split('.').reduce<string[]>((pre, cur, idx) => { | |
114 | + if (idx === 0) { | |
115 | + pre.push(`${nodeId}|${cur}`); | |
150 | 116 | } else { |
151 | - n = false; | |
117 | + pre.push(`${pre[idx - 1]}.${cur}`); | |
152 | 118 | } |
153 | - } | |
154 | 119 | |
155 | - return displayConfig; | |
120 | + return pre; | |
121 | + }, [])]; | |
122 | + }; | |
123 | + | |
124 | + const getDisplayConfig = (value: string, nodes: INode[] = optionalNodes) => { | |
125 | + const itemIds = getIds(value); | |
126 | + | |
127 | + if (!itemIds || !itemIds.length) return []; | |
128 | + | |
129 | + // let displayConfig: any[] = []; | |
130 | + // let n = true; | |
131 | + // let index = 0; | |
132 | + // while (n && index <= 20) { | |
133 | + // displayConfig = []; | |
134 | + // // eslint-disable-next-line @typescript-eslint/no-use-before-define | |
135 | + // const curNode = cloneDeep(nodes[index]) || {}; | |
136 | + // displayConfig.push({ | |
137 | + // title: curNode.name, | |
138 | + // icon: curNode.icon, | |
139 | + // ...curNode, | |
140 | + // }); | |
141 | + // // eslint-disable-next-line @typescript-eslint/no-use-before-define | |
142 | + // recursionNodeResult(curNode.data?.result); | |
143 | + // index++; | |
144 | + // } | |
145 | + | |
146 | + // function recursionNodeResult(result: FiledType[], idx = 1) { | |
147 | + // if (Array.isArray(result)) { | |
148 | + // for (let i = 0; i < result.length; i++) { | |
149 | + // const item = result[i]; | |
150 | + // if (!n) return; | |
151 | + | |
152 | + // displayConfig.splice(idx, 1, item); | |
153 | + // displayConfig = displayConfig.slice(0, idx + 1); | |
154 | + | |
155 | + // if (itemId === item.id) { | |
156 | + // n = false; | |
157 | + // return; | |
158 | + // } | |
159 | + | |
160 | + // if (Array.isArray(item.child)) { | |
161 | + // recursionNodeResult(item.child, idx + 1); | |
162 | + // } | |
163 | + // } | |
164 | + // } else { | |
165 | + // n = false; | |
166 | + // } | |
167 | + // } | |
168 | + | |
169 | + // eslint-disable-next-line @typescript-eslint/no-use-before-define | |
170 | + return itemIds.map((id) => resultFieldMaps[id]); | |
156 | 171 | }; |
157 | 172 | |
158 | 173 | const genDisplayDom = (value: string, nodes: INode[] = optionalNodes) => { |
... | ... | @@ -171,7 +186,7 @@ export const useNodeFieldDisplay = ({ |
171 | 186 | FieldMapType[item.type] && |
172 | 187 | !item.icon && |
173 | 188 | `[${FieldMapType[item.type]}]`} |
174 | - {item.title} | |
189 | + {item.title || item.name} | |
175 | 190 | </span> |
176 | 191 | {idx !== displayConfig.length - 1 && ( |
177 | 192 | <span className="qx-node-select-input__content-item__arrow"> |
... | ... | @@ -274,22 +289,36 @@ export const useNodeFieldDisplay = ({ |
274 | 289 | return sourceParentNodes; |
275 | 290 | }; |
276 | 291 | |
277 | - const correctionNodeField = (fields: FiledType[] | FiledType) => { | |
292 | + /** | |
293 | + * 统一字段格式 | |
294 | + */ | |
295 | + const correctionNodeField = ( | |
296 | + fields: FiledType[] | FiledType, | |
297 | + nodeId: string, | |
298 | + parent?: FiledType | INode, | |
299 | + ) => { | |
278 | 300 | if (Array.isArray(fields)) { |
279 | 301 | for (let i = 0; i <= fields.length; i++) { |
280 | - correctionNodeField(fields[i]); | |
302 | + correctionNodeField(fields[i], nodeId, parent); | |
281 | 303 | } |
282 | 304 | } else if (fields) { |
283 | 305 | Object.assign(fields, { |
284 | 306 | ...fields, |
285 | 307 | name: fields.title, |
308 | + code: parent | |
309 | + ? `${parent.code}.${fields.code}` | |
310 | + : `${nodeId}|${fields.code}`, | |
286 | 311 | extract: { |
287 | 312 | ...(fields.extract || {}), |
288 | 313 | fieldType: fields.type, |
289 | 314 | fieldKey: fields.code, |
290 | 315 | }, |
291 | - child: correctionNodeField(fields.child!), | |
292 | 316 | }); |
317 | + fields.child = correctionNodeField( | |
318 | + fields.child!, | |
319 | + nodeId, | |
320 | + fields, | |
321 | + ) as FiledType[]; | |
293 | 322 | } |
294 | 323 | |
295 | 324 | return fields; |
... | ... | @@ -299,7 +328,10 @@ export const useNodeFieldDisplay = ({ |
299 | 328 | const targetParentNodes = await handleGetAppsFields(); |
300 | 329 | |
301 | 330 | for (let i = 0; i < targetParentNodes.length; i++) { |
302 | - correctionNodeField(targetParentNodes[i].data?.result || []); | |
331 | + correctionNodeField( | |
332 | + targetParentNodes[i].data?.result || [], | |
333 | + targetParentNodes[i].id, | |
334 | + ); | |
303 | 335 | } |
304 | 336 | |
305 | 337 | if (!limitTypes || !limitTypes.length) { |
... | ... | @@ -308,11 +340,14 @@ export const useNodeFieldDisplay = ({ |
308 | 340 | return; |
309 | 341 | } |
310 | 342 | |
343 | + /** | |
344 | + * 根据 limitType 获取可选择的字段 | |
345 | + */ | |
311 | 346 | function getEffectiveResult(result: FiledType[]) { |
312 | 347 | const newResult = []; |
313 | 348 | for (let i = 0; i < result.length; i++) { |
314 | 349 | const resultItem = result[i] || {}; |
315 | - correctionNodeField(resultItem); | |
350 | + // correctionNodeField(resultItem, nodeId, parent); | |
316 | 351 | |
317 | 352 | if (resultItem.child) { |
318 | 353 | resultItem.child = getEffectiveResult(resultItem.child); |
... | ... | @@ -355,6 +390,27 @@ export const useNodeFieldDisplay = ({ |
355 | 390 | renderInputDisplay([...newNodes]); |
356 | 391 | }; |
357 | 392 | |
393 | + const resultFieldMaps = useMemo(() => { | |
394 | + const resultMap: Record<string, FiledType | INode> = {}; | |
395 | + function genResultMap(result: FiledType[]) { | |
396 | + if (Array.isArray(result)) { | |
397 | + result.forEach((i) => { | |
398 | + resultMap[i.code] = i; | |
399 | + if (i.child) { | |
400 | + genResultMap(i.child); | |
401 | + } | |
402 | + }); | |
403 | + } | |
404 | + } | |
405 | + | |
406 | + for (let i = 0; i < optionalNodes.length; i++) { | |
407 | + resultMap[optionalNodes[i].id] = optionalNodes[i]; | |
408 | + genResultMap(optionalNodes[i]?.data?.result); | |
409 | + } | |
410 | + | |
411 | + return resultMap; | |
412 | + }, [optionalNodes]); | |
413 | + | |
358 | 414 | useEffect(() => { |
359 | 415 | getOptionalNodes(); |
360 | 416 | }, []); |
... | ... | @@ -365,6 +421,7 @@ export const useNodeFieldDisplay = ({ |
365 | 421 | renderInputDisplay, |
366 | 422 | getDisplayConfig, |
367 | 423 | inputDisplay, |
424 | + resultFieldMaps, | |
368 | 425 | }; |
369 | 426 | }; |
370 | 427 | |
... | ... | @@ -456,7 +513,6 @@ export const QxFlowNodeFieldSelector: React.FC<NodeFieldSelectProps> = ( |
456 | 513 | useNodeFieldDisplay(props); |
457 | 514 | |
458 | 515 | const getOptions = () => { |
459 | - console.log('optionalNodes', optionalNodes); | |
460 | 516 | return optionalNodes.map((node) => ({ |
461 | 517 | label: ( |
462 | 518 | <div className={cls('qx-node-select-dropdown-header')}> |
... | ... | @@ -485,7 +541,7 @@ export const QxFlowNodeFieldSelector: React.FC<NodeFieldSelectProps> = ( |
485 | 541 | }; |
486 | 542 | |
487 | 543 | const handleItemClick = (nodeKey: string, item: any) => { |
488 | - const newValue = '${' + `${nodeKey}|${item.id}` + '}'; | |
544 | + const newValue = '${' + item.code + '}'; | |
489 | 545 | props.onChange?.(newValue, item); |
490 | 546 | if (!props.children) { |
491 | 547 | renderInputDisplay(optionalNodes, newValue); | ... | ... |