Reactjs 当用户是管理员时,更改注释模块中芯片(材质UI)的不同背景颜色
我想改变芯片的背景颜色,当用户是管理员,在一个评论模块,就像在图片这是我得到的Reactjs 当用户是管理员时,更改注释模块中芯片(材质UI)的不同背景颜色,reactjs,react-redux,material-ui,Reactjs,React Redux,Material Ui,我想改变芯片的背景颜色,当用户是管理员,在一个评论模块,就像在图片这是我得到的 <Chip label={comment['user.firstname']} style={styles.chip} /> 您可以根据数据添加不同的类名。见代码沙盒- 上述代码不正确,因为它将始终返回“蓝色”。(任何数字%0始终为“NaN”)。我想这应该是代码中的问题。请修复并让
<Chip
label={comment['user.firstname']}
style={styles.chip}
/>
您可以根据数据添加不同的类名。见代码沙盒- 上述代码不正确,因为它将始终返回“蓝色”。(任何数字%0始终为“NaN”)。我想这应该是代码中的问题。请修复并让我知道它是否有效 根据更多信息进一步编辑: 代码沙盒-
我已经创建了一个代码沙盒来模拟您的需求。我使用的是一个包含用户数据的JS文件。我已经给出了基于用户名设置芯片颜色的逻辑,还注释了为每个备用用户设置芯片颜色的逻辑(user.id%2==0)。希望这有帮助。您可以根据数据添加不同的类名。见代码沙盒- 上述代码不正确,因为它将始终返回“蓝色”。(任何数字%0始终为“NaN”)。我想这应该是代码中的问题。请修复并让我知道它是否有效 根据更多信息进一步编辑: 代码沙盒- 我已经创建了一个代码沙盒来模拟您的需求。我使用的是一个包含用户数据的JS文件。我已经给出了基于用户名设置芯片颜色的逻辑,还注释了为每个备用用户设置芯片颜色的逻辑(user.id%2==0)。希望这能有所帮助。这是我的代码。我的想法是为管理员用户检查我的api,每个管理员用户都有一个与普通用户不同的颜色
`import React from 'react';
import TimeAgo from 'react-timeago';
import PropTypes from 'prop-types';
import Card from 'components/commons/Card';
import Chip from '@material-ui/core/Chip';
import Button from '@material-ui/core/Button';
import { Grid } from '@material-ui/core';
import { getUser } from 'api';
const styles = {
root: {
paddingTop: '25px',
color: '#FFFFFF'
},
chip: {
color: 'white',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
height: '40px',
lineHeight: '32px',
margin: '2px 15px',
minWidth: '50px',
backgroundColor: 'blue'
},
chipUserAdmin:{
color: 'white',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
height: '40px',
lineHeight: '32px',
margin: '2px 15px',
minWidth: '50px',
backgroundColor: getUser.id % 0 === 2 ? 'red':'blue'
},
ChipUserNormal:{
color: 'white',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
height: '40px',
lineHeight: '32px',
margin: '2px 15px',
minWidth: '50px',
backgroundColor: 'gray'
},
Button: {
fontWeight: 'bold',
padding: '6%',
paddingTop: '3%',
paddingBottom: '3%',
fontSize: '1rem',
letterSpacing: '0px',
textTransform: 'uppercase',
borderRadius: '2.5px',
textAlign: 'center'
},
};
class Comment extends React.Component {
state = {
comment: '',
};
onChange = e => {
this.setState({ [e.target.name]: e.target.value });
}
renderComments(comments) {
const commentDivs = comments.map(comment => {
return (
<div key={comment.id}>
<div className="row">
<div className="col comment">{comment.description}</div>
</div>
<div className="row">
<div className="col-9">
<TimeAgo className="time-ago" date={comment.createdAt} />
</div>
<div className="col-3">
<div style={styles.wrapper}>
<span className="time-ago">
<Chip
label={comment['user.firstname']}
style={{ backgroundColor: comment.id % 2 === 0 ? 'blue': 'red' || styles.chipUserAdmin }}
/>
</span>
</div>
</div>
</div>
<hr/>
</div>
)
});
return (
<div>{commentDivs}</div>
);
}
render() {
return (
<Card title="comments">
<Grid item xs={12} md={12}>
{this.renderComments(this.props.comments)}
<div className="form-group">
<textarea
className="form-control"
name="comment"
placeholder="Write a comment"
onChange={this.onChange}
value={this.state.comment}
rows="3"
></textarea>
</div>
<div className="form-group">
<Button
onClick={() => this.props.onSubmit(this.state.comment)}
color="primary"
variant="contained"
size="large"
style={styles.Button}>
Send
</Button>
</div>
</Grid>
</Card>
);
}
}
Comment.propTypes = {
comments: PropTypes.array.isRequired,
onSubmit: PropTypes.func.isRequired,
};
export default Comment;`
`import React from'React';
从“react TimeAgo”导入TimeAgo;
从“道具类型”导入道具类型;
从“组件/公用程序/卡”导入卡;
从“@material ui/core/Chip”导入芯片;
从“@material ui/core/Button”导入按钮;
从'@material ui/core'导入{Grid};
从“api”导入{getUser};
常量样式={
根目录:{
paddingTop:'25px',
颜色:“#FFFFFF”
},
芯片:{
颜色:'白色',
位置:'相对',
显示:“flex”,
flexDirection:'行',
对齐项目:“居中”,
高度:'40px',
线宽:“32px”,
边距:“2x15px”,
最小宽度:“50px”,
背景颜色:“蓝色”
},
chipUserAdmin:{
颜色:'白色',
位置:'相对',
显示:“flex”,
flexDirection:'行',
对齐项目:“居中”,
高度:'40px',
线宽:“32px”,
边距:“2x15px”,
最小宽度:“50px”,
背景颜色:getUser.id%0==2?'red':'blue'
},
ChipUserNormal:{
颜色:'白色',
位置:'相对',
显示:“flex”,
flexDirection:'行',
对齐项目:“居中”,
高度:'40px',
线宽:“32px”,
边距:“2x15px”,
最小宽度:“50px”,
背景颜色:“灰色”
},
按钮:{
fontWeight:'粗体',
填充:“6%”,
paddingTop:'3%',
填充底部:“3%”,
fontSize:'1rem',
字母间距:“0px”,
textTransform:'大写',
边界半径:“2.5px”,
textAlign:“中心”
},
};
类注释扩展了React.Component{
状态={
评论:“”,
};
onChange=e=>{
this.setState({[e.target.name]:e.target.value});
}
renderComments(注释){
const commentDivs=comments.map(comment=>{
返回(
{comment.description}
)
});
返回(
{commentDivs}
);
}
render(){
返回(
{this.renderComments(this.props.comments)}
this.props.onSubmit(this.state.comment)}
color=“primary”
variant=“包含”
size=“大”
style={style.Button}>
发送
);
}
}
Comment.propTypes={
注释:需要PropTypes.array.isRequired,
onSubmit:PropTypes.func.isRequired,
};
导出默认注释`
这是我的代码想法是为管理员用户检查我的api,每个管理员用户都有一个与普通用户不同的颜色
`import React from 'react';
import TimeAgo from 'react-timeago';
import PropTypes from 'prop-types';
import Card from 'components/commons/Card';
import Chip from '@material-ui/core/Chip';
import Button from '@material-ui/core/Button';
import { Grid } from '@material-ui/core';
import { getUser } from 'api';
const styles = {
root: {
paddingTop: '25px',
color: '#FFFFFF'
},
chip: {
color: 'white',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
height: '40px',
lineHeight: '32px',
margin: '2px 15px',
minWidth: '50px',
backgroundColor: 'blue'
},
chipUserAdmin:{
color: 'white',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
height: '40px',
lineHeight: '32px',
margin: '2px 15px',
minWidth: '50px',
backgroundColor: getUser.id % 0 === 2 ? 'red':'blue'
},
ChipUserNormal:{
color: 'white',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
height: '40px',
lineHeight: '32px',
margin: '2px 15px',
minWidth: '50px',
backgroundColor: 'gray'
},
Button: {
fontWeight: 'bold',
padding: '6%',
paddingTop: '3%',
paddingBottom: '3%',
fontSize: '1rem',
letterSpacing: '0px',
textTransform: 'uppercase',
borderRadius: '2.5px',
textAlign: 'center'
},
};
class Comment extends React.Component {
state = {
comment: '',
};
onChange = e => {
this.setState({ [e.target.name]: e.target.value });
}
renderComments(comments) {
const commentDivs = comments.map(comment => {
return (
<div key={comment.id}>
<div className="row">
<div className="col comment">{comment.description}</div>
</div>
<div className="row">
<div className="col-9">
<TimeAgo className="time-ago" date={comment.createdAt} />
</div>
<div className="col-3">
<div style={styles.wrapper}>
<span className="time-ago">
<Chip
label={comment['user.firstname']}
style={{ backgroundColor: comment.id % 2 === 0 ? 'blue': 'red' || styles.chipUserAdmin }}
/>
</span>
</div>
</div>
</div>
<hr/>
</div>
)
});
return (
<div>{commentDivs}</div>
);
}
render() {
return (
<Card title="comments">
<Grid item xs={12} md={12}>
{this.renderComments(this.props.comments)}
<div className="form-group">
<textarea
className="form-control"
name="comment"
placeholder="Write a comment"
onChange={this.onChange}
value={this.state.comment}
rows="3"
></textarea>
</div>
<div className="form-group">
<Button
onClick={() => this.props.onSubmit(this.state.comment)}
color="primary"
variant="contained"
size="large"
style={styles.Button}>
Send
</Button>
</div>
</Grid>
</Card>
);
}
}
Comment.propTypes = {
comments: PropTypes.array.isRequired,
onSubmit: PropTypes.func.isRequired,
};
export default Comment;`
`import React from'React';
从“react TimeAgo”导入TimeAgo;
从“道具类型”导入道具类型;
从“组件/公用程序/卡”导入卡;
从“@material ui/core/Chip”导入芯片;
从“@material ui/core/Button”导入按钮;
从'@material ui/core'导入{Grid};
从“api”导入{getUser};
常量样式={
根目录:{
paddingTop:'25px',
颜色:“#FFFFFF”
},
芯片:{
颜色:'白色',
位置:'相对',
显示:“flex”,
flexDirection:'行',
对齐项目:“居中”,
高度:'40px',
线宽:“32px”,
边距:“2x15px”,
最小宽度:“50px”,
背景颜色:“蓝色”
},
chipUserAdmin:{
颜色:'白色',
位置:'相对',
显示:“flex”,
flexDirection:'行',
对齐项目:“居中”,
高度:'40px',
线宽:“32px”,
边距:“2x15px”,
最小宽度:“50px”,
背景颜色:getUser.id%0==2?'red':'blue'
},
ChipUserNormal:{
颜色:'白色',
位置:'相对',
显示:“flex”,
flexDirection:'行',
对齐项目:“居中”,
高度:'40px',
线宽:“32px”,
边距:“2x15px”,
最小宽度:“50px”,
背景颜色:“灰色”
},
按钮:{
fontWeight:'粗体',
填充:“6%”,
paddingTop:'3%',
填充底部:“3%”,
fontSize:'1rem',
字母间距:“0px”,
textTransform:'大写',
边界半径:“2.5px”,
textAlign:“中心”
},
};
类注释扩展了React.Component{
状态={
评论:“”,
};
onChange=e=>{
this.setState({[e.target.name]:e.target
`import React from 'react';
import TimeAgo from 'react-timeago';
import PropTypes from 'prop-types';
import Card from 'components/commons/Card';
import Chip from '@material-ui/core/Chip';
import Button from '@material-ui/core/Button';
import { Grid } from '@material-ui/core';
import { getUser } from 'api';
const styles = {
root: {
paddingTop: '25px',
color: '#FFFFFF'
},
chip: {
color: 'white',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
height: '40px',
lineHeight: '32px',
margin: '2px 15px',
minWidth: '50px',
backgroundColor: 'blue'
},
chipUserAdmin:{
color: 'white',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
height: '40px',
lineHeight: '32px',
margin: '2px 15px',
minWidth: '50px',
backgroundColor: getUser.id % 0 === 2 ? 'red':'blue'
},
ChipUserNormal:{
color: 'white',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
height: '40px',
lineHeight: '32px',
margin: '2px 15px',
minWidth: '50px',
backgroundColor: 'gray'
},
Button: {
fontWeight: 'bold',
padding: '6%',
paddingTop: '3%',
paddingBottom: '3%',
fontSize: '1rem',
letterSpacing: '0px',
textTransform: 'uppercase',
borderRadius: '2.5px',
textAlign: 'center'
},
};
class Comment extends React.Component {
state = {
comment: '',
};
onChange = e => {
this.setState({ [e.target.name]: e.target.value });
}
renderComments(comments) {
const commentDivs = comments.map(comment => {
return (
<div key={comment.id}>
<div className="row">
<div className="col comment">{comment.description}</div>
</div>
<div className="row">
<div className="col-9">
<TimeAgo className="time-ago" date={comment.createdAt} />
</div>
<div className="col-3">
<div style={styles.wrapper}>
<span className="time-ago">
<Chip
label={comment['user.firstname']}
style={{ backgroundColor: comment.id % 2 === 0 ? 'blue': 'red' || styles.chipUserAdmin }}
/>
</span>
</div>
</div>
</div>
<hr/>
</div>
)
});
return (
<div>{commentDivs}</div>
);
}
render() {
return (
<Card title="comments">
<Grid item xs={12} md={12}>
{this.renderComments(this.props.comments)}
<div className="form-group">
<textarea
className="form-control"
name="comment"
placeholder="Write a comment"
onChange={this.onChange}
value={this.state.comment}
rows="3"
></textarea>
</div>
<div className="form-group">
<Button
onClick={() => this.props.onSubmit(this.state.comment)}
color="primary"
variant="contained"
size="large"
style={styles.Button}>
Send
</Button>
</div>
</Grid>
</Card>
);
}
}
Comment.propTypes = {
comments: PropTypes.array.isRequired,
onSubmit: PropTypes.func.isRequired,
};
export default Comment;`