【问题标题】:Delete chip in react js Material-ui在 react js Material-ui 中删除芯片
【发布时间】:2020-09-20 12:57:58
【问题描述】:

我使用 react-js 和 Material-ui 芯片 我添加了许多筹码,然后一个筹码出现在文本中。我想要删除芯片的功能。但我不知道如何删除它。

如何使用onDelete={} 删除它?

这只是我的代码的一部分:

import React, { useEffect, useState, Component } from 'react'
import Chip from '@material-ui/core/Chip'

const useStyles = makeStyles(theme => ({
  root: {
    display: 'flex'
  },
  chip: {
    backgroundColor: '#104d56'
  },
}))


  const handleDelete = () => {
       console.log(hashtag);
    };

const studentlist = []

function ResponsiveDrawer(props) {
{
    namefamily.map((item, index) => {
      if (index + 1 > namefamily.length - gpcapacity) {
        if (showstu == item[2]) {
          studentlist.push(
            <Chip

              key={item[2]}
              className={classes.chip}
              clickable={false}
              variant="outlined"
              label={
                <Typography variant="caption" color="textSecondary">
                  {item[0]} {item[1]}
                </Typography>
              }
              onDelete={handele}
              icon={<FaceIcon />}
            />
          )
        } else
          studentlist.push(
            <Chip
              key={item[2]}
              variant="outlined"
              clickable={true}
              label={
                <Typography variant="caption" color="textSecondary">
                  {item[0]} {item[1]}
                </Typography>
              }

              icon={<FaceIcon />}
            />
          )
      }
    })
  }
}

handleDelete() 函数的内部是什么?

【问题讨论】:

标签: reactjs material-ui


【解决方案1】:

据我了解

  1. namefamily 是一个数组数组,即[['a','b','c'],['x','y','z']]
  2. 您想使用if (index + 1 &gt; namefamily.length - gpcapacity) { 条件跳过一些学生筹码
  3. 允许使用showstu == item[2] 条件删除showstu 学生的筹码

Ans:你ResponsiveDrawer看起来像

查看我在代码中的评论了解更多详情。

const ResponsiveDrawer = () => {
//considering you are getting showstu and gpcapacity as props or from somewhere

  const classes = useStyles();
  // point 2. you can skip here instead of checking if everytime
  const [chipData, setChipData] = React.useState(
     namefamily.slice(0, ((namefamily.length - 2) - gpcapacity))
   );

  const handleDelete = (chipToDelete) => {
    setChipData((chips) => chips.filter((chip) => chip[2] !== chipToDelete));
  };

  return (
       chipData.map((item, index) => {
        let chipProps = { clickable:true };
        //point 3. allowing delete to those who is {showstu}
        if (showstu == item[2]) {
          chipProps.onDelete = () => handleDelete(item[2]);
          chipProps.className = classes.chip;
          chipProps.clickable = false;
        }

        return (
          <Chip
              //for key better to use index as a unique
              key={index}
              // this is spread operator (https://reactjs.org/docs/jsx-in-depth.html#spread-attributes) 
              {...chipProps}
              variant="outlined"
              label={
                <Typography variant="caption" color="textSecondary">
                  {item[0]} {item[1]}
                </Typography>
              }
              icon={<FaceIcon />}
            />
        );
      })
  );
}

【讨论】:

    猜你喜欢
    • 2021-02-16
    • 2016-07-05
    • 2019-09-12
    • 2018-02-12
    • 2020-11-19
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多