[英]Material UI TextField border overlaps with Label
我正在使用 Material UI TextField
和 Material UI Tab
。 我有兩個選項卡,每個選項卡里面都有一個文本字段。 單擊TextField
,標簽的邊框應該打開,但如果當前Tab
不是Tab1
,則不會發生這種情況!
我設法在這個 CodeSandBox 中重現了這個問題,代碼也包含在下面。
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 TextField from "@material-ui/core/TextField";
function TabPanel(props) {
const { children, value, index, ...other } = props;
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
id={`scrollable-force-tabpanel-${index}`}
aria-labelledby={`scrollable-force-tab-${index}`}
{...other}
>
<Box p={1}>{children}</Box>
</Typography>
);
}
TabPanel.propTypes = {
children: PropTypes.node,
index: PropTypes.any.isRequired,
value: PropTypes.any.isRequired
};
function a11yProps(index) {
return {
id: `scrollable-force-tab-${index}`,
"aria-controls": `scrollable-force-tabpanel-${index}`
};
}
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
width: "100%",
backgroundColor: theme.palette.background.paper,
padding: 0,
margin: 0
},
Tab: {
MuiTab: {
root: {
minWidth: "130px"
}
}
}
}));
export default function Demo(props) {
const classes = useStyles();
const [value, setValue] = React.useState(0);
function handleChange(event, newValue) {
setValue(newValue);
console.log(newValue);
}
return (
<div className={classes.root}>
<AppBar position="static" color="default">
<Tabs
key={"tabs"}
value={value}
onChange={handleChange}
variant="scrollable"
scrollButtons="on"
indicatorColor="primary"
textColor="primary"
aria-label="scrollable force tabs example"
>
<Tab
key={"tab1"}
className={classes.Tab}
label={0}
{...a11yProps(0)}
/>
<Tab
key={"tab2"}
className={classes.Tab}
label={1}
{...a11yProps(1)}
/>
</Tabs>
</AppBar>
<TabPanel
key={"panel1"}
value={value}
index={0}
style={{ padding: 0, margin: 0 }}
>
<div key={"div1"}>
hi im tab1{" "}
<TextField
key={"textfield1"}
variant={"outlined"}
margin={"dense"}
label={"im tab 0 textfield"}
/>
</div>
</TabPanel>
<TabPanel
key={"panel2"}
value={value}
index={1}
style={{ padding: 0, margin: 0 }}
>
<div key={"div2"}>
hi im tab2
<TextField
key={"textfield2"}
variant={"outlined"}
margin={"dense"}
label={"im tab 1 textfield"}
/>
</div>
</TabPanel>
</div>
);
}
編輯1:
我設法找到了一個類似的問題...,
Material-UI TextField Outline Label 在條件渲染時與邊框重疊
似乎這與選項卡無關,因為它與條件渲染有關,這對我來說是在使用選項卡時發生的
編輯2:
我嘗試給Textfield
一個鍵,但問題仍然存在並且Textfield
邊框和標簽之間存在重疊,我更新了沙箱以便它可以反映這一點
我在選擇時遇到了重疊問題。 不過場景略有不同。 我瀏覽了很多頁面,但找不到解決這些問題的方法:
所有這些的一個簡單解決方案是以下對我有用的檢查 -
<FormControl variant="outlined">
確保添加了變體。
<InputLabel id="input_designationselected" style={{backgroundColor:'white'}}>Designation*</InputLabel>
確保為標簽設置了背景。 或在此處參考此鏈接https://github.com/mui-org/material-ui/issues/14530
labelId
與InputLabel
的 ID 匹配最終輸出是我們需要的。
它讓我瘋狂了好幾天 - 它認為我也會將它分享給其他人。 對不起,如果這是發布它的錯誤地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.