【问题标题】:How to use <Form.List /> (or something similar) in 'formik-antd'?如何在“formik-antd”中使用 <Form.List /> (或类似的东西)?
【发布时间】:2020-07-05 14:43:28
【问题描述】:

我有一个简单的问题 - 是否可以在 formik-antd 中使用

?试图找到有关它的信息,但在 3 小时内找不到任何东西。我需要在表单中添加动态字段,想像在 antd 中一样使用 ,但我使用“formik-antd”。看起来在 'formik-antd' 中没有 Form.List.. :( 只有 Form.Item。

有谁知道'formik-antd'得到了

或类似的东西吗?

【问题讨论】:

    标签: reactjs antd formik


    【解决方案1】:

    formik-antd只是用来使antd的组件与formik兼容,但Form.List之类的东西不会在formik-antd中,而是来自formik。

    你应该使用FieldArray。

    来自文档的示例:

    Working codesandbox.

    import React from 'react';
    import { Formik, Form, Field, FieldArray } from 'formik';
    
    // Here is an example of a form with an editable list.
    // Next to each input are buttons for insert and remove.
    // If the list is empty, there is a button to add an item.
    export const FriendList = () => (
      <div>
        <h1>Friend List</h1>
        <Formik
          initialValues={{ friends: ['jared', 'ian', 'brent'] }}
          onSubmit={values =>
            setTimeout(() => {
              alert(JSON.stringify(values, null, 2));
            }, 500)
          }
          render={({ values }) => (
            <Form>
              <FieldArray
                name="friends"
                render={arrayHelpers => (
                  <div>
                    {values.friends && values.friends.length > 0 ? (
                      values.friends.map((friend, index) => (
                        <div key={index}>
                          <Field name={`friends.${index}`} />
                          <button
                            type="button"
                            onClick={() => arrayHelpers.remove(index)} // remove a friend from the list
                          >
                            -
                          </button>
                          <button
                            type="button"
                            onClick={() => arrayHelpers.insert(index, '')} // insert an empty string at a position
                          >
                            +
                          </button>
                        </div>
                      ))
                    ) : (
                      <button type="button" onClick={() => arrayHelpers.push('')}>
                        {/* show this when user has removed all friends from the list */}
                        Add a friend
                      </button>
                    )}
                    <div>
                      <button type="submit">Submit</button>
                    </div>
                  </div>
                )}
              />
            </Form>
          )}
        />
      </div>
    );
    

    【讨论】:

    • "formik-antd 仅用于使 antd 的组件与 formik 兼容" - 这正是我所需要的。我得到了充满 antd 组件的表单,我应该将它与 formik 和 yup 一起使用。所以我需要用于字段数组(输入)的 formik-antd 组件
    猜你喜欢
    • 2021-11-04
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    相关资源
    最近更新 更多