index.tsx
2.74 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
import React, { useEffect, useState } from 'react';
import type { ValueAssignmentPopupProps } from '../qx-base-condition-item';
import { QxBaseConditionItem } from '../qx-base-condition-item';
import './index.less';
export enum FieldBaseType {
STRING = 'TEXT',
DOUBLE = 'NUM',
YEAR_SEC = 'DATE',
}
export const QxBaseCondition: React.FC<QxBaseConditionProps> = (props) => {
const [localOptions, setLocalOptions] = useState(props.options || []);
const getDefaultConditionOptions = (item: QxBaseConditionField) => ({
...item,
// @ts-ignore
fieldGroupType: FieldBaseType[item.fieldType] || 'TEXT',
mappingValues: [],
opt: 'IS',
valuesObj: [],
});
const handleItemChange = (val: any, key: number) => {
props.onChange?.(
localOptions?.map((item, idx) => {
if (idx === key) return val;
return props.value?.[idx]
? props.value?.[idx]
: getDefaultConditionOptions(item);
}),
);
};
const handleDelete = (key: number) => {
localOptions.splice(key, 1);
setLocalOptions([...localOptions]);
props.onChange?.([...localOptions]);
};
useEffect(() => {
setLocalOptions(props.options);
}, [props.options]);
const RenderCondition = localOptions?.map((item, key) => (
<div className="qx-base-condition-list-item" key={item.code || key}>
{(props?.showIdx ?? true) && (
<span className="qx-base-condition-list-item__idx">{key + 1}.</span>
)}
<QxBaseConditionItem
key={item.code || key}
mode={props.mode}
{...item}
value={Object.assign(
{},
getDefaultConditionOptions(item),
props.value?.[key] || {},
)}
field={item}
onChange={(val) => handleItemChange(val, key)}
remove={() => handleDelete(key)}
ValueAssignmentPopup={
item.showValueAssignmentPopup ?? true
? props.ValueAssignmentPopup
: undefined
}
/>
</div>
));
return (
<div className="qx-base-condition">
<div className="qx-base-condition-list">{RenderCondition}</div>
</div>
);
};
export interface QxBaseConditionField {
field: string;
fieldType: string;
fieldName: string;
fieldGroupType: string;
qxProps?: string;
extract?: any;
}
export interface QxBaseConditionOptionsProps extends QxBaseConditionField {
showValueAssignmentPopup?: boolean;
isMultiple?: boolean;
isRange?: boolean;
[key: string]: any;
}
export type QxBaseConditionMode = 'condition' | 'variable';
export interface QxBaseConditionProps {
showIdx?: boolean;
mode: QxBaseConditionMode;
options: QxBaseConditionOptionsProps[];
value: any[];
onChange?: (val: any[]) => void;
ValueAssignmentPopup?: React.FC<ValueAssignmentPopupProps>;
}