![](/img/trans.png)
[英]ReactJS: How can I center the placeholder and text for Material-UI's <DatePicker/>?
[英]How to center placeholder and text in React Material UI TextField
您可以覆蓋InputAdornment
組件中的positionStart
規則,邊距百分比取決於您的大小和影響您的文本字段樣式的其他參數。 我會建議這樣的事情(我假設你正在生成你的 styles 與makeStyles
,但通過你自己的方式進行調整)。
要使 helperText 居中,只需將 Typography 組件添加到 prop 中,因為它是節點類型。 向 Typography 組件添加樣式以使文本居中,它應該可以工作:
const useStyles = makeStyles(() => ({
centerAdornment: {
marginLeft: "50%" // or your relevant measure
},
centerText: {
textAlign: "center"
}
}));
最重要的是:
<TextField
multiline={false}
autoFocus
placeholder={props.defaultAmt}
helperText={
<Typography
variant="caption"
className={classes.centerText}
display="block"
>
Enter amount to pay
</Typography>
}
margin="normal"
InputProps={{
startAdornment: (
<InputAdornment
position="start"
classes={{ positionStart: classes.centerAdornment}}
>
₹
</InputAdornment>
}}
/>
您可以設置 Child div alignment 並減小 TextBox 寬度來實現此目的。 創造這樣的風格,
const styles = {
textBox: {
"& $div": {
justifyContent: "center",
"& $input": {
width: "30%"
}
},
"& $p": {
alignSelf: "center"
}
}
};
然后將樣式設置為 TextField
<TextField
className={props.classes.textBox}
multiline={false}
autoFocus
placeholder={"5000"}
helperText="Enter amount to pay"
margin="normal"
InputProps={{
startAdornment: <InputAdornment position="start">₹</InputAdornment>
}}
/>
這是有效的 CodeSandbox 示例。
對於助手,簡單的做:
<TextField
...
error={isError}
helperText={errorMessage}
FormHelperTextProps={{
style: {
textAlign: 'center'
}
}}
/>
hintStyle={{ width: '600px', textAlign: 'center' }}
添加此道具並嘗試
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.