reactjs - MUI 组件上的 fullWidth 和 className
问题描述
我想在 Material UI TextField 组件上同时使用 className 和 fullWidth 属性,但这似乎不起作用。
仅使用 className 可以正常工作,并且仅使用 fullWidth 也可以正常工作,但是当我尝试同时使用这两者时,它只会监听 className。
//Only className, works fine
<TextField
id="date"
label="Only className"
type="date"
defaultValue="2017-05-23"
className={classes.textField}
InputLabelProps={{
shrink: true
}}
/>
//Only fullWidth, works fine
<TextField
id="date"
label="Only fullWidth"
type="date"
defaultValue="2017-05-24"
fullWidth={true}
InputLabelProps={{
shrink: true
}}
/>
//Both, not working (only className is applied)
<TextField
id="date"
label="Both"
type="date"
defaultValue="2017-05-25"
className={classes.textField}
fullWidth={true}
InputLabelProps={{
shrink: true
}}
/>
解决方案
在您的风格中,您通过提供自己的宽度TextField
来覆盖道具 200px 。fullWidth
textField: {
marginLeft: theme.spacing(1),
marginRight: theme.spacing(1),
width: 200
}
将其更改为:
textField: {
marginLeft: theme.spacing(1),
marginRight: theme.spacing(1),
width: '100%'
}
或者
只需从 textField 样式中删除宽度
textField: {
marginLeft: theme.spacing(1),
marginRight: theme.spacing(1),
}
然后在你的 TextField 中使用fullWidth=true
<TextField
id="date"
label="Birthday"
type="date"
defaultValue="2017-05-25"
className={classes.textField}
fullWidth={true}
InputLabelProps={{
shrink: true
}}
/>
推荐阅读
- ios - 在 iOS 上以编程方式控制通知“显示预览”选项
- django - Vue警告:属性或方法'processLogout'未在实例上定义,但在渲染期间定义
- r - 基于R中的多个相似变量创建变量
- powershell - 删除域重新排序用户名powershell
- tslint - 如何禁用命名导入必须按字母顺序排列 - tslint
- random - PRNG 的速度
- java - 将 Liquibase 与 Play Java 框架一起使用
- sql - 按用户 ID 的 DATEDIFF
- vba - 访问vba:出错时进入下一次迭代
- javascript - how to remove a class from a div on page scroll at particular height