• React-hook-form-mui(五):包含内嵌表单元素的表单


    前言

    在上一篇文章中,我们介绍了react-hook-form-mui如何与与后端数据联调。在实际项目中,从后端获取的数据可能是复杂的数据对象,本文将介绍,如何通过react-hook-form-mui实现一个包含内嵌表单元素的表单

    Demo

    以下代码实现了一个包含内嵌表单元素的表单的完整代码:

    import React from 'react';
    import { useForm } from 'react-hook-form';
    import { Button, MenuItem } from '@mui/material';
    import { FormContainer, TextFieldElement } from 'react-hook-form-mui';
    
    //内嵌表单元素
    const InnerForm = ({ index }: any) => {
      return (
        <>
          <TextFieldElement name={`items[${index}].name`} label="Name" />
          <TextFieldElement
            name={`items[${index}].quantity`}
            label="Quantity"
            type="number"
          />
        </>
      );
    };
    
    const MyForm = () => {
      const formContext = useForm({
        defaultValues: {
          firstName: '',
          lastName: '',
          email: '',
          gender: '',
          age: '',
          items: [{ name: '', quantity: '' }]
        }
      });
      const { watch } = formContext;
    
      const onSubmit = (data) => {
        console.log(data);
      };
    
      return (
        <FormContainer
          formContext={formContext}
          onSuccess={(data) => {
            onSubmit(data);
          }}
        >
          <TextFieldElement name="firstName" label="First Name" />
          <TextFieldElement name="lastName" label="Last Name" />
          <TextFieldElement name="email" label="Email" />
          <TextFieldElement select name="gender" label="Gender">
            <MenuItem value="male">Male</MenuItem>
            <MenuItem value="female">Female</MenuItem>
          </TextFieldElement>
          <TextFieldElement name="age" label="Age" type="number" />
          {watch('items')?.map((_, index) =>
            <InnerForm key={index} index={index} />
          )}
          //像数组中插入表新的元素
          <Button
            type="button"
            onClick={() => watch('items').push({ name: '', quantity: '' })}
          >
            Add Item
          </Button>
          <Button type="submit">Submit</Button>
        </FormContainer>
      );
    };
    
    export default MyForm;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    解析

    //内嵌表单元素
    const InnerForm = ({ index }: any) => {
      return (
        <>
          <TextFieldElement name={`items[${index}].name`} label="Name" />
          <TextFieldElement
            name={`items[${index}].quantity`}
            label="Quantity"
            type="number"
          />
        </>
      );
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    以上代码是实现内嵌表单元素的关键代码,了解以上代码,我们需要了解react-hook-form-mui的核心理念。它是通过获取表单元素的name,生成数据结构数。因此,对于内嵌的组件而言,我们需要通过index来给name赋值。这样就可以获取到内嵌表单元素的表单值。

    总结

    以上是关于React-hook-form-mui的内嵌表单元素的讲解。希望本文会对你有所帮助。如果有什么问题,可在下方留言沟通。

  • 相关阅读:
    仿网吧游戏菜单-超好用
    apifox 针对测试使用教程(持续更新)
    Spring Cloud微服务治理框架深度解析
    可扩展标记语言-----XML
    Kamiya丨Kamiya艾美捷人CP ELISA说明书
    【LeetCode】Day125-最接近的三数之和 & 电话号码的字母组合
    介绍各个PHP版本一些特性知识点
    Java进阶学习路线图
    svg之全局组件,配合雪碧图解决vue2的svg优化问题
    在线图片转文字怎么转?这种方法大家可以学会
  • 原文地址:https://blog.csdn.net/m0_73117087/article/details/132443957