Form.jsx 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React, { useState, useEffect } from 'react';
  2. import { Button, Form, Input, DatePicker, notification } from 'antd';
  3. import AuthButton from '@/components/AuthButton'
  4. import request from '@/utils/request';
  5. import apis from '@/services/apis';
  6. import moment from 'moment';
  7. import ModalForm from '../components/ModalForm';
  8. const fullWidth = { width: '100%' };
  9. const AMForm = props => {
  10. const { visibleData, onCancel, form, panoramaList, onSuccess } = props;
  11. const { visible, data } = visibleData;
  12. const { getFieldDecorator, setFieldsValue, resetFields, validateFields } = form;
  13. useEffect(() => {
  14. if (data) {
  15. data.trendTime=moment(data.trendTime)
  16. setFieldsValue(data);
  17. }
  18. }, [data]);
  19. function submitData(e) {
  20. e.preventDefault();
  21. validateFields((err, values) => {
  22. if (err) {
  23. console.error(err);
  24. return;
  25. }
  26. values.buildingId = props.buildingId;
  27. // 网路请求
  28. if (data?.trendId) {
  29. request({ ...apis.building.putTrend, urlData: {id:data?.trendId}, data: { ...values } })
  30. .then(() => {
  31. notification.success({ message: '操作成功' });
  32. onSuccess();
  33. })
  34. .catch(err => {
  35. notification.error({ message: 'err' });
  36. props.onCancel();
  37. });
  38. } else {
  39. request({ ...apis.building.addTrend, data: { ...values } })
  40. .then(() => {
  41. notification.success({ message: '操作成功' });
  42. onSuccess();
  43. })
  44. .catch(err => {
  45. notification.error({ message: 'err' });
  46. props.onCancel();
  47. });
  48. }
  49. });
  50. }
  51. return (
  52. <ModalForm
  53. title="项目动态设置"
  54. visible={visible}
  55. onCancel={onCancel}
  56. modalProps={{ destroyOnClose: true }}
  57. onSubmit={submitData}
  58. >
  59. <Form.Item label="时间">
  60. {getFieldDecorator('trendTime', {
  61. rules: [{ required: true, message: '请选择时间' }],
  62. })(<DatePicker style={fullWidth} />)}
  63. </Form.Item>
  64. <Form.Item label="标题">
  65. {getFieldDecorator('trendTitle', {
  66. rules: [
  67. { required: true, message: '请填写标题' },
  68. { max: 30, message: '标题不超过30个字符' },
  69. ],
  70. })(<Input placeholder="标题不超过30个字符" />)}
  71. </Form.Item>
  72. <Form.Item label="内容">
  73. {getFieldDecorator('trendContent', {
  74. rules: [{ required: true, message: '请填写内容' }],
  75. })(<Input.TextArea row={4} />)}
  76. </Form.Item>
  77. <Form.Item label=" " colon={false} style={{ marginTop: '2em' }}>
  78. <AuthButton name="building.dynamic.edit">
  79. <Button style={{ marginLeft: '4em' }} type="primary" htmlType="submit">
  80. 保存
  81. </Button>
  82. </AuthButton>
  83. <Button style={{ marginLeft: '2em' }} onClick={props.onCancel}>
  84. 取消
  85. </Button>
  86. </Form.Item>
  87. </ModalForm>
  88. );
  89. };
  90. export default Form.create({})(AMForm);