reactjs - 在 Material-UI 中将芯片/标签移到自动完成框之外

标签 reactjs autocomplete material-ui

我正在使用 Material-UI Autocomplete 组件,并试图弄清楚是否可以将筹码/标签移到输入框之外。这甚至可能..?我更愿意将筹码列在盒子下方。这样,文本框可以单独用于用户输入,而不是同时显示筹码/标签和用户输入。
我曾尝试玩其中一个基本演示,但没有任何运气。我把它清除回默认状态,以防这里有一些用户有这方面的经验。示例的起点可以是以下沙箱
https://codesandbox.io/s/material-demo-forked-7vroo?file=/demo.js

最佳答案

您可以通过禁用 Autocomplete 中的标签呈现并在 Chip 下方添加您自己的 Autocomplete 列表来实现。

const [value, setValue] = useState([]);
const onDelete = (title) => () => {
  setValue((value) => value.filter((v) => v.title !== title));
};

return (
  <div>
    <Autocomplete
      multiple
      options={top100Films}
      getOptionLabel={(option) => option.title}
      value={value}
      onChange={(e, newValue) => setValue(newValue)}
      renderTags={() => null} // don't render tag in the TextField
      renderInput={(params) => (
        <TextField {...params} variant="outlined" placeholder="Favorites" />
      )}
    />
    <div>
      // manage and render your own tag list here instead
      {value.map((v) => (
        <Chip key={v.title} label={v.title} onDelete={onDelete(v.title)} />
      ))}
    </div>
  </div>
);
const top100Films = [
  { title: "The Shawshank Redemption", year: 1994 },
  { title: "The Godfather", year: 1972 },
]
现场演示
Edit 66881433/moving-the-chips-tags-out-of-the-autocomplete-box-in-material-ui

关于reactjs - 在 Material-UI 中将芯片/标签移到自动完成框之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66881433/

相关文章:

javascript - 组件不会在路由更改时重新呈现 - React HashRouter

reactjs - react 与 Material -UI : Theme undefined in createStyles() with TypeScript

javascript - createSwitchNavigator-TypeError : Cannot call a class as a function

javascript - React - 按性别或年龄过滤列表

python - 为什么 PyCharm 不建议为 Django 模型自动填充?

c++ - 无法解析基于模板的标识符 "get".Netbeans 8.1

javascript - useEffect 仅运行一次,刷新后不会再次获取

material-ui - Material ui 中的 CSS 子选择器

javascript - 类型错误:this.transferPropsTo 不是一个函数

python - 如何在 IPython 中完成 win32com 代码?