当前位置:   article > 正文

在react antd中动态生成多个 form表单组,包括一个动态添加/删除表单项的功能和一个提交表单的功能_recat antd 动态展示from数据

recat antd 动态展示from数据

在这个示例中,我们首先使用 Form.useForm() 创建一个表单实例。接着,我们使用 Form.List 组件来动态生成多个表单项。在 Form.List 组件中,我们使用 fields.map 方法循环渲染每个表单项,并使用 Form.Item 组件包裹每个表单项。在 Form.Item 组件中,我们使用 label 属性指定标签,使用 name 属性指定表单项的名称,使用 rules 属性指定验证规则。在输入框中,我们使用 placeholder 属性来指定占位文本。 在每个表单项的最后,我们使用 Button 组件来实现删除表单项的功能。在表单的最后,我们使用 Button 组件来实现提交表单的功能。当用户点击 Add field 按钮时,将会调用 add 方法,动态添加一个新的表单项。当用户提交表单时,将会调用 onFinish 方法,打印所有表单项的内容。 最后,我们在 index.js 文件中渲染 DynamicForm 组件:

示例:

接收传入的值为

const [formData, setFormData] = useState([ { cpu: 1, gpu: 3, memory: 1, frequency: 'week', day: 1, startTimeFormat: '00:00:00', endTimeFormat: '02:00:00', }, { cpu: 2, gpu: 4, memory: 1, frequency: 'day', day: 1, startTimeFormat: '00:00:00', endTimeFormat: '02:00:00', }, ]);

函数组件:

  1. import React, { useState , useEffect,forwardRef, useImperativeHandle} from 'react';
  2. import {
  3. Form, Input,
  4. Button,
  5. Table,
  6. message,
  7. InputNumber,
  8. Spin, Radio,
  9. Modal,
  10. Card,
  11. Row,
  12. Col, TimePicker,
  13. Select,
  14. } from 'antd';
  15. import moment from "moment";
  16. import { PlusOutlined, MinusCircleOutlined } from '@ant-design/icons';
  17. const { Option } = Select;
  18. import styles from "./index.module.less";
  19. const TimescrollTemplate = [
  20. { value: 'month', name: '月' },
  21. { value: 'week', name: '周' },
  22. { value: 'day', name: '天' },
  23. ]
  24. function FormItem({ formItem, delShow, index, onChange, onDelete }) {
  25. const formatTime = (time) => {
  26. if (!time) {
  27. return '00:00:00';
  28. }
  29. const hours = time.hours().toString().padStart(2, '0');
  30. const minutes = time.minutes().toString().padStart(2, '0');
  31. const seconds = time.seconds().toString().padStart(2, '0');
  32. return `${hours}:${minutes}:${seconds}`;
  33. };
  34. const handleInputChange = (index, name, value) => {
  35. console.log(index, name, value, "index, name, value")
  36. if (name, value) {
  37. if (name == "startTimeFormat") {
  38. // const formattedTime = formatTime(value);
  39. const [startTime, endTime] = value
  40. console.log(startTime, endTime, "startTime, endTime")
  41. onChange(index, "startTimeFormat", formatTime(startTime));
  42. onChange(index, "endTimeFormat", formatTime(endTime));
  43. } else {
  44. onChange(index, name, value);
  45. }
  46. }
  47. };
  48. return (
  49. <>
  50. <Row gutter={[0, 24]} className={styles.listBody}>
  51. <Col className="gutter-row" span={10}>
  52. <Row gutter={[0, 24]} >
  53. <Col className="gutter-row" style={{
  54. textAlign: "left",
  55. lineHeight: '32px',
  56. }} span={8}>
  57. <div className={styles.rowTie}>CPU(核)</div>
  58. <InputNumber min={0} style={{ maxwidth: 160, marginLeft: 24, borderRadius: 2 }} value={formItem.cpu}
  59. name="cpu"
  60. onChange={(newValue) => handleInputChange(index, 'cpu', newValue)}
  61. />
  62. </Col>
  63. <Col className="gutter-row" style={{
  64. lineHeight: '32px',
  65. textAlign: "left"
  66. }} span={8}>
  67. <div className={styles.rowTie}>GPU(个)</div>
  68. <InputNumber
  69. name="gpu"
  70. value={formItem.gpu}
  71. onChange={(newValue) => handleInputChange(index, 'gpu', newValue)}
  72. min={0} style={{ maxwidth: 160, marginLeft: 24, borderRadius: 2 }} />
  73. </Col>
  74. <Col className="gutter-row" style={{
  75. lineHeight: '32px',
  76. textAlign: "left"
  77. }} span={8}>
  78. <div className={styles.rowTie}>内存(G)</div>
  79. <InputNumber
  80. name="memory"
  81. value={formItem.memory}
  82. onChange={(newValue) => handleInputChange(index, 'memory', newValue)}
  83. min={0} style={{ maxwidth: 160, marginLeft: 24, borderRadius: 2 }} />
  84. </Col>
  85. </Row>
  86. </Col>
  87. <Col className="gutter-row" span={14}>
  88. <div style={{
  89. textAlign: "left",
  90. fontWeight: 500,
  91. marginLeft: 24,
  92. lineHeight: "32px"
  93. }}>时间段</div>
  94. <div style={{
  95. marginLeft: 24, display: "flex", lineHeight: "32px", color: "#999"
  96. }}>
  97. 在每
  98. <Select
  99. placeholder="天"
  100. name="frequency"
  101. value={formItem.frequency}
  102. onChange={(newValue) => handleInputChange(index, 'frequency', newValue)}
  103. style={{ borderRadius: 2, marginLeft: 5 }}
  104. >
  105. {(
  106. TimescrollTemplate || []).map((item) => (
  107. <Select.Option key={item.value} value={item.value}>
  108. {item.name}
  109. </Select.Option>
  110. ))}
  111. </Select>
  112. <Select
  113. type="number"
  114. name="day"
  115. value={formItem.day}
  116. onChange={(newValue) => handleInputChange(index, 'day', newValue)}
  117. mode="tags"
  118. placeholder="请选择执行日"
  119. style={{ overflowY: 'auto', width: 200,maxHeight:32, marginLeft: 10 }}
  120. >
  121. {Array.from({ length: 31 }, (_, index) => index + 1).map((item,index) => (
  122. <Select.Option key={item.index} value={item}>
  123. {item}
  124. </Select.Option>
  125. ))}
  126. </Select>
  127. <TimePicker.RangePicker
  128. name="startTimeFormat"
  129. onChange={(newValue) => handleInputChange(index, 'startTimeFormat', newValue)}
  130. size="small"
  131. value={
  132. // moment(formItem.startTimeFormat, 'HH:mm:ss')
  133. [moment(formItem.startTimeFormat, 'HH:mm:ss'), moment(formItem.endTimeFormat, 'HH:mm:ss')]
  134. }
  135. style={{ marginLeft: 10, width: 200 }} />
  136. {/* <TimePicker
  137. name="endTimeFormat"
  138. size="small"
  139. value={moment(formItem.endTimeFormat, 'HH:mm:ss')}
  140. onChange={(newValue) => handleInputChange(index, 'endTimeFormat', newValue)}
  141. style={{ marginLeft: 10, width: 200 }} /> */}
  142. {delShow &&
  143. <div
  144. style={{
  145. marginLeft: 5, position: 'absolute',
  146. right: '24px',
  147. top: '27px'
  148. }}
  149. >
  150. <a onClick={() => onDelete(index)} >删除</a>
  151. </div>
  152. }
  153. </div>
  154. </Col>
  155. </Row>
  156. </>
  157. );
  158. }
  159. const DynamicForm = forwardRef(({ onSubmit, value = [] }, ref) => {
  160. console.log(value,"value")
  161. const [formData, setFormData] = useState(value);
  162. console.log(formData,"formData")
  163. useEffect(() => {
  164. setFormData(value);
  165. }, [value]);
  166. const handleFormSubmit = (event) => {
  167. event.preventDefault();
  168. console.log(formData); // 打印表单数据
  169. onSubmit(formData);
  170. };
  171. useImperativeHandle(ref, () => ({ handleFormSubmit }));
  172. const handleFormItemChange = (index, name, value) => {
  173. console.log(index, name, value, "index, name, value")
  174. setFormData((prevFormData) => {
  175. const newFormData = [...prevFormData];
  176. newFormData[index][name] = value;
  177. return newFormData;
  178. });
  179. };
  180. const handleFormItemDelete = (index) => {
  181. setFormData((prevFormData) => {
  182. const newFormData = [...prevFormData];
  183. newFormData.splice(index, 1);
  184. return newFormData;
  185. });
  186. };
  187. const handleFormAdd = () => {
  188. setFormData((prevFormData) => [
  189. ...prevFormData,
  190. {
  191. cpu: 1,
  192. gpu: 1,
  193. memory: 1,
  194. frequency: 'month',
  195. day: 1,
  196. startTimeFormat: '00:00:00',
  197. endTimeFormat: '00:00:00',
  198. },
  199. ]);
  200. };
  201. return (
  202. <form onSubmit={handleFormSubmit}>
  203. {formData.map((formItem, index) => (
  204. <FormItem
  205. key={index}
  206. formItem={formItem}
  207. index={index}
  208. delShow={formData.length > 1}
  209. onChange={handleFormItemChange}
  210. onDelete={handleFormItemDelete}
  211. />
  212. ))}
  213. <div >
  214. <Button style={{ textAlign: 'center', width: 400 }} type="primary" onClick={handleFormAdd} icon={<PlusOutlined />}>
  215. 添加资源评估
  216. </Button>
  217. </div>
  218. {/* <button type="submit">提交</button> */}
  219. </form>
  220. );
  221. });
  222. export default DynamicForm;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/91568
推荐阅读
相关标签
  

闽ICP备14008679号