如何从Material-UI React更改Paper组件的边框颜色?

huangapple go评论83阅读模式
英文:

How to change the borderColor of Paper component from material-ui react?

问题

我正在使用React和Material-UI,在Material-UI中,我尝试对Paper组件进行一些自定义,比如添加绿色的边框。

这是我尝试过的代码:

<Paper 
   style={{ padding: 50, }}
   variant="outlined" square={true}
   classes={{
      root: classes.root, // class name, e.g. `classes-nesting-root-x`
   }}
>
   这是Paper组件
</Paper>

这是我的自定义样式:

root: {
    borderRadius: 20,
    borderColor: '#000'
}

borderRadius属性正常工作,但borderColor属性不起作用。

有什么建议吗?

英文:

I'm using react with material-ui, In material-ui, im trying to make use of Paper component with some customization like adding borderColor to green.

Here is what i tried,

 &lt;Paper 
       style={{ padding: 50, }}
       variant=&quot;outlined&quot; square={true}
       classes={{
          root: classes.root, // class name, e.g. `classes-nesting-root-x`
       }}
   &gt;
       This paper component                     
  &lt;/Paper&gt;   

Here is my customization styles..

root: {
        borderRadius: 20,
        borderColor: &#39;#000&#39;
    },

BorderRadius property is working fine, But BorderColor property is not working,

Any suggestions?

答案1

得分: 6

使用withStyles在你的组件中。这将允许你覆盖样式:

import React, { Component } from "react";
import withStyles from "@material-ui/core/styles/withStyles";
import Paper from "@material-ui/core/Paper";

const styles = () => ({
  root: { borderRadius: 20, borderColor: "#000", padding: 50 }
});

export class ExampleComponent extends Component {
  render() {
    const {classes} = this.props;
    return (
      <Paper
        className={classes.root}
        variant="outlined"
        square={true}
      >
        This paper component
      </Paper>
    );
  }
}

export default withStyles(styles)(ExampleComponent);

如果你有一个主题,只需重新编写样式,这将首先解构主题文件和其中的属性,然后执行(或覆盖)你在这个对象中制定的样式:

const styles = theme => ({
  ...theme,
  paper: { borderRadius: 20, borderColor: "#000", padding: 50 }
});

对于函数式组件:

import React from 'react';
import { styled } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';

const MyPaper = styled(Paper)({ borderRadius: 20, borderColor: "#000", padding: 50 });

export default function StyledComponents() {
  return <MyPaper>Styled Components</MyPaper>;
}

或者

import React from 'react';
import { withStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';

const styles = {
  root: { borderRadius: 20, borderColor: "#000", padding: 50 },
};

function AnotherComponent(props) {
  const { classes } = props;
  return <Paper className={classes.root}>Another component</Paper>;
}

export default withStyles(styles)(AnotherComponent);
英文:

Use it with withstyles in your component. This will allow you to override styles:

import React, { Component } from &quot;react&quot;;
import withStyles from &quot;@material-ui/core/styles/withStyles&quot;;
import Paper from &quot;@material-ui/core/Paper&quot;;

const styles = () =&gt; ({
  root: { borderRadius: 20, borderColor: &quot;#000&quot;, padding: 50 }
});

export class ExampleComponent extends Component {
  render() {
    const {classes} = this.props;
    return;
    &lt;Paper
      className={classes.root}
      variant=&quot;outlined&quot;
      square={true}
    &gt;
      This paper component
    &lt;/Paper&gt;;
  }
}

export default withStyles(styles)(ExampleComponent);

If you have a theme, just rewrite the styles, this will first destructure the theme file and the properties in it, after then will execute (or override) the styles you make in this object:

const styles = theme =&gt; ({
  ...theme,
  paper: { borderRadius: 20, borderColor: &quot;#000&quot;, padding: 50 }
});

for functional component:

import React from &#39;react&#39;;
import { styled } from &#39;@material-ui/core/styles&#39;;
import Paper from &#39;@material-ui/core/Paper&#39;;

const MyPaper = styled(Paper)({ borderRadius: 20, borderColor: &quot;#000&quot;, padding: 50 });

export default function StyledComponents() {
  return &lt;MyPaper&gt;Styled Components&lt;/MyPaper&gt;;
}

or

import React from &#39;react&#39;;
import { withStyles } from &#39;@material-ui/core/styles&#39;;
import Paper from &#39;@material-ui/core/Paper&#39;;

const styles = {
  root: { borderRadius: 20, borderColor: &quot;#000&quot;, padding: 50 },
};

function AnotherComponent(props) {
  const { classes } = props;
  return &lt;Paper className={classes.root}&gt;Another component&lt;/Paper&gt;;
}


export default withStyles(styles)(AnotherComponent);

答案2

得分: 1

在Material-UI v5中,您可以轻松使用sx属性来更改颜色。您的代码将如下所示:

<Paper sx={{ p: '50px', borderRadius: '20px', borderColor: 'green' }} variant="outlined">
     Paper组件的内容
</Paper>

注意: 对于paddingborder-radius,如果您不想使用Material-UI主题间距(例如,使用主题缩放因子padding: 1将变为padding: '8px'),则最有可能需要使用px单位,参见文档

英文:

In Material-UI v5 you can easily use the sx-property to change the color. Your code would look like that:

&lt;Paper sx={{p: &#39;50px&#39;, borderRadius: &#39;20px&#39;, borderColor: &#39;green&#39;}} variant=&quot;outlined&quot;&gt;
 Content of Paper Component                     
&lt;/Paper&gt;

Note: For padding and border-radius you most likely have to use the px unit if you don't want to use the Material-UI theme spacing (e.g. with the theme scaling factor padding: 1 would be padding: &#39;8px&#39;, see the docs)

答案3

得分: 1

将全局设置纸张边框样式可以通过以下方式实现

createTheme(theme, {
  components: {
    MuiPaper: {
      styleOverrides: {
        rounded: { border: "solid red", borderRadius: 5 },
      },
    },
英文:

Setting paper's border styling globally can be achieved the following way

createTheme(theme, {
  components: {
    MuiPaper: {
      styleOverrides: {
        rounded: { border: &quot;solid red&quot;, borderRadius: 5 },
      },
    },

huangapple
  • 本文由 发表于 2020年1月3日 20:49:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定