admin管理员组文章数量:1290258
I use Material-UI TextField
with an Icon
inside like this. The label "Your good name here" should be next to the icon instead of overlapping it.
This happens after I added InputLabelProps={{ shrink: false }}
to the TextField
.
How could I fix this position correctly? Any help!
Thanks in advance! =)
This is my code pen
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Input from "@material-ui/core/Input";
import InputLabel from "@material-ui/core/InputLabel";
import InputAdornment from "@material-ui/core/InputAdornment";
import FormControl from "@material-ui/core/FormControl";
import TextField from "@material-ui/core/TextField";
import Grid from "@material-ui/core/Grid";
import AccountCircle from "@material-ui/icons/AccountCircle";
const useStyles = makeStyles((theme) => ({
margin: {
margin: theme.spacing(1)
}
}));
export default function InputWithIcon() {
const classes = useStyles();
return (
<div>
<TextField
className={classes.margin}
id="input-with-icon-textfield"
label="Your good name here"
InputLabelProps={{ shrink: false }}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<AccountCircle />
</InputAdornment>
)
}}
/>
<TextField
className={classes.margin}
id="input-with-icon-textfield"
label="Your good name here"
// InputLabelProps={{ shrink: false }}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<AccountCircle />
</InputAdornment>
)
}}
/>
</div>
);
}
I use Material-UI TextField
with an Icon
inside like this. The label "Your good name here" should be next to the icon instead of overlapping it.
This happens after I added InputLabelProps={{ shrink: false }}
to the TextField
.
How could I fix this position correctly? Any help!
Thanks in advance! =)
This is my code pen
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Input from "@material-ui/core/Input";
import InputLabel from "@material-ui/core/InputLabel";
import InputAdornment from "@material-ui/core/InputAdornment";
import FormControl from "@material-ui/core/FormControl";
import TextField from "@material-ui/core/TextField";
import Grid from "@material-ui/core/Grid";
import AccountCircle from "@material-ui/icons/AccountCircle";
const useStyles = makeStyles((theme) => ({
margin: {
margin: theme.spacing(1)
}
}));
export default function InputWithIcon() {
const classes = useStyles();
return (
<div>
<TextField
className={classes.margin}
id="input-with-icon-textfield"
label="Your good name here"
InputLabelProps={{ shrink: false }}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<AccountCircle />
</InputAdornment>
)
}}
/>
<TextField
className={classes.margin}
id="input-with-icon-textfield"
label="Your good name here"
// InputLabelProps={{ shrink: false }}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<AccountCircle />
</InputAdornment>
)
}}
/>
</div>
);
}
Share
Improve this question
edited Mar 27, 2021 at 3:31
Ryan Cogswell
81.1k9 gold badges241 silver badges212 bronze badges
asked Mar 27, 2021 at 2:41
margherita pizzamargherita pizza
7,18529 gold badges102 silver badges178 bronze badges
1 Answer
Reset to default 6Below are the relevant default styles for the label:
formControl: {
position: 'absolute',
left: 0,
top: 0,
transform: 'translate(0, 24px) scale(1)',
},
/* Styles applied to the `input` element if `shrink={true}`. */
shrink: {
transform: 'translate(0, 1.5px) scale(0.75)',
transformOrigin: 'top left',
},
To fix the positioning of the label to account for the width of the input adornment, you need to modify the x-axis portion of the translate CSS (e.g. translate(32px, 24px) scale(1)
).
Below is a working example based on your sandbox:
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import InputAdornment from "@material-ui/core/InputAdornment";
import TextField from "@material-ui/core/TextField";
import AccountCircle from "@material-ui/icons/AccountCircle";
const useStyles = makeStyles((theme) => ({
margin: {
margin: theme.spacing(1)
},
inputLabelNoShrink: {
transform: "translate(32px, 24px) scale(1)"
}
}));
export default function InputWithIcon() {
const classes = useStyles();
const [value, setValue] = React.useState("");
const shrink = value.length > 0;
return (
<div>
<TextField
className={classes.margin}
id="input-with-icon-textfield"
label="Your good name here"
value={value}
onChange={(event) => setValue(event.target.value)}
InputLabelProps={{
shrink: shrink,
className: shrink ? undefined : classes.inputLabelNoShrink
}}
InputProps={{
startAdornment: (
<InputAdornment position="start">
<AccountCircle />
</InputAdornment>
)
}}
/>
</div>
);
}
版权声明:本文标题:javascript - Label position for Material-UI text field with icon adornment when shrink false - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741425381a2378046.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论