当前位置: 首页 > news >正文

antd Form表单实践模板(Form.useForm)

定制一个Form表单模板,主要支持Form.List、Form.Item之间联动,快速进入开发

表单结构如下:

  1. 修改type时重置assetTypeName以及details
  2. frequencyParam的options监听frequency的值的变化,如果frequency的值变化了,重置frequencyParam的值,同时 frequencyParam的options跟着变动
  3. details的每一项支持新增/删除
  4. items的每一项支持新增/删除
  • type
  • assetTypeName
  • details
    • name
    • frequency
    • frequencyParam
    • items
      • title
      • content
      • attachments1
      • attachments2
import React, { useEffect } from 'react'
import { message, Form, Select, Button, Input, Card, Row, Col, Modal } from 'antd'
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons'
import { Upload } from '@/components'const Index = ({ data }: { data: any }) => {const [form] = Form.useForm()const onFinishFailed = (errorInfo: any) => {console.log('Failed:', errorInfo)}useEffect(() => {pageInit()}, [])const pageInit = async () => {// ...}const onFinish = (values: any) => {const params = {// ...}const promise = data.code ? dispatch.xxx.编辑接口 : dispatch.xxx.创建接口promise(params).then((res) => {if (res.code === 200) {message.success('操作成功')}})}return (<Formform={form}name="basic"labelCol={{ span: 4 }}wrapperCol={{ span: 20 }}onFinish={onFinish}onFinishFailed={onFinishFailed}autoComplete="off"><Form.Itemlabel={'xxx'}name={'type'}rules={[{ required: true, message: '请选择xxx' }]}wrapperCol={{ span: 12 }}><Selectplaceholder="请选择"optionFilterProp="label"showSearchoptions={[]}onChange={(e) => {form.setFieldsValue({assetTypeName: undefined,details: [],})}}/></Form.Item><Form.Itemlabel={'名称'}name={'assetTypeName'}rules={[{ required: true, message: '请输入名称' }]}wrapperCol={{ span: 12 }}><Input  placeholder="请输入名称" /></Form.Item>{<><Card title="详情"><Form.List name="details">{(details) => (<>{details?.map?.((field) => (<React.Fragment key={field.key}><Row gutter={24}><Col span={8}><Form.Item{...field}labelCol={{ span: 9 }}wrapperCol={{ span: 14 }}label="名称"name={[field.name, 'name']}rules={[{ required: true, message: '请输入名称' }]}><Input  placeholder="请输入名称" /></Form.Item></Col><Col span={8}><Form.Item{...field}labelCol={{ span: 9 }}wrapperCol={{ span: 14 }}label="频次"name={[field.name, 'frequency']}rules={[{ required: true, message: '请选择频次' }]}><SelectshowSearchoptionFilterProp="label"style={{ width: '100%' }}placeholder="请选择频次"options={[]}onChange={(e) => {form.setFieldValue(['details', field.name, 'frequencyParam'],undefined,)}}/></Form.Item></Col><Col span={8}><Form.Item noStyle shouldUpdate>{({ getFieldValue }) => {const frequency = getFieldValue(['details', field.name, 'frequency'])if (frequency && frequency !== 1) {return (<Form.Item{...field}labelCol={{ span: 9 }}wrapperCol={{ span: 14 }}label="维保频次"name={[field.name, 'frequencyParam']}rules={[{ required: true, message: '请选择维保频次' }]}><Selectmode="multiple"showSearchoptionFilterProp="label"style={{ width: '100%' }}placeholder="请选择维保频次"options={frequency === 2 ? WeekOptions : DaysOptions}/></Form.Item>)}return null}}</Form.Item></Col></Row><Form.List name={[field.name, 'items']}>{(items, { add, remove }) => (<>{items.map((field2, itemsIndex) => (<React.Fragment key={itemsIndex}><Row gutter={24}><Col span={12}><Form.ItemlabelCol={{ span: 7 }}wrapperCol={{ span: 16 }}label="标题"name={[field2.name, 'title']}rules={[{ required: true, message: '请输入标题' }]}><Input placeholder="请输入标题" maxLength={200} /></Form.Item></Col><Col span={6}><MinusCircleOutlinedonClick={() => {Modal.confirm({title: '是否需要删除该内容?',okText: '确定',okType: 'danger',cancelText: '取消',onOk() {remove(field2.name)},})}}/></Col></Row><Form.ItemlabelCol={{ span: 3.5 }}wrapperCol={{ span: 12 }}label="内容"name={[field2.name, 'content']}rules={[{ required: true, message: '请输入内容' }]}><Input.TextArea placeholder="请输入内容" maxLength={2000} /></Form.Item><Form.ItemlabelCol={{ span: 4 }}wrapperCol={{ span: 12 }}label="上传示意图"name={[field2.name, 'attachments1']}rules={[{ required: true, message: '请选择上传示意图' }]}><UploadbeforeUpload={() => false}text={'点击上传'}multiple={true}accept=".jpg,.jpeg,.png"maxCount={25}/></Form.Item><Form.ItemlabelCol={{ span: 4 }}wrapperCol={{ span: 12 }}label="上传维保视频"name={[field2.name, 'attachments2']}// rules={[{ required: true, message: '请上传维保视频' }]}><UploadbeforeUpload={() => false}text={'点击上传'}multiple={true}accept="video/mp4"maxCount={3}/></Form.Item></React.Fragment>))}<Form.Item><Buttontype="dashed"onClick={() => add()}blockicon={<PlusOutlined />}>添加内容</Button></Form.Item></>)}</Form.List></React.Fragment>))}</>)}</Form.List></Card></>}<Form.Item wrapperCol={{ offset: 8, span: 16 }}><Button type="primary" htmlType="submit" style={{ marginRight: 8 }}>确定</Button><Button type="default">取消</Button></Form.Item></Form>)
}
export default Index
http://www.dtcms.com/a/585778.html

相关文章:

  • 数据结构与算法概述
  • n8n + Pinecone + ModelScope:知识库向量查询工作流实战
  • C++ 面向对象三大特性之一---多态
  • 合肥企业网站建设网站图片怎样做seo优化
  • 短剧小程序 2025 核心痛点分析:内容、技术与合规的三重困境
  • 河南省住房和城乡建设厅网站查证网站前台右侧怎么做二维码
  • 从原理到实操:ddraw.dll是什么?为何游戏启动时频繁提示“找不到ddraw.dll”?解决思路全解析
  • 计算机网络自顶向下方法39——网络层 中间盒 互联网架构原则(IP沙漏 端到端原则)
  • 广州有做虚拟货币网站视频创作用什么软件
  • wap网站和app开发正邦集团招聘
  • RV1126 NO.43:OPENCV形态学基础之二:腐蚀
  • 算法学习 24 使用集合解决问题
  • Java基础——集合进阶3
  • Ascend C 编程模型揭秘:深入理解核函数与任务并行机制
  • 算法题——002
  • 佛山微信网站开发易语言网站开发教程
  • 从零搭建PyTorch计算机视觉模型
  • 培训课程网站建设淮阳 网站建设
  • 服务器为何成为网络攻击的“重灾区“?
  • Linux rcu机制
  • ES 总结
  • j集团公司的网站建设石家庄百度推广优化排名
  • k8s-node-NotReady后如何保证服务可用
  • 5-GGML:看一下CUDA加法算子!
  • 做网站优化需要做哪些事项wordpress圆圈特效
  • 濮阳网站建设费用网站怎样做外链
  • Docker 部署 Java 项目实践
  • Git push/pull 避坑指南:什么时候加 origin?什么时候不用加?
  • Ubuntu22.04系统中各文件目录的作用
  • 49_AI智能体核心业务之使用Flask蓝图模块化AI智能体服务:构建可维护的RESTful API