modal.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {Form, Input, InputNumber, message, Modal, Select,} from 'antd';
  2. import {useEffect, useRef} from 'react'
  3. import './modal.scss'
  4. import {useSelector} from "react-redux";
  5. import {getChild} from "@/utils/getDict.js";
  6. import axios from "@/utils/axios.js";
  7. export default function ModalHooks({isModalOpen, closeModal, row, inputVal}) {
  8. const [messageApi, contextHolder] = message.useMessage();
  9. // 获取store的数据
  10. const dictData = useSelector((state) => state.counter.dictData);
  11. // 表单
  12. const [form] = Form.useForm();
  13. // 下拉框数值
  14. let option = useRef([])
  15. //状态下拉框
  16. const status = [
  17. {value: 1, label: '运营'},
  18. {value: 2, label: '非运营'},
  19. ]
  20. /**
  21. *确定时候的回调
  22. */
  23. function handleOk() {
  24. // 验证表单
  25. form.validateFields().then(async () => {
  26. let formData = JSON.parse(JSON.stringify(form.getFieldsValue()))
  27. if (row?.id) {
  28. // 修改数据
  29. formData.id = row.id
  30. }
  31. formData.modelType = inputVal
  32. let {code, message} = await axios.post('/homePageEdit/editMapElement', formData)
  33. if (code === 200) {
  34. messageApi.success(message)
  35. handleCancel()
  36. } else {
  37. messageApi.error(message)
  38. }
  39. })
  40. }
  41. // 关闭页面
  42. const handleCancel = () => {
  43. closeModal(false);
  44. form.resetFields()
  45. };
  46. // 表单
  47. useEffect(() => {
  48. if (row.id) {
  49. let formData = JSON.parse(JSON.stringify(row))
  50. form.setFieldsValue(formData)
  51. }
  52. }, [row])
  53. useEffect(() => {
  54. option.current = getChild(dictData, inputVal)
  55. }, [inputVal]);
  56. return (
  57. <div className="form">
  58. {contextHolder}
  59. <Modal title={row?.id ? '修改数据' : '新增数据'} open={isModalOpen} onOk={handleOk} onCancel={handleCancel}
  60. width={900}
  61. cancelText="取消" okText="确定">
  62. <Form
  63. name="statics"
  64. form={form}
  65. wrapperCol={
  66. {span: 30, offset: 0}
  67. }
  68. layout='inline'
  69. initialValues={{
  70. remember: true,
  71. }}
  72. autoComplete="off"
  73. >
  74. <Form.Item
  75. label="类型"
  76. name="type"
  77. rules={[
  78. {
  79. required: true,
  80. message: '请输入',
  81. },
  82. ]}
  83. >
  84. <Select options={option.current} fieldNames={{label: 'name', value: 'val',}}></Select>
  85. </Form.Item>
  86. <Form.Item
  87. label="元素名称"
  88. name="name"
  89. rules={[
  90. {
  91. required: true,
  92. message: '请输入',
  93. },
  94. ]}
  95. >
  96. <Input></Input>
  97. </Form.Item>
  98. <Form.Item
  99. label="经度"
  100. name="lon"
  101. rules={[
  102. {
  103. required: true,
  104. message: '请输入',
  105. },
  106. ]}
  107. >
  108. <InputNumber min={0} style={{width: '100%'}}></InputNumber>
  109. </Form.Item>
  110. <Form.Item
  111. label="维度"
  112. name="lat"
  113. rules={[
  114. {
  115. required: true,
  116. message: '请输入',
  117. },
  118. ]}
  119. >
  120. <InputNumber min={0} style={{width: '100%'}}></InputNumber>
  121. </Form.Item>
  122. </Form>
  123. </Modal>
  124. </div>
  125. )
  126. }