@@ -24,6 +24,27 @@ interface Item {
2424 del : boolean
2525}
2626
27+ const normalizeConditionRule = ( item ?: Partial < Item > ) => ( {
28+ id : item ?. id || generateUUID ( 8 ) ,
29+ left_var : typeof item ?. left_var === 'string' ? item . left_var : '' ,
30+ left_label : typeof item ?. left_label === 'string' ? item . left_label : '' ,
31+ comparison_operation : typeof item ?. comparison_operation === 'string' ? item . comparison_operation : '' ,
32+ right_value_type : item ?. right_value_type === 'ref' ? 'ref' : 'input' ,
33+ right_value : typeof item ?. right_value === 'string' ? item . right_value : '' ,
34+ right_label : typeof item ?. right_label === 'string' ? item . right_label : '' ,
35+ } ) ;
36+
37+ const normalizeConditionBranches = ( value ) => {
38+ if ( ! Array . isArray ( value ) ) return [ ] ;
39+ return value . map ( ( branch ) => ( {
40+ id : branch ?. id || generateUUID ( 8 ) ,
41+ operator : branch ?. operator === 'or' ? 'or' : 'and' ,
42+ conditions : Array . isArray ( branch ?. conditions )
43+ ? branch . conditions . map ( ( item ) => normalizeConditionRule ( item ) )
44+ : [ ] ,
45+ } ) ) ;
46+ } ;
47+
2748
2849const Item = ( { nodeId, item, index, del, required, varErrors, onUpdateItem, onDeleteItem } ) => {
2950 const { t } = useTranslation ( 'flow' ) ;
@@ -159,6 +180,11 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
159180 const { t } = useTranslation ( 'flow' ) ; // 获取翻译函数
160181 const [ value , setValue ] = useState ( [ ] ) ;
161182 const [ required , setRequired ] = useState ( false ) ;
183+ const normalizedParamValue = useMemo ( ( ) => normalizeConditionBranches ( paramItem ?. value ) , [ paramItem ?. value ] ) ;
184+ const needsParamNormalization = useMemo (
185+ ( ) => JSON . stringify ( normalizedParamValue ) !== JSON . stringify ( paramItem ?. value ?? [ ] ) ,
186+ [ normalizedParamValue , paramItem ?. value ]
187+ ) ;
162188
163189 const handleAddCondition = ( ) => {
164190 setRequired ( false ) ;
@@ -170,12 +196,20 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
170196 } ;
171197
172198 useEffect ( ( ) => {
173- if ( paramItem . value && paramItem . value . length ) {
174- setValue ( paramItem . value ) ;
175- } else {
176- handleAddCondition ( ) ;
199+ if ( normalizedParamValue . length ) {
200+ setValue ( normalizedParamValue ) ;
201+ if ( needsParamNormalization ) {
202+ onChange ( normalizedParamValue ) ;
203+ }
204+ return ;
177205 }
178- } , [ ] ) ;
206+ setValue ( ( current ) => {
207+ if ( current . length ) return current ;
208+ const initialValue = [ { id : generateUUID ( 8 ) , operator : 'and' , conditions : [ ] } ] ;
209+ onChange ( initialValue ) ;
210+ return initialValue ;
211+ } ) ;
212+ } , [ needsParamNormalization , normalizedParamValue , onChange , paramItem ?. value ] ) ;
179213
180214 const deleteCondition = ( id ) => {
181215 setValue ( ( val ) => {
@@ -219,8 +253,8 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
219253 setTimeout ( ( ) => {
220254 setRequired ( true ) ;
221255 } , 100 ) ;
222- if ( paramItem . value . length === 0 ) return t ( 'conditionBranchCannotBeEmpty' ) ; // 条件分支不可为空
223- const res = paramItem . value . some ( ( item ) => {
256+ if ( value . length === 0 ) return t ( 'conditionBranchCannotBeEmpty' ) ; // 条件分支不可为空
257+ const res = value . some ( ( item ) => {
224258 if ( ! item . conditions . length ) return true ;
225259 return item . conditions . some ( ( cds ) => {
226260 if ( ! cds . left_label ) return true ;
@@ -237,7 +271,7 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
237271 } ) ;
238272
239273 return ( ) => onValidate ( ( ) => { } ) ;
240- } , [ paramItem . value ] ) ;
274+ } , [ onValidate , t , value ] ) ;
241275
242276 // 校验变量是否可用
243277 const { flow } = useFlowStore ( ) ;
@@ -265,7 +299,7 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
265299 useEffect ( ( ) => {
266300 onVarEvent && onVarEvent ( validateVarAvailble ) ;
267301 return ( ) => onVarEvent && onVarEvent ( ( ) => { } ) ;
268- } , [ paramItem , value ] ) ;
302+ } , [ onVarEvent , paramItem , value ] ) ;
269303
270304 // Update Preset Questions
271305 // const [_, forceUpdate] = useState(false)
@@ -361,4 +395,4 @@ export default function ConditionItem({ nodeId, node, data: paramItem, onChange,
361395 </ Button >
362396 </ div >
363397 ) ;
364- }
398+ }
0 commit comments