- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目标
我有一个多个字段:
import React from "react";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
interface User {
id: number;
name: string;
}
const userList: User[] = [
{ id: 1, name: "name 1" },
{ id: 2, name: "name 2" },
{ id: 3, name: "name 3" }
];
export default function AutocompleteControlled() {
const [users, setUsers] = React.useState<number[]>([]);
const [inputValue, setInputValue] = React.useState("");
return (
<div>
<br />
<Autocomplete
multiple
value={userList.filter((el) => users.includes(el.id))}
onChange={(event: any, newValue: User[]) => {
setUsers(newValue.map((el) => el.id));
}}
inputValue={inputValue}
onInputChange={(event: any, newInputValue: string) => {
console.log("newInputValue", newInputValue);
setInputValue(newInputValue);
}}
id="controllable-states-demo"
options={userList}
getOptionLabel={(option) => option.name}
renderInput={(params) => (
<TextField {...params} label="Controllable" variant="outlined" />
)}
/>
</div>
);
}
欢迎所有评论:)
最佳答案
您想启用多选,并允许用户在文本框中输入任意值。因此,您可以使用 Autocomplete
freesolo
的组件prop 设置为 true,因此文本框可以包含任意值。
This是最接近您的用例的 Material-UI 示例。
我们将使用受控组件,因此您可以使用 value
来控制其行为。和 onChange
Prop 。检查下面的代码。
您可以从预先填写的项目中进行选择,也可以输入任意值并按回车键,将向组件添加一个芯片,并将该值添加到该状态的数组中。
目前value
是名称数组,但您可以将其设置为 id
s 或任何你想要的。
试试这个代码:
import React from "react";
import Chip from "@material-ui/core/Chip";
import Autocomplete from "@material-ui/lab/Autocomplete";
import { createStyles, makeStyles, Theme } from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
const useStyles = makeStyles((theme: Theme) =>
createStyles({
root: {
width: 500,
"& > * + *": {
marginTop: theme.spacing(3),
},
},
})
);
interface User {
id: number;
name: string;
}
const userList: User[] = [
{ id: 1, name: "name 1" },
{ id: 2, name: "name 2" },
{ id: 3, name: "name 3" },
{ id: 4, name: "name 4" },
{ id: 5, name: "name 5" },
];
export default function AutocompleteControlled() {
const classes = useStyles();
const [value, setValue] = React.useState<any>([userList[0].name]);
console.log("value: ", value);
return (
<div className={classes.root}>
<Autocomplete
value={value}
onChange={(event, newValue) => {
setValue(newValue);
}}
multiple
id="tags-filled"
options={userList.map((option) => option.name)}
freeSolo
renderTags={(value: string[], getTagProps) =>
value.map((option: string, index: number) => (
<Chip
variant="outlined"
label={option}
{...getTagProps({ index })}
/>
))
}
renderInput={(params) => (
<TextField
{...params}
variant="filled"
label="Users"
placeholder="Search"
/>
)}
/>
</div>
);
}
这就是它的外观,受控值记录到控制台。
关于javascript - 如何使用 Material-UI Autocomplete 组件选择多个选项并添加新选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67234558/
我正在尝试让 Ace Editor 支持我自己的查询语言的自动完成功能。 查询本身如下所示 city:newyork color:red color:blue 在上述情况下,我希望用户在输入“c”时可
我有一个 Material-UI 组件,我正在使用它,您可以在其中输入某人的姓名,它将提供一个可供选择的人员列表。这是一个非常标准的用例,但是我需要表单中的所选项目与标签不同。 目前,如果您选择标有
我开始使用 atom IDE 来编写 HTML+ CSS 代码。 autocomplete-atom-api 被激活,但仍然没有代码自动完成,也没有当我按下 ctrl+space 时。 这是我的 ke
我成功地使用了 API,但今天早上遇到错误,文本框中出现“糟糕!出了点问题”,用户无法输入。我发现该问题与 key 相关并已修复,但是,这表明可能会出现一些问题,并且由于此阻塞,用户无法完成。我希望能
我无法弄清楚这两个流行插件中的哪一个在 Sublime 3 环境中的自动完成功能中运行得更好。究竟有什么区别? 此外,我想知道他们是否可以在不搞砸的情况下正常工作。你能帮我解决这个问题吗? 可以找到插
我显然遗漏了一些东西,但是当我尝试将自动完成添加到文本框时,我不断收到此消息:.autocomplete 不是函数 _布局(仅头部): @ViewBag.Title - My ASP.NET
是否有一种标准方法可以使用 ElasticSearch 为小字段(例如地名)实现逐字符输入自动完成。 (在撰写本文时,可以通过搜索进行大量讨论,但似乎没有什么是确定的。(另外,我看到有人谈论 Apac
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 3 年前。
我正在使用 rails3-jquery-autocomplete gem 来自动完成用户名,搜索属性是 lastname 但我想显示全名作为显示值。这是我的 Controller 中的内容 autoc
我有一个 jQuery 自动完成插件,当我复制/粘贴到它时,它不会触发。有什么办法让它工作吗? 最佳答案 $("#ID").bind("paste", function () { setTim
我有 2 个自动完成 UITextField这将在输入文本时触发表格 View 。其中一个我正在使用MVPlaceSearchTextField .我使用 this 自定义构建的另一个. 当我输入 t
我在一个页面上有多个自动完成。 当我对所有文本字段使用 jQuery UI AutoComplete 时,它会添加表单的动态选择列表 Entry 1 有一个 CSS 样式,ui-au
我在 Google Places API Web 服务中使用 Place Autocomplete,以下 API 调用工作正常: https://maps.googleapis.com/maps/ap
我已经在 angular 5 项目上安装了primeng。 我正在使用 p-autoComplete 组件,但现在发生此错误。 我应该如何解决这个错误? 这是我的源代码。 Basic 最佳答案 通常
我不断出现这个错误: “无效的 DOM 属性 autocomplete 。您是说 autoComplete 吗?” 当我在 上编译我的 React 项目时谷歌浏览器 . 当我尝试在 上编译代码时火狐
我想创建一个动态自动完成输入,可以使用 metroui 进行更新和它类似 jquery m4q 。基于on its documentation ,我需要在input标签中设置data-autocomp
我有两个 下拉菜单。我想根据第一个下拉列表的选择更新第二个下拉列表的搜索列表。 这是一个准备好的非工作插件:http://plnkr.co/edit/GxQujjAcxYdawlANJd9O?p=pr
我们有一个标准的地址表格(街道 1、街道 2、城市、州、邮政编码、国家/地区)。 当我在 Street 1 添加 Google Places Autocomplete API 的代码时,它会在加载页面
我有 md-autocomplete : {{item.name}} :: {{item.type}} 带指令:ng-enter . 我的目标:当用户按下 En
我正在为一家医院创建一个动态表单。我在这里遇到数据自动完成的问题。问题是在将 json 数组更新为 data-autocomplete=' ' 后,它没有更新 data-autocomplete='
我是一名优秀的程序员,十分优秀!