|
@@ -1,20 +1,76 @@
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
-import { Divider, Input, Modal, Select, Table } from 'antd';
|
|
|
|
|
|
+import { Divider, Input, Modal, Select, Table, message, Button } from 'antd';
|
|
|
|
|
|
const { TextArea } = Input;
|
|
const { TextArea } = Input;
|
|
|
|
|
|
|
|
+export const FormulaType = {
|
|
|
|
+ Filed: 'filed',
|
|
|
|
+ Symbol: 'symbol',
|
|
|
|
+ Number: 'number',
|
|
|
|
+};
|
|
|
|
+
|
|
const FormulaModal = (props) => {
|
|
const FormulaModal = (props) => {
|
|
const { item, numFiledList = [], visible, onCancel, onChange } = props;
|
|
const { item, numFiledList = [], visible, onCancel, onChange } = props;
|
|
const symbolList = ['+', '-', '*', '/', '(', ')'];
|
|
const symbolList = ['+', '-', '*', '/', '(', ')'];
|
|
const numberList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'];
|
|
const numberList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'];
|
|
|
|
|
|
- const [formula, setFormula] = useState();
|
|
|
|
|
|
+ const [formula, setFormula] = useState([]);
|
|
|
|
|
|
- const [data, setData] = useState([]);
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ if (!visible) setFormula([]);
|
|
|
|
+ if (item?.props) setFormula(item.props.formula || []);
|
|
|
|
+ }, [visible, item]);
|
|
|
|
|
|
const value = useMemo(() => {
|
|
const value = useMemo(() => {
|
|
- return '计算公式=';
|
|
|
|
|
|
+ const strList = formula.map((item) =>
|
|
|
|
+ item.type == FormulaType.Filed ? `【${item.label}】` : item.label,
|
|
|
|
+ );
|
|
|
|
+ return '计算公式=' + strList.join('');
|
|
}, [formula]);
|
|
}, [formula]);
|
|
|
|
+ console.log('-----------------', formula);
|
|
|
|
+
|
|
|
|
+ const isSameBeforItem = (type) => {
|
|
|
|
+ return formula.length > 0 && formula[formula.length - 1].type == type;
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ const handlerChange = (type, item) => {
|
|
|
|
+ let obj = {};
|
|
|
|
+ console.log(formula[formula.length - 1]);
|
|
|
|
+ switch (type) {
|
|
|
|
+ case FormulaType.Filed:
|
|
|
|
+ if (isSameBeforItem(FormulaType.Filed)) {
|
|
|
|
+ message.error('不能选择连续两个组件');
|
|
|
|
+ }
|
|
|
|
+ obj = {
|
|
|
|
+ type: FormulaType.Filed,
|
|
|
|
+ id: item.props.id,
|
|
|
|
+ label: item.props.label,
|
|
|
|
+ };
|
|
|
|
+ setFormula([...formula, obj]);
|
|
|
|
+ break;
|
|
|
|
+ case FormulaType.Symbol:
|
|
|
|
+ if (isSameBeforItem(FormulaType.Symbol)) {
|
|
|
|
+ message.error('不能选择连续两个符号');
|
|
|
|
+ }
|
|
|
|
+ obj = { type: FormulaType.Symbol, label: item };
|
|
|
|
+ setFormula([...formula, obj]);
|
|
|
|
+ break;
|
|
|
|
+ case FormulaType.Number:
|
|
|
|
+ //如果前一个选择的也是数字则合并数字 否则直接添加
|
|
|
|
+ if (isSameBeforItem(FormulaType.Number)) {
|
|
|
|
+ const len = formula.length - 1;
|
|
|
|
+ const label = formula[len].label + item;
|
|
|
|
+ obj = { type: FormulaType.Number, label };
|
|
|
|
+ const result = [...formula];
|
|
|
|
+ result[len] = obj;
|
|
|
|
+ setFormula(result);
|
|
|
|
+ } else {
|
|
|
|
+ obj = { type: FormulaType.Number, label: item };
|
|
|
|
+ setFormula([...formula, obj]);
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
|
|
return (
|
|
return (
|
|
<Modal
|
|
<Modal
|
|
@@ -22,20 +78,24 @@ const FormulaModal = (props) => {
|
|
open={visible}
|
|
open={visible}
|
|
onCancel={onCancel}
|
|
onCancel={onCancel}
|
|
onOk={() => {
|
|
onOk={() => {
|
|
- const linked = {};
|
|
|
|
- data?.forEach((cur) => {
|
|
|
|
- linked[cur.name] = cur.linked;
|
|
|
|
- });
|
|
|
|
- onChange?.({ linked });
|
|
|
|
|
|
+ onChange?.({ ...item.props, formula, formulaLabel: value });
|
|
}}
|
|
}}
|
|
width={1000}
|
|
width={1000}
|
|
pagination={false}
|
|
pagination={false}
|
|
>
|
|
>
|
|
<Divider />
|
|
<Divider />
|
|
|
|
+
|
|
|
|
+ <Button
|
|
|
|
+ type="primary"
|
|
|
|
+ style={{ marginBottom: '20px' }}
|
|
|
|
+ onClick={() => setFormula([])}
|
|
|
|
+ >
|
|
|
|
+ 清空
|
|
|
|
+ </Button>
|
|
<TextArea value={value} />
|
|
<TextArea value={value} />
|
|
<div style={{ margin: '20px 20px 20px 0' }}>
|
|
<div style={{ margin: '20px 20px 20px 0' }}>
|
|
计算对象:
|
|
计算对象:
|
|
- {numFiledList.map((item) => (
|
|
|
|
|
|
+ {numFiledList.map((item, idx) => (
|
|
<span
|
|
<span
|
|
style={{
|
|
style={{
|
|
padding: '6px 10px',
|
|
padding: '6px 10px',
|
|
@@ -43,14 +103,15 @@ const FormulaModal = (props) => {
|
|
borderRadius: '2px',
|
|
borderRadius: '2px',
|
|
marginRight: '10px',
|
|
marginRight: '10px',
|
|
}}
|
|
}}
|
|
|
|
+ onClick={() => handlerChange(FormulaType.Filed, item)}
|
|
>
|
|
>
|
|
{item.props.label}
|
|
{item.props.label}
|
|
</span>
|
|
</span>
|
|
))}
|
|
))}
|
|
</div>
|
|
</div>
|
|
<div style={{ margin: '0 20px 20px 0' }}>
|
|
<div style={{ margin: '0 20px 20px 0' }}>
|
|
- 计算对象:
|
|
|
|
- {symbolList.map((item) => (
|
|
|
|
|
|
+ 计算符号:
|
|
|
|
+ {symbolList.map((item, idx) => (
|
|
<span
|
|
<span
|
|
style={{
|
|
style={{
|
|
padding: '6px 10px',
|
|
padding: '6px 10px',
|
|
@@ -58,6 +119,7 @@ const FormulaModal = (props) => {
|
|
borderRadius: '2px',
|
|
borderRadius: '2px',
|
|
marginRight: '10px',
|
|
marginRight: '10px',
|
|
}}
|
|
}}
|
|
|
|
+ onClick={() => handlerChange(FormulaType.Symbol, item)}
|
|
>
|
|
>
|
|
{item}
|
|
{item}
|
|
</span>
|
|
</span>
|
|
@@ -65,7 +127,7 @@ const FormulaModal = (props) => {
|
|
</div>
|
|
</div>
|
|
<div style={{ margin: '0 20px 20px 0' }}>
|
|
<div style={{ margin: '0 20px 20px 0' }}>
|
|
数字键盘:
|
|
数字键盘:
|
|
- {numberList.map((item) => (
|
|
|
|
|
|
+ {numberList.map((item, idx) => (
|
|
<span
|
|
<span
|
|
style={{
|
|
style={{
|
|
padding: '6px 10px',
|
|
padding: '6px 10px',
|
|
@@ -73,6 +135,7 @@ const FormulaModal = (props) => {
|
|
borderRadius: '2px',
|
|
borderRadius: '2px',
|
|
marginRight: '10px',
|
|
marginRight: '10px',
|
|
}}
|
|
}}
|
|
|
|
+ onClick={() => handlerChange(FormulaType.Number, item)}
|
|
>
|
|
>
|
|
{item}
|
|
{item}
|
|
</span>
|
|
</span>
|