React 中使用 formik 的字段数组

发布于 2025-01-17 07:46:37 字数 1421 浏览 1 评论 0原文

我想像待办事项列表一样关注。就像下面的图片一样。 现在,我的代码的问题是,它的工作方式不同。 我只想要一个呈现 div 列表而不是输入字段的输入。 如下图所示:

在此处输入图像描述

请在此处检查我的代码和框

点击此处

     <FieldArray
        name="todos"
        render={(arrayHelpers) => (
          <div>
            {formik.values.todos.map((friend, index) => (
              <div key={index} style={{ display: "flex", gap: "1rem" }}>
                <input
                  name={`todos[${index}].name`}
                  value={formik.values.todos[index].name}
                  onChange={formik.handleChange}
                />
                <button
                  disabled={formik.values.todos?.length === 1}
                  type="button"
                  onClick={() => arrayHelpers.remove(index)}
                  className="deleteButton"
                >
                  Remove
                </button>
              </div>
            ))}
            <button
              type="button"
              onClick={() => arrayHelpers.push({ name: "" })}
            >
              Add
            </button>
          </div>
        )}
      />

I wanted to follow like a todo list. Something just like in the picture below.
Right now, the problem with my code is that, its working differently.
I wanted only one input that renders a list of divs instead of input fields.
Exactly just like in the picture below:

enter image description here

Pls check my codesandbox here

CLICK HERE

     <FieldArray
        name="todos"
        render={(arrayHelpers) => (
          <div>
            {formik.values.todos.map((friend, index) => (
              <div key={index} style={{ display: "flex", gap: "1rem" }}>
                <input
                  name={`todos[${index}].name`}
                  value={formik.values.todos[index].name}
                  onChange={formik.handleChange}
                />
                <button
                  disabled={formik.values.todos?.length === 1}
                  type="button"
                  onClick={() => arrayHelpers.remove(index)}
                  className="deleteButton"
                >
                  Remove
                </button>
              </div>
            ))}
            <button
              type="button"
              onClick={() => arrayHelpers.push({ name: "" })}
            >
              Add
            </button>
          </div>
        )}
      />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

献世佛 2025-01-24 07:46:37

这是一个非常简单粗暴的方法 - 我在我的项目中使用了 Formik前一段时间,我很确定它并不完美,但这正是您所需要的。

所以一些细节。

  • 不要为列表中的每一项呈现输入,您只需要使用删除处理程序呈现它的值和元素
  • ,您只需要一个受控输入,它的值用于 todos< 中的 name 字段/代码>。单击添加按钮时状态将被清除。
  • 仅当 formik.values.todos.lenght > 时才需要渲染元素0
  • formik.values.todos.lenght === 1 时,您需要禁用 Delete 按钮
  • 应该禁用 Add 按钮newValue.lenght === 0

小菜一碟,对吧?

Here is a very simple and rude approach - I used Formik my project some time ago and I'm pretty sure it's not perfect but this is exactly what you need.

So some details.

  • do not render input for every item in your list, you need only render it's value and element with delete handler
  • you need only one controlled input and it's value is used for name field in your todos. State is cleaned when you click Add button.
  • you need to render your element only if formik.values.todos.lenght > 0
  • you need to disable Delete button when formik.values.todos.lenght === 1
  • Add button should be disabled when newValue.lenght === 0

Piece a cake, right?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文