ControlParamsForm.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import React, { useState } from 'react';
  2. import { Form, Input, Slider, Row, Col, Button, Modal, message } from 'antd';
  3. import Panel from '@/pages/Smart/components/Panel';
  4. const ControlParamsForm = ({ form, process, edit, onSave, loading, current, changeProcess }) => {
  5. const [name, setName] = useState('');
  6. const [visible, setVisible] = useState(false);
  7. const control = current?.control || {};
  8. const { getFieldDecorator, setFieldsValue } = form;
  9. const params = process.control;
  10. const renderFormItems = () => {
  11. return params.map((param, index) => {
  12. let key = param.name;
  13. return (
  14. <Row span={12} key={index}>
  15. <Form.Item label={key}>
  16. {getFieldDecorator(key, {
  17. initialValue: control[key],
  18. })(
  19. <Input
  20. disabled={!edit}
  21. onChange={e => handleChangeProcess(key, e.target.value)}
  22. addonAfter={param.unit}
  23. />
  24. )}
  25. {edit &&
  26. getFieldDecorator(key, {
  27. initialValue: control[key],
  28. })(
  29. <Slider
  30. min={0}
  31. max={param.max}
  32. step={param.max < 1 ? 0.01 : 1}
  33. onChange={value => handleChangeProcess(key, value)}
  34. />
  35. )}
  36. </Form.Item>
  37. </Row>
  38. );
  39. });
  40. };
  41. const handleChangeProcess = (key, value) => {
  42. let data = form.getFieldsValue();
  43. let params = process.control.find(item => item.name == key);
  44. params.value = value;
  45. changeProcess({ ...process });
  46. };
  47. const onShowModal = () => {
  48. if (!current?.id) {
  49. message.error('请先保存基础参数');
  50. return;
  51. }
  52. setVisible(true);
  53. };
  54. return (
  55. <Panel
  56. title="控制参数"
  57. btns={
  58. edit && (
  59. <Button loading={loading} onClick={onShowModal} type="primary">
  60. 新增
  61. </Button>
  62. )
  63. }
  64. >
  65. <Form labelCol={{ span: 8 }} wrapperCol={{ span: 16 }}>
  66. <div>{renderFormItems()}</div>
  67. </Form>
  68. <Modal
  69. destroyOnClose
  70. confirmLoading={loading}
  71. visible={visible}
  72. title={`创建目标工况`}
  73. onCancel={() => {
  74. setVisible(false);
  75. }}
  76. onOk={() => {
  77. onSave(name);
  78. setVisible(false);
  79. setName('');
  80. }}
  81. >
  82. <Form labelCol={{ span: 5 }} wrapperCol={{ span: 15 }}>
  83. <Form.Item label="名称">
  84. <Input
  85. placeholder="请输入目标工况名称"
  86. value={name}
  87. onChange={e => setName(e.target.value)}
  88. />
  89. </Form.Item>
  90. </Form>
  91. </Modal>
  92. </Panel>
  93. );
  94. };
  95. export default Form.create()(ControlParamsForm);