使用 Material UI + redux-form 创建自定义样式时,redux-form 中的 Field 元素不会应用自定义类名称。当简单地使用 Material UI 中的 FieldText 时。我已经注释掉了下面无效的代码行。
自定义样式对象正在通过原型上定义的名为“类”的道具应用于反应组件。
import React, {Component} from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';
import { Field, reduxForm } from 'redux-form';
import { connect } from 'react-redux';
import {createOrder} from '../actions';
const styles = theme => ({
root: {
display: 'flex',
flexWrap: 'wrap',
justifyContent: 'center'
},
margin: {
margin: theme.spacing.unit,
},
textField: {
flexBasis: 400,
},
button: {
margin: theme.spacing.unit,
},
input: {
display: 'none',
},
});
class Order extends Component {
renderField(field) {
return (
<div>
<TextField
id="outlined-simple-start-adornment"
// className={classNames(classes.margin, classes.textField)}
variant="outlined"
label="Age"
{...field.input}
/>
</div>
)
}
render() {
const { classes } = this.props;
return (
<form >
<div className={classes.root}>
<Field
name="age"
label="age"
component={this.renderField}
/>
<TextField
id="outlined-simple-start-adornment"
className={classNames(classes.margin, classes.textField)}
variant="outlined"
label="Company Name"
/>
</div>
</form>
);
}
}
Order.propTypes = {
classes: PropTypes.object.isRequired,
};
export default reduxForm({
form: 'Form'
})(
connect(null, {createOrder})(withStyles(styles)(Order))
);