我有用于我的React Project的material-ui的导航选项卡。选定的活动选项卡必须具有背景色,然后在不活动时切换回原始颜色。透明控件同时应用于这两个选项卡,而不是应用于选定的活动选项卡下面的代码。我只想为活动标签应用一些背景色。有解决办法吗?
这是导航标签的代码
import React from 'react';
import PropTypes from 'prop-types';
import SwipeableViews from 'react-swipeable-views';
import { makeStyles, useTheme } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Tabs from '@material-ui/core/Tabs';
import Tab from '@material-ui/core/Tab';
import Typography from '@material-ui/core/Typography';
import Box from '@material-ui/core/Box';
import ForecastDays from '../forecast/index';
const TabPanel = (props) => {
const { children, value, index, ...other } = props;
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
id={`full-width-tabpanel-${index}`}
aria-labelledby={`full-width-tab-${index}`}
{...other}
>
<Box p={3}>{children}</Box>
</Typography>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.any.isRequired,
value: PropTypes.any.isRequired,
};
const a11yProps = (index) => {
return {
id: `full-width-tab-${index}`,
'aria-controls': `full-width-tabpanel-${index}`
};
}
const useStyles = makeStyles(theme => ({
root: {
backgroundColor: 'none',
width: 275,
marginLeft:72,
marginTop:40
},
}));
const DailyHourly = (props) => {
const classes = useStyles();
const theme = useTheme();
const [value, setValue] = React.useState(0);
const handleChange = (event, newValue) => {
setValue(newValue);
};
const handleChangeIndex = index => {
setValue(index);
};
const {forecastdays, hourlyforecast} = props
return (
<div className={classes.root}>
<AppBar position="static" color="default" style={{background: 'transparent'}}>
<Tabs
value={value}
onChange={handleChange}
indicatorColor="secondary"
textColor="primary"
variant="fullWidth"
aria-label="full width tabs example"
>
<Tab label="Daily" {...a11yProps(0)}
/>
<Tab label="Hourly" {...a11yProps(1)}
/>
</Tabs>
</AppBar>
<SwipeableViews
axis={theme.direction === 'rtl' ? 'x-reverse' : 'x'}
index={value}
onChangeIndex={handleChangeIndex}
>
<TabPanel value={value} index={0} dir={theme.direction}>
<ForecastDays forecastdays={forecastdays}/>
</TabPanel>
<TabPanel value={value} index={1} dir={theme.direction}>
<ForecastDays hourlyforecast={hourlyforecast}/>
</TabPanel>
</SwipeableViews>
</div>
);
}
export default DailyHourly
当心图像,以供参考。 任何建议,高度赞赏...在此先感谢
答案 0 :(得分:1)
您可以基于当前的活动选项卡值添加一个类。像下面这样。如果您事先不知道选项卡的数量,则可能必须创建另一个函数。
<Tab className={value === 0 ? classes.active : ""} label="Daily" {...a11yProps(0)}
/>
<Tab className={value === 1 ? classes.active : ""} label="Hourly" {...a11yProps(1)}
/>
Codesandbox-https://codesandbox.io/s/pedantic-ives-t7sir