admin管理员组

文章数量:1391934

I'm working with older versions of material-ui with no possibility to upgrade.

I am trying to change the background of the Paper ponent based on a few binations of the props. I don't think it's plicated to require use of the makeStyles HOC. Is this possible?

I think the problem is this line: classes={{ root: correctBackgroundColor.root }}

but the documentation on the unhelpfully says "Other properties (not documented) are applied to the root element."

import React from "react";

const correctBackgroundColor = {
  root: {
    width: 30,
    height: 30,
    border: "1px solid lightgrey",
    backgroundColor: props => {
      if (props.ledIsOn === true && props.ledColorType === "Green") {
        return "#00FF00";
      }
      if (props.ledIsOn === true && props.ledColorType === "Orange") {
        return "#FFA500";
      } 
    }
  }
};

class ColorChange extends React.Component {
  render() {
    const { classes } = this.props;
    let textToRender = (
      <Paper
        id={this.props.id}
        classes={{ root: correctBackgroundColor.root }}
      />
    );
    return (
      <div>
        <Typography variant="p" className={classes.typography_root}>
          {this.props.textLabel}
        </Typography>
        {textToRender}
      </div>
    );
  }
}

export default withStyles(styles)(ColorChange);

there is a sandbox at : TIA!

I'm working with older versions of material-ui with no possibility to upgrade.

I am trying to change the background of the Paper ponent based on a few binations of the props. I don't think it's plicated to require use of the makeStyles HOC. Is this possible?

I think the problem is this line: classes={{ root: correctBackgroundColor.root }}

but the documentation on the https://v0.material-ui./#/ponents/paper unhelpfully says "Other properties (not documented) are applied to the root element."

import React from "react";

const correctBackgroundColor = {
  root: {
    width: 30,
    height: 30,
    border: "1px solid lightgrey",
    backgroundColor: props => {
      if (props.ledIsOn === true && props.ledColorType === "Green") {
        return "#00FF00";
      }
      if (props.ledIsOn === true && props.ledColorType === "Orange") {
        return "#FFA500";
      } 
    }
  }
};

class ColorChange extends React.Component {
  render() {
    const { classes } = this.props;
    let textToRender = (
      <Paper
        id={this.props.id}
        classes={{ root: correctBackgroundColor.root }}
      />
    );
    return (
      <div>
        <Typography variant="p" className={classes.typography_root}>
          {this.props.textLabel}
        </Typography>
        {textToRender}
      </div>
    );
  }
}

export default withStyles(styles)(ColorChange);

there is a sandbox at : https://codesandbox.io/s/adoring-bell-oyzsn TIA!

Share Improve this question edited Aug 13, 2020 at 10:54 SomoKRoceS 3,0533 gold badges24 silver badges32 bronze badges asked Jul 15, 2020 at 10:23 tmctmc 5141 gold badge8 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9 +50

I hope i got you right. There are two things you should put attention to:

First, correctBackgroundColor does not recognize props because this is out of scope, therefore, I would rement to change it into a function, pass the props to it, and make the function return a style object.

Second thing, I would use style when applying the object to Paper, so the style of that paper would be a call to correctBackgroundColor with the props, like this:

<Paper id={this.props.id} style={correctBackgroundColor(this.props)} />

Final code:

import React from "react";
import withStyles from "@material-ui/core/styles/withStyles";
import Typography from "@material-ui/core/Typography/Typography";
import Paper from "@material-ui/core/Paper/Paper";

const styles = theme => ({
  typography_root: {
    fontSize: 12,
    margin: 10
  }
});
const correctBackgroundColor = props => {
  let bgSwitch = "none";
  if (props.ledIsOn === true && props.ledColorType === "Green")
    bgSwitch = "#00FF00";
  if (props.ledIsOn === true && props.ledColorType === "Orange")
    bgSwitch = "#FFA500";
  if (props.ledIsOn === true && props.ledColorType === "Red")
    bgSwitch = "#FF0000";
  if (props.ledIsOn === true && props.ledColorType === "Grey")
    bgSwitch = "lightGrey";
  return {
    width: 30,
    height: 30,
    border: "1px solid lightgrey",
    backgroundColor: bgSwitch
  };
};

class ColorChange extends React.Component {
  render() {
    const { classes } = this.props;
    let textToRender = (
      <Paper id={this.props.id} style={correctBackgroundColor(this.props)} />
    );
    return (
      <div>
        <Typography variant="p" className={classes.typography_root}>
          {this.props.textLabel}
        </Typography>
        {textToRender}
      </div>
    );
  }
}

export default withStyles(styles)(ColorChange); //with styles injects classes props with styles contained.

Code Sandbox

本文标签: javascriptReact class componentsconditional styling based on propsStack Overflow