javascript - 如何在 React Material UI TextField 中居中占位符和文本

标签 javascript css reactjs material-ui jsx

当前设计:

Current design

预期设计:

enter image description here

TextField 标签如下所示:

<TextField
        multiline={false}
        autoFocus
        placeholder={props.defaultAmt}
        helperText="Enter amount to pay"
        margin="normal"
        InputProps={{
            startAdornment: <InputAdornment position="start">₹</InputAdornment>
        }}
        />

我想将所有内容置于 TextField 的中心。我试过使用 textAlign: 'center' 但它不起作用。

最佳答案

您可以覆盖 InputAdornment 组件中的 positionStart 规则,边距百分比取决于您的大小和影响文本字段样式的其他参数。我会建议这样的事情(我假设您正在使用 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>
    }}
/>

关于javascript - 如何在 React Material UI TextField 中居中占位符和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57852393/

相关文章:

javascript - 如何合并状态和 Prop ,然后将它们作为 Prop 传递?

javascript - enzyme 内部错误 : configured enzyme adapter did not inherit from the EnzymeAdapter base class

javascript - 在 JavaScript 中搜索字符串内部

javascript - map 内的 React.js map 获得意外标记

javascript - 接受带参数的javascript函数作为参数(无包装器)?

html - 带有固定标题的滚动 UL

android - Safari Repeating-y 背景拉伸(stretch)宽度

css - 在 block 和无之间切换表格单元格背景

javascript - 如果有条件地检查 react jsx 中的空值 [es6]

reactjs - "composes"无法使用 babel-plugin-react-css-modules