【问题标题】:Moving the chips/tags outside Autocomplete box in MUI将芯片/标签移到 MUI 中的自动完成框之外
【发布时间】:2021-06-27 02:08:36
【问题描述】:

我正在使用 MUI Autocomplete 组件,并试图弄清楚是否可以将芯片/标签 移出 输入框。这甚至可能吗?我宁愿把筹码列在盒子下面。这样,文本框可以单独用于用户输入,而不是同时显示芯片/标签和用户输入。

我曾尝试使用其中一个基本演示,但没有任何运气。我将其清除回默认状态,以防这里有一些用户对此有经验。示例的起点可以是以下沙箱

https://codesandbox.io/s/material-demo-forked-7vroo?file=/demo.js

【问题讨论】:

    标签: reactjs autocomplete material-ui


    【解决方案1】:

    您可以通过禁用Autocomplete 内的标签呈现并在Autocomplete 下方添加您自己的Chip 列表来做到这一点。

    const [value, setValue] = useState([]);
    const onDelete = (title) => () => {
      setValue((value) => value.filter((v) => v.title !== title));
    };
    
    return (
      <Box sx={{ width: 500 }}>
        <Autocomplete
          multiple
          options={top100Films}
          defaultValue={[top100Films[13]]}
          getOptionLabel={(option) => option.title}
          value={value}
          onChange={(e, newValue) => setValue(newValue)}
          renderTags={() => null}
          renderInput={(params) => (
            <TextField {...params} variant="outlined" placeholder="Favorites" />
          )}
        />
        <Box
          mt={3}
          sx={{
            '& > :not(:last-child)': { marginRight: 1 },
            '& > *': { marginBottom: 1 },
          }}
        >
          {value.map((v) => (
            <Chip key={v.title} label={v.title} onDelete={onDelete(v.title)} />
          ))}
        </Box>
      </Box>
    );
    
    const top100Films = [
      { title: "The Shawshank Redemption", year: 1994 },
      { title: "The Godfather", year: 1972 },
    ]
    

    现场演示

    V5

    V4

    【讨论】:

      猜你喜欢
      • 2021-12-07
      • 1970-01-01
      • 2020-03-14
      • 2019-05-04
      • 2022-01-21
      • 2020-09-07
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多