|
@@ -0,0 +1,147 @@
|
|
|
|
+import { useEffect, useMemo, useState } from 'react';
|
|
|
|
+import { Divider, Input, Modal, Select, Table, message, Button } from 'antd';
|
|
|
|
+
|
|
|
|
+const { TextArea } = Input;
|
|
|
|
+
|
|
|
|
+export const FormulaType = {
|
|
|
|
+ Filed: 'filed',
|
|
|
|
+ Symbol: 'symbol',
|
|
|
|
+ Number: 'number',
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const FormulaModal = (props) => {
|
|
|
|
+ const { item, numFiledList = [], visible, onCancel, onChange } = props;
|
|
|
|
+ const symbolList = ['+', '-', '*', '/', '(', ')'];
|
|
|
|
+ const numberList = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '.'];
|
|
|
|
+
|
|
|
|
+ const [formula, setFormula] = useState([]);
|
|
|
|
+
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ if (!visible) setFormula([]);
|
|
|
|
+ if (item?.props) setFormula(item.props.formula || []);
|
|
|
|
+ }, [visible, item]);
|
|
|
|
+
|
|
|
|
+ const value = useMemo(() => {
|
|
|
|
+ const strList = formula.map((item) =>
|
|
|
|
+ item.type == FormulaType.Filed ? `【${item.label}】` : item.label,
|
|
|
|
+ );
|
|
|
|
+ return '计算公式=' + strList.join('');
|
|
|
|
+ }, [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 (
|
|
|
|
+ <Modal
|
|
|
|
+ title="编辑计算公式"
|
|
|
|
+ open={visible}
|
|
|
|
+ onCancel={onCancel}
|
|
|
|
+ onOk={() => {
|
|
|
|
+ onChange?.({ ...item.props, formula, formulaLabel: value });
|
|
|
|
+ }}
|
|
|
|
+ width={1000}
|
|
|
|
+ pagination={false}
|
|
|
|
+ >
|
|
|
|
+ <Divider />
|
|
|
|
+
|
|
|
|
+ <Button
|
|
|
|
+ type="primary"
|
|
|
|
+ style={{ marginBottom: '20px' }}
|
|
|
|
+ onClick={() => setFormula([])}
|
|
|
|
+ >
|
|
|
|
+ 清空
|
|
|
|
+ </Button>
|
|
|
|
+ <TextArea value={value} />
|
|
|
|
+ <div style={{ margin: '20px 20px 20px 0' }}>
|
|
|
|
+ 计算对象:
|
|
|
|
+ {numFiledList.map((item, idx) => (
|
|
|
|
+ <span
|
|
|
|
+ style={{
|
|
|
|
+ padding: '6px 10px',
|
|
|
|
+ border: '1px solid #e5e5e5',
|
|
|
|
+ borderRadius: '2px',
|
|
|
|
+ marginRight: '10px',
|
|
|
|
+ }}
|
|
|
|
+ onClick={() => handlerChange(FormulaType.Filed, item)}
|
|
|
|
+ >
|
|
|
|
+ {item.props.label}
|
|
|
|
+ </span>
|
|
|
|
+ ))}
|
|
|
|
+ </div>
|
|
|
|
+ <div style={{ margin: '0 20px 20px 0' }}>
|
|
|
|
+ 计算符号:
|
|
|
|
+ {symbolList.map((item, idx) => (
|
|
|
|
+ <span
|
|
|
|
+ style={{
|
|
|
|
+ padding: '6px 10px',
|
|
|
|
+ border: '1px solid #e5e5e5',
|
|
|
|
+ borderRadius: '2px',
|
|
|
|
+ marginRight: '10px',
|
|
|
|
+ }}
|
|
|
|
+ onClick={() => handlerChange(FormulaType.Symbol, item)}
|
|
|
|
+ >
|
|
|
|
+ {item}
|
|
|
|
+ </span>
|
|
|
|
+ ))}
|
|
|
|
+ </div>
|
|
|
|
+ <div style={{ margin: '0 20px 20px 0' }}>
|
|
|
|
+ 数字键盘:
|
|
|
|
+ {numberList.map((item, idx) => (
|
|
|
|
+ <span
|
|
|
|
+ style={{
|
|
|
|
+ padding: '6px 10px',
|
|
|
|
+ border: '1px solid #e5e5e5',
|
|
|
|
+ borderRadius: '2px',
|
|
|
|
+ marginRight: '10px',
|
|
|
|
+ }}
|
|
|
|
+ onClick={() => handlerChange(FormulaType.Number, item)}
|
|
|
|
+ >
|
|
|
|
+ {item}
|
|
|
|
+ </span>
|
|
|
|
+ ))}
|
|
|
|
+ </div>
|
|
|
|
+ </Modal>
|
|
|
|
+ );
|
|
|
|
+};
|
|
|
|
+export default FormulaModal;
|