首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在自动完成freeSolo上删除前获得mui芯片值?

如何在自动完成freeSolo上删除前获得mui芯片值?
EN

Stack Overflow用户
提问于 2021-12-05 05:15:16
回答 1查看 749关注 0票数 1

我正在使用mui库中的AutocompleteChip组件。这是演示 (标准样板)。

在删除芯片内容之前,我无法得到它:

代码语言:javascript
运行
复制
<Autocomplete
          multiple
          id="tags-filled"
          options={top100Films.map((option) => option.title)}
          defaultValue={[top100Films[1].title]}
          freeSolo
          onKeyDown={(prop) => {
              if (prop.key === 'Enter') {
                  console.log(prop.target.value)
              }
          }} 
          renderTags={(value, getTagProps) =>
              value.map((option, index) => (
                  <Chip
                      onDelete={(s) => console.log("the one", option)}
                      key={index} variant="outlined"
                      label={option} {...getTagProps({ index })} />
              ))
          }
          renderInput={(params) => (
              <TextField
                  {...params}
                  variant="filled"
                  label="freeSolo"
                  placeholder="Favorites"
              />
          )}
      />

问题是

代码语言:javascript
运行
复制
renderTags={(value, getTagProps) =>
   value.map((option, index) => (
      <Chip
         onDelete={(s) => console.log("the one", option)}
         key={index} variant="outlined"
         label={option} {...getTagProps({ index })} />
       ))
    }

如果我删除了{...getTagProps({ index })},我确实使onDelete按我所需要的方式工作,但实际的删除却无法工作。再一次,这里的演示

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-05 06:34:41

您可以使用

代码语言:javascript
运行
复制
onChange={(e, value, situation, option) => {
              if (situation === "removeOption") {

               //write your code here
                console.log("--->", e, value, situation, option);
              }
              setReceivers((state) => value);
            }}

而不是像这样的onDelete:

代码语言:javascript
运行
复制
import * as React from "react";
import Chip from "@mui/material/Chip";
import Autocomplete from "@mui/material/Autocomplete";
import TextField from "@mui/material/TextField";
import Stack from "@mui/material/Stack";
export default function Tags() {
  const [val, setVal] = React.useState({});
  const [receivers, setReceivers] = React.useState([]);
  console.log(receivers);
  const handleClick = () => {
    setVal(top100Films[0]); //you pass any value from the array of top100Films
    // set value in TextField from dropdown list
  };
  return (
    <Stack spacing={1} sx={{ width: 500 }}>
      <Autocomplete
        multiple
        id="tags-filled"
        options={top100Films.map((option) => option.title)}
        defaultValue={[top100Films[13].title]}
        freeSolo
        onChange={(e, value, situation, option) => {
          if (situation === "removeOption") {
            console.log("--->", e, value, situation, option);
          }
          setReceivers((state) => value);
        }}
        renderTags={(value, getTagProps) =>
          value.map((option, index) => (
            <Chip
              variant="outlined"
              label={option}
              {...getTagProps({ index })}
            />
          ))
        }
        renderInput={(params) => (
          <TextField
            {...params}
            variant="filled"
            label="freeSolo"
            placeholder="Favorites"
          />
        )}
      />
    </Stack>
  );
}

码箱

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70231653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档