gpt4 book ai didi

javascript - React Material Multi Select 默认值未选中

转载 作者:行者123 更新时间:2023-12-02 21:07:20 25 4
gpt4 key购买 nike

我正在使用 multiple-values checkbox 开发 React Material 自动完成组件似乎 defaultValue 属性和复选框示例存在问题。

如果我将列表中的一个项目设置为defaultValue,它将出现在文本输入标签中,但不会在自动完成下拉列表中显示为选中状态。我尝试将项目设置为检查是否存在于 defaultValue 中,但这并没有解决问题。

如何将默认值项目设置为在自动完成下拉列表中选中?

Condsandbox

代码:

import React from "react";
import Checkbox from "@material-ui/core/Checkbox";
import TextField from "@material-ui/core/TextField";
import Autocomplete from "@material-ui/lab/Autocomplete";
import CheckBoxOutlineBlankIcon from "@material-ui/icons/CheckBoxOutlineBlank";
import CheckBoxIcon from "@material-ui/icons/CheckBox";

const icon = <CheckBoxOutlineBlankIcon fontSize="small" />;
const checkedIcon = <CheckBoxIcon fontSize="small" />;

export default function CheckboxesTags() {
return (
<Autocomplete
multiple
id="checkboxes-tags-demo"
options={top100Films}
defaultValue={[{ title: "The Shawshank Redemption", year: 1994 }]}
disableCloseOnSelect
getOptionLabel={option => option.title}
renderOption={(option, { selected }) => (
<React.Fragment>
<Checkbox
icon={icon}
checkedIcon={checkedIcon}
style={{ marginRight: 8 }}
checked={selected}
/>
{option.title}
</React.Fragment>
)}
style={{ width: 500 }}
renderInput={params => (
<TextField
{...params}
variant="outlined"
label="Checkboxes"
placeholder="Favorites"
/>
)}
/>
);
}

// Top 100 films as rated by IMDb users. http://www.imdb.com/chart/top
const top100Films = [
{ title: "The Shawshank Redemption", year: 1994 },
{ title: "The Godfather", year: 1972 },
{ title: "The Godfather: Part II", year: 1974 },
{ title: "The Dark Knight", year: 2008 },
{ title: "12 Angry Men", year: 1957 },
{ title: "Schindler's List", year: 1993 },
{ title: "Pulp Fiction", year: 1994 },
{ title: "The Lord of the Rings: The Return of the King", year: 2003 },
{ title: "The Good, the Bad and the Ugly", year: 1966 },
{ title: "Fight Club", year: 1999 },
{ title: "The Lord of the Rings: The Fellowship of the Ring", year: 2001 },
{ title: "Star Wars: Episode V - The Empire Strikes Back", year: 1980 },
{ title: "Forrest Gump", year: 1994 },
{ title: "Inception", year: 2010 },
{ title: "The Lord of the Rings: The Two Towers", year: 2002 },
{ title: "One Flew Over the Cuckoo's Nest", year: 1975 },
{ title: "Goodfellas", year: 1990 },
{ title: "The Matrix", year: 1999 },
{ title: "Seven Samurai", year: 1954 },
{ title: "Star Wars: Episode IV - A New Hope", year: 1977 },
{ title: "City of God", year: 2002 },
{ title: "Se7en", year: 1995 },
{ title: "The Silence of the Lambs", year: 1991 },
{ title: "It's a Wonderful Life", year: 1946 },
{ title: "Life Is Beautiful", year: 1997 },
{ title: "The Usual Suspects", year: 1995 },
{ title: "Léon: The Professional", year: 1994 },
{ title: "Spirited Away", year: 2001 },
{ title: "Saving Private Ryan", year: 1998 },
{ title: "Once Upon a Time in the West", year: 1968 },
{ title: "American History X", year: 1998 },
{ title: "Interstellar", year: 2014 }
];

enter image description here

最佳答案

如果您想使用列表中的值作为 Autocomplete 组件的 defaultValue - 您应该使用确切的值(在您的情况下 -是您拥有的 options 的一部分的确切对象。

您可以使用以下命令来执行此操作(例如):

<Autocomplete
options={ top100Films }
defaultValue={ [ top100Films[0] ] }
...
/>

关于javascript - React Material Multi Select 默认值未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61192976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com