React+AntDesign结合Table、Modal+Form的使用,以及Ant Design 4.x Modal + Form搭配使用时,如何在Modal中使用表单验证

React+AntDesign结合Table、Modal+Form的使用

最近写React + AntDesign时,使用到Modal和Form组件时候,因为Modal组件自带onOk按钮,因此不能使用Form自带的onFinish函数进行表单验证,查了查资料,大致了解是通过使用 ref 的方式获得表单值甚至可以发起表单校验方法。不多说了,直接上代码。请添加图片描述

export default class App extends Component {

  formRef=React.createRef();
  state={
    categoryList:[],        //商品分类列表
    isModalVisible:false,  //控制弹窗的展示或隐藏
    operType:'',            //操作类型(新增?修改?)
  }

      //弹出商品分类,作为添加
  showAdd=()=>{
        this.setState(()=>{
          return({operType:'add',isModalVisible:true})
        });
      }
      //弹出商品分类,作为修改
  showUpdate=(item)=>{
        this.setState(()=>{
          return({operType:'update',isModalVisible:true})
        });
        this.formRef.current.setFieldsValue({categoryName:item.categoryName})
      }
//弹出框确定的回调
  handleOk=()=>{
    const {operType}=this.state;
    //Modal中表单验证回调
    this.formRef.current.validateFields().then(values=>{ 
          // if(operType==='add'){
          //   this.toAdd(values);
          // }else{

          // }
          this.setState(()=>{
            return({isModalVisible:false})
          });
    }).catch(reason=>{
      message.warning('表单输入不允许为空,请检查');
    })
  }
//弹出框取消的回调
  handleCancel=(e)=>{
    this.formRef.current.resetFields();
    this.setState(()=>{
      return({isModalVisible:false})
    });
}
  render() {
    const dataSource = [
      {
        key: '1',
        categoryName: '手机',
      },
      {
          key: '2',
          categoryName: '电脑',
        },
        {
          key: '3',
          categoryName: '汽车',
        },
        {
          key: '4',
          categoryName: '火车',
        },
        {
          key: '5',
          categoryName: '飞机',
        },
        {
          key: '6',
          categoryName: '地铁',
        },
    ];
    
    const columns = [
      {
        title: '分类名',
        dataIndex: 'categoryName',
        key: 'categoryName',
      },
      {
        title: '操作',
        render:(item)=>{
            return(
                <Button  type="link" onClick={()=>this.showUpdate(item)}>修改分类</Button>
            )
        }
      },
    ];
    const {operType,isModalVisible}=this.state;
    return (
      <div>
          <Card  extra={<Button type='primary'  onClick={this.showAdd}>添加</Button>} >
            <Table 
            dataSource={dataSource} 
            columns={columns}  
            bordered 
            pagination={{pageSize:5,showQuickJumper:true}}
            />
          </Card>
        
          <Modal 
          title={operType === 'add'?'新增分类':'修改分类'} 
          visible={isModalVisible} 
          onOk={this.handleOk} 
          okText="确定"
          onCancel={this.handleCancel}
          cancelText="取消"
          forceRender={true}
          >
              <Form
              name="normal_category"
              ref={this.formRef}
              >
                <Form.Item
                name="categoryName"
                rules={[
                {required: true,message: '分类名称不允许为空'},
                ]}
                >
                    <Input placeholder="请输入分类名称" />
                </Form.Item>
              </Form>
          </Modal>
      </div>
    )
  }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.ToString()°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值