Компонент Tabs
клонирует его дочерние элементы (предположительно Tab
элементы) для передачи дополнительных свойств (например, свойств, связанных с вкладкой «selected»).
Предупреждения вызваны тем, что эти дополнительные свойства передаются компоненту Button
.Вы можете исправить эти предупреждения, заключив Button
в компонент, который игнорирует дополнительные свойства, передаваемые Tabs
, такие как:
const ButtonInTabs = ({ className, onClick, children }) => {
return <Button className={className} onClick={onClick} children={children} />;
};
Полный рабочий пример:
import React from "react";
import PropTypes from "prop-types";
import { makeStyles } 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 Button from "@material-ui/core/Button";
import AddIcon from "@material-ui/icons/Add";
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
id={`simple-tabpanel-${index}`}
aria-labelledby={`simple-tab-${index}`}
{...other}
>
<Box p={3}>{children}</Box>
</Typography>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.any.isRequired,
value: PropTypes.any.isRequired
};
function a11yProps(index) {
return {
id: `simple-tab-${index}`,
"aria-controls": `simple-tabpanel-${index}`
};
}
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper
},
addButton: {
color: "white"
}
}));
const ButtonInTabs = ({ className, onClick, children }) => {
return <Button className={className} onClick={onClick} children={children} />;
};
export default function SimpleTabs() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const [showThirdTab, setShowThirdTab] = React.useState(false);
const handleChange = (event, newValue) => {
setValue(newValue);
};
return (
<div className={classes.root}>
<AppBar position="static">
<Tabs
value={value}
onChange={handleChange}
aria-label="simple tabs example"
>
<Tab label="Item One" {...a11yProps(0)} />
<Tab label="Item Two" {...a11yProps(1)} />
{showThirdTab && <Tab label="Item Three" {...a11yProps(2)} />}
<ButtonInTabs
onClick={() => setShowThirdTab(true)}
className={classes.addButton}
>
<AddIcon color="secondary" />
New Tab
</ButtonInTabs>
</Tabs>
</AppBar>
<TabPanel value={value} index={0}>
Item One
</TabPanel>
<TabPanel value={value} index={1}>
Item Two
</TabPanel>
<TabPanel value={value} index={2}>
Item Three
</TabPanel>
</div>
);
}
![Edit Button in Tabs](https://codesandbox.io/static/img/play-codesandbox.svg)