[英]How to make the Button and TextField pitch perfect?
I'm trying to make a form using Material UI
and React
.我正在尝试使用
Material UI
和React
制作表单。 This is what I have so far:这是我到目前为止:
import React from 'react'
import {Button, TextField} from '@material-ui/core'
import AddIcon from '@material-ui/icons/Add'
import PropTypes from 'prop-types'
class AddItem extends React.Component {
state = {
text: ''
}
handleChange = e => {
this.setState({text: e.target.value})
}
render() {
return (
<form onSubmit={this.props.onSubmit(this.state.text)}>
<TextField id="task" label="Task" placeholder="e.g. Feed the fish" value={this.state.text}
onChange={this.handleChange} color="secondary" variant="outlined" size="small"/>
<Button color="secondary" variant="outlined" endIcon={<AddIcon/>} size="large">
Add
</Button>
</form>
)
}
}
AddItem.propTypes = {
onSubmit: PropTypes.func.isRequired
}
export default AddItem
The result is the following screenshot:结果是以下屏幕截图:
But wait!可是等等! Can you see that?
你能看到吗? The
Button
height and the TextField
height are just so slightly misaligned! Button
高度和TextField
高度只是稍微错位了! (By 2.5px I think). (我认为是 2.5 像素)。 Is there any way to fix this?
有没有什么办法解决这一问题?
This is better than earlier, where the TextField was a lot bigger than the Button (my fix was size="small"
on the TextField).这比之前的 TextField 比 Button 大很多(我的修复是 TextField 上的
size="small"
)要好。
How can I make sure that the TextField and the Button are the same height?如何确保 TextField 和 Button 的高度相同?
I know that in Bulma there's something like a level
component that helps out with that, so is there any similar solution in Material UI
?我知道在 Bulma 中有类似
level
组件的东西可以帮助解决这个问题,那么Material UI
是否有类似的解决方案?
You can make your custom <Button>
styled as you want .您可以根据需要设置自定义
<Button>
样式。
Lets create <StyledButton>
by overwriting style (padding) of outlined and large variant of button .让我们通过覆盖button的轮廓和
<StyledButton>
的样式(填充)来创建<StyledButton>
。
import { withStyles } from "@material-ui/core/styles";
const styles = {
outlinedSizeLarge: {
padding: "6px 21px" // default is "7px 21px" https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/Button/Button.js#L202
}
};
const StyledButton = withStyles(styles)(Button);
Then you can use it in your component:然后你可以在你的组件中使用它:
<form onSubmit={this.props.onSubmit(this.state.text)}>
<TextField id="task" label="Task" placeholder="e.g. Feed the fish" value={this.state.text} onChange={this.handleChange} color="secondary" variant="outlined" size="small"/>
<StyledButton color="secondary" variant="outlined" endIcon={<AddIcon/>} size="large">Add</StyledButton>
</form>
Result:结果:
Live demo现场演示
您是否尝试使用带有outlined
值的variant
属性?
<Button variant='outlined' size='small'>Add</Button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.