|
@@ -1,4 +1,4 @@
|
|
-import React, { useState, useEffect, FC } from 'react';
|
|
|
|
|
|
+import React, { useState, useEffect, FC, useMemo } from 'react';
|
|
import {
|
|
import {
|
|
makeStyles,
|
|
makeStyles,
|
|
Theme,
|
|
Theme,
|
|
@@ -9,39 +9,7 @@ import {
|
|
import CloseIcon from '@material-ui/icons/Close';
|
|
import CloseIcon from '@material-ui/icons/Close';
|
|
import { ConditionProps, Field } from './Types';
|
|
import { ConditionProps, Field } from './Types';
|
|
import CustomSelector from '../customSelector/CustomSelector';
|
|
import CustomSelector from '../customSelector/CustomSelector';
|
|
-
|
|
|
|
-// Todo: Move to corrsponding Constant file.
|
|
|
|
-// Static logical operators.
|
|
|
|
-const LogicalOperators = [
|
|
|
|
- {
|
|
|
|
- value: '<',
|
|
|
|
- label: '<',
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- value: '<=',
|
|
|
|
- label: '<=',
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- value: '>',
|
|
|
|
- label: '>',
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- value: '>=',
|
|
|
|
- label: '>=',
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- value: '==',
|
|
|
|
- label: '==',
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- value: '!=',
|
|
|
|
- label: '!=',
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- value: 'in',
|
|
|
|
- label: 'in',
|
|
|
|
- },
|
|
|
|
-];
|
|
|
|
|
|
+import { LOGICAL_OPERATORS } from '../../consts/Util';
|
|
|
|
|
|
const Condition: FC<ConditionProps> = props => {
|
|
const Condition: FC<ConditionProps> = props => {
|
|
const {
|
|
const {
|
|
@@ -54,9 +22,9 @@ const Condition: FC<ConditionProps> = props => {
|
|
...others
|
|
...others
|
|
} = props;
|
|
} = props;
|
|
const [operator, setOperator] = useState(
|
|
const [operator, setOperator] = useState(
|
|
- initData?.op || LogicalOperators[0].value
|
|
|
|
|
|
+ initData?.op || LOGICAL_OPERATORS[0].value
|
|
);
|
|
);
|
|
- const [conditionField, setConditionField] = useState<Field | any>(
|
|
|
|
|
|
+ const [conditionField, setConditionField] = useState<Field>(
|
|
initData?.field || fields[0] || {}
|
|
initData?.field || fields[0] || {}
|
|
);
|
|
);
|
|
const [conditionValue, setConditionValue] = useState(initData?.value || '');
|
|
const [conditionValue, setConditionValue] = useState(initData?.value || '');
|
|
@@ -90,6 +58,10 @@ const Condition: FC<ConditionProps> = props => {
|
|
? regFloatInterval.test(conditionValueWithNoSpace)
|
|
? regFloatInterval.test(conditionValueWithNoSpace)
|
|
: regFloat.test(conditionValueWithNoSpace);
|
|
: regFloat.test(conditionValueWithNoSpace);
|
|
break;
|
|
break;
|
|
|
|
+ case 'bool':
|
|
|
|
+ const legalValues = ['false', 'true'];
|
|
|
|
+ isLegal = legalValues.includes(conditionValueWithNoSpace);
|
|
|
|
+ break;
|
|
default:
|
|
default:
|
|
isLegal = false;
|
|
isLegal = false;
|
|
break;
|
|
break;
|
|
@@ -108,6 +80,16 @@ const Condition: FC<ConditionProps> = props => {
|
|
|
|
|
|
const classes = useStyles();
|
|
const classes = useStyles();
|
|
|
|
|
|
|
|
+ const logicalOperators = useMemo(() => {
|
|
|
|
+ if (conditionField.type === 'bool') {
|
|
|
|
+ const data = LOGICAL_OPERATORS.filter(v => v.value === '==');
|
|
|
|
+ setOperator(data[0].value);
|
|
|
|
+ // bool only support ==
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+ return LOGICAL_OPERATORS;
|
|
|
|
+ }, [conditionField]);
|
|
|
|
+
|
|
// Logic operator input change.
|
|
// Logic operator input change.
|
|
const handleOpChange = (event: React.ChangeEvent<{ value: unknown }>) => {
|
|
const handleOpChange = (event: React.ChangeEvent<{ value: unknown }>) => {
|
|
setOperator(event.target.value);
|
|
setOperator(event.target.value);
|
|
@@ -140,7 +122,7 @@ const Condition: FC<ConditionProps> = props => {
|
|
label="Logic"
|
|
label="Logic"
|
|
value={operator}
|
|
value={operator}
|
|
onChange={handleOpChange}
|
|
onChange={handleOpChange}
|
|
- options={LogicalOperators}
|
|
|
|
|
|
+ options={logicalOperators}
|
|
variant="filled"
|
|
variant="filled"
|
|
wrapperClass={classes.logic}
|
|
wrapperClass={classes.logic}
|
|
/>
|
|
/>
|