用n个标签创建表单为代表值的字段

发布于 2025-02-06 08:14:15 字数 1433 浏览 1 评论 0原文

我想创建一个 tag 代表由API给出的字符串数组的列表。

import { Tag } from 'antd'

const TagList = () => {
   const items = ['Tag1', 'Tag2', 'Tag3'];

   return (
      <>
        {
           items.map((item) => (
              <Tag>{item}</Tag>
           )
        }
      </>
   )
}

我如何将其包装在a form.list.list 中,以便最终我可以将数据阵列发送回API端点?用例如重新排序的项目,例如['tag3','tag1','tag2']

我的第一种方法就像

<Form.List name={props.tagListName} initialValue={items}>
   {(fields, { add, remove, move }) => {
      return (
         <div>
            {fields.map((field) => (
               <>
                  <Form.Item {...field}>                  
                     <CustomTagComponent
                        name={id}
                        closable={items.length > 1}
                        isDrag={activeId === id}
                        handleClose={onClose}
                     />
                  </Form.Item>
               </>
            ))}
         </div>
      )
   }}
</Form.List>

我尝试将项目作为初始值添加到列表中,但这没有奏效。

如何创建items.length标签,并结合包含这些值的表单?

I want to create a tag list which represents a string array given by an api.

import { Tag } from 'antd'

const TagList = () => {
   const items = ['Tag1', 'Tag2', 'Tag3'];

   return (
      <>
        {
           items.map((item) => (
              <Tag>{item}</Tag>
           )
        }
      </>
   )
}

How can I wrap this in a Form.List so that in the end I can send the data array back to an api endpoint? With let's say reordered items e.G. ['Tag3', 'Tag1', 'Tag2']

My first approach was like

<Form.List name={props.tagListName} initialValue={items}>
   {(fields, { add, remove, move }) => {
      return (
         <div>
            {fields.map((field) => (
               <>
                  <Form.Item {...field}>                  
                     <CustomTagComponent
                        name={id}
                        closable={items.length > 1}
                        isDrag={activeId === id}
                        handleClose={onClose}
                     />
                  </Form.Item>
               </>
            ))}
         </div>
      )
   }}
</Form.List>

I tried to add items as initialValue to the List, but that didn't work out.

How can I create items.length tags, combined with a Form containing these values?

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

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

发布评论

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

评论(1

好听的两个字的网名 2025-02-13 08:14:15

好吧,我想我找到了一个解决方案:

创建 codesandbox < /a>为此。

评论,意见或更好的解决方案赞赏:)

Ok, I think I found a solution:

Created a Codesandbox for it.

Comments, opinions or better solutions appreciated :)

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