reactjs Material UI中的CSS子选择器

ctrmrzij  于 2023-05-22  发布在  React
关注(0)|答案(6)|浏览(160)

尝试在CSS中编写与此等效的Material UI样式

.deleted td {
    background: red
}

但不知道如何在Material UI的CSS in JS方式中实现。
以下是我目前掌握的相关片段

const styles = theme => ({
    deleted: {
        background: '#eee'
    }
})

<TableRow className={classes.deleted}>
    <TableCell></TableCell>
</TableRow>

它应该生成类似于以下内容的样式:

.deleted td {
    background: red
}
cotxawn7

cotxawn71#

根据@josh的建议,使用&

deleted: {
    "& td": {
      background: "red"
    }
  }

https://codesandbox.io/s/llmq5or1w7

import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import Table from "@material-ui/core/Table";
import TableBody from "@material-ui/core/TableBody";
import TableCell from "@material-ui/core/TableCell";
import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow";
import Paper from "@material-ui/core/Paper";

const styles = theme => ({
  root: {
    width: "100%",
    marginTop: theme.spacing.unit * 3,
    overflowX: "auto"
  },
  table: {
    minWidth: 700
  },
  deleted: {
    "& td": {
      background: "red"
    }
  }
});

let id = 0;
function createData(name, calories, fat, carbs, protein) {
  id += 1;
  return { id, name, calories, fat, carbs, protein };
}

const rows = [
  createData("Frozen yoghurt", 159, 6.0, 24, 4.0),
  createData("Ice cream sandwich", 237, 9.0, 37, 4.3),
  createData("Eclair", 262, 16.0, 24, 6.0),
  createData("Cupcake", 305, 3.7, 67, 4.3),
  createData("Gingerbread", 356, 16.0, 49, 3.9)
];

function SimpleTable(props) {
  const { classes } = props;

  return (
    <Paper className={classes.root}>
      <Table className={classes.table}>
        <TableHead>
          <TableRow>
            <TableCell>Dessert (100g serving)</TableCell>
            <TableCell align="right">Calories</TableCell>
            <TableCell align="right">Fat (g)</TableCell>
            <TableCell align="right">Carbs (g)</TableCell>
            <TableCell align="right">Protein (g)</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {rows.map(row => {
            return (
              <TableRow key={row.id} className={classes.deleted}>
                <TableCell component="th" scope="row">
                  {row.name}
                </TableCell>
                <TableCell align="right">{row.calories}</TableCell>
                <TableCell align="right">{row.fat}</TableCell>
                <TableCell align="right">{row.carbs}</TableCell>
                <TableCell align="right">{row.protein}</TableCell>
              </TableRow>
            );
          })}
        </TableBody>
      </Table>
    </Paper>
  );
}

SimpleTable.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(SimpleTable);
vyswwuz2

vyswwuz22#

如果你想选择你可以使用的所有子项:“& > *”类似于:

root: {
       "& > *": {
       ...
       }
    },
    ...
   
   },
mbyulnm0

mbyulnm03#

import { Box, styled } from "@mui/material";

const StyledBox = styled(Box)({
  // root selector (.MuiBox-root)
  background: "red",

  "&": {
    // '&' points to the root selector which is the same as the above (.MuiBox-root)
    background: "red"
  },
  "&&": {
    // also a root selector but with higher CSS specificity (.MuiBox-root.MuiBox-root)
    background: "red"
  },
  "& .ChildSelector": {
    // child selector (.MuiBox-root .ChildSelector)
    background: "orange",

    // this '&' points to the current selector which is '.MuiBox-root .ChildSelector'
    "& .NestedChildSelector": {
      // nested child selector (.MuiBox-root .ChildSelector .NestedChildSelector) (#1)
      background: "yellow"
    }
  },
  "& .ChildSelector .NestedChildSelector": {
    // same as (#1) (.MuiBox-root .ChildSelector .NestedChildSelector)
    background: "yellow"
  },
});

8fq7wneg

8fq7wneg4#

以防有人在看,下面是如何使用data属性选择子对象:

...
    root: {
    "& span[data-index='0']": {
        transform: 'translateX(-15%)',
    },
    "& span[data-index='3']": {
        ...
    }
   },
   ...
ltqd579y

ltqd579y5#

我正在寻找一种方法来设计webkit子选择器。

audioPlayer: {
  "&::-webkit-media-controls-play-button": {

  }
}

希望能为别人节省时间!

hwamh0ep

hwamh0ep6#

使用@mui v5

您可以在sx API中选择子元素,例如:

<Box
   sx={{
        display: { xs: 'block', sm: 'none' },
        '& img': {  width: 50 },
   }}
>
  <img />
</Box>

相关问题