如何获取并设置自动完成的多个值?

huangapple go评论73阅读模式
英文:

How to get and set Autocomplete Multiple values?

问题

我按照这个指南创建了一个文本框。
但我无法获得此字段的值。
有什么建议吗?

https://mui.com/material-ui/react-autocomplete/#multiple-values

 const categories = [
   {id:1, name: "blog"},
   {id:2, name: "music"},
   {id:3, name: "video"},
]
 const [category, setCategory]: any = useState([]);

 <Autocomplete
                                            multiple
                                            limitTags={1}
                                            value={category}
                                            onChange={(event, newValue) => {
                                                setCategory([
                                                    ...category,
                                                    newValue
                                                ]);
                                            }}
                                            id="category-filter"
                                            options={categories}
                                            getOptionLabel={(option) => option.name}
                                            renderInput={(params) => (
                                                <TextField {...params} label="Category" placeholder="categories" />
                                            )}

                                        />
英文:

I following this guild made a textfield.
But i can not got this field value,
Any suggestions?

https://mui.com/material-ui/react-autocomplete/#multiple-values

 const categories = [
   {id:1, name: "blog"},
   {id:2, name: "music"},
   {id:3, name: "video"},
]
 const [category, setCategory]: any = useState([]);

 <Autocomplete
                                            multiple
                                            limitTags={1}
                                            value={category}
                                            onChange={(event, newValue) => {
                                                setCategory([
                                                    ...category,
                                                    newValue
                                                ]);
                                            }}
                                            id="category-filter"
                                            options={categories}
                                            getOptionLabel={(option) => option.name}
                                            renderInput={(params) => (
                                                <TextField {...params} label="Category" placeholder="categories" />
                                            )}

                                        />

答案1

得分: 1

The newValue 在 onChange 处理程序中已经是选定选项的数组,所以您可以简单地将其设置到 category 中。另外,由于每个选项都是一个对象,您必须添加 isOptionEqualToValue 属性,以告诉组件如何比较选项与所选值。 代码如下:

import { useState } from "react";
import { Autocomplete, TextField } from "@mui/material";

export const Test = () => {
  const categories = [
    { id: 1, name: "blog" },
    { id: 2, name: "music" },
    { id: 3, name: "video" }
  ];
  const [category, setCategory] = useState([]);
  return (
    <Autocomplete
      multiple
      limitTags={1}
      value={category}
      onChange={(event, newValue) => {
        setCategory(newValue);
      }}
      id="category-filter"
      options={categories}
      getOptionLabel={(option) => option.name}
      isOptionEqualToValue={(option, value) => option.id === value.id}
      renderInput={(params) => (
        <TextField {...params} label="Category" placeholder="categories" />
      )}
    />
  );
};
英文:

The newValue in onChange handler is already an array of selected options, so you can just set it into the category simply.
Also since each option is an object, you have to add isOptionEqualToValue attribute to tell the component how to compare the option to the selected value. The code is as follows:

import { useState } from &quot;react&quot;;
import { Autocomplete, TextField } from &quot;@mui/material&quot;;

export const Test = () =&gt; {
  const categories = [
    { id: 1, name: &quot;blog&quot; },
    { id: 2, name: &quot;music&quot; },
    { id: 3, name: &quot;video&quot; }
  ];
  const [category, setCategory] = useState([]);
  return (
    &lt;Autocomplete
      multiple
      limitTags={1}
      value={category}
      onChange={(event, newValue) =&gt; {
        setCategory(newValue);
      }}
      id=&quot;category-filter&quot;
      options={categories}
      getOptionLabel={(option) =&gt; option.name}
      isOptionEqualToValue={(option, value) =&gt; option.id === value.id}
      renderInput={(params) =&gt; (
        &lt;TextField {...params} label=&quot;Category&quot; placeholder=&quot;categories&quot; /&gt;
      )}
    /&gt;
  );
};

huangapple
  • 本文由 发表于 2023年4月17日 19:29:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定