简体   繁体   English

使用 Material UI 访问 renderValue 组件上的反应事件处理程序时出现问题

[英]Problems accessing react event handler on renderedValue component using Material UI

I am trying to access the onDelete event handler in the chip component using Material UI in the follow code snippet.我正在尝试使用以下代码片段中的 Material UI 访问chip组件中的onDelete事件处理程序。

When I click on the chip , the Select behavior (opening a dropdown) occurs.当我点击chip ,会发生Select行为(打开下拉菜单)。

Is there any way to override this behavior so I could also delete the chips?有什么方法可以覆盖这种行为,以便我也可以删除筹码?

Demo here on code sandbox: https://codesandbox.io/s/purple-thunder-s0isr在代码沙箱上演示: https : //codesandbox.io/s/purple-thunder-s0isr

<Select
  labelId="demo-mutiple-chip-label"
  id="demo-mutiple-chip"
  key=""
  multiple
  value={["Fred", "Tom"]}]
  input={<Input id="select-multiple-chips" />}
  renderValue={() =>
    ["Fred", "Tom"].map((name) => (
      <Chip
        label={name}
        key={name}
        onDelete={() => {
          console.log("Delete clicked")
        }}
      />
    ))
  }
></Select>

see sample :见示例:

import React, {useState} from "react";
import ReactDOM from "react-dom";
import { Select, Chip, Input, MenuItem } from "@material-ui/core";

import "./styles.css";

function App() {

  const [chipData, setChipData] = React.useState([
    { key: 0, label: 'A' },
    { key: 1, label: 'B' },
    { key: 2, label: 'C' },
    { key: 3, label: 'D' },
    { key: 4, label: 'E' }
  ]);  

  const [selectedChips, setSelectedChips] = React.useState([]);
  const [chipName, setChipName] = React.useState([]);
  const [canOpen, setCanOpen] = React.useState(false);

  const handleDelete = chipToDelete => () => {        
    let s = selectedChips.filter(chip => chip.key !== chipToDelete.key);
    setSelectedChips(s);
    setChipName(s);
  };

  const selectHandleOnOpen = event =>{    
    setCanOpen(!canOpen);    
  }

  const handleChange = event => {        
    setChipName(event.target.value);    
    setSelectedChips(event.target.value);
  };


  return (

    <Select
      style={{width: "300px"}}
      labelId="demo-mutiple-chip-label"
      id="demo-mutiple-chip"      
      multiple
      onChange={handleChange}
      value={chipName}      
      onClick={selectHandleOnOpen}      
      open={canOpen}
      renderValue={selected =>
        selectedChips.map(item => (          
          <Chip
            clickable={true}
            label={item.label}
            key={item.key}            
            onDelete={handleDelete(item)}
            style={{margin: "5px"}}  
          />

        ))
      }

    >
      {chipData.map(item => (
            <MenuItem key={'MenuItem-'+item.key} value={item} >
              {item.label}
            </MenuItem>
          ))}
      </Select>

  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

see sample output here: https://codesandbox.io/s/flamboyant-saha-felni在此处查看示例输出: https : //codesandbox.io/s/flamboyant-saha-felni

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在事件处理程序中访问React组件参数 - Accessing React component params in event handler 如何使用 react-testing-library 测试材质 ui MenuList 组件上的按键按下事件 - How to test key down event on material ui MenuList component using react-testing-library 将click事件传递给嵌套组件 - React | 材料的UI - Pass click event to nested component - React | Material-UI React Popover 组件 - Material UI - React Popover component - Material UI 直接在 React 组件中使用 Material-ui 和 SCSS - using material-ui with SCSS directly in React component 如何在反应中使用材料 ui 或 mui 创建可重用的下拉组件? - How to create reusable dropdown component using material ui or mui in react? 如何使用React JS围绕材料UI组件编写包装器? - How to write a wrapper around a material UI component using React JS? 如何使用“this”在react组件中编写内联js事件处理程序代码 - how to write inline js event handler code in the react component using “this” 如何在不使用任何事件处理程序的情况下重新加载反应组件? - How to reload a react component without using any event handler? 如何在不点击react功能组件中的任何事件的情况下获取material UI组件的当前state? - How can get the current state of the material UI component without clicking on any event in react functional component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM