reactjs - 如何将材质 ui 组件包装为字段的 redux 表单组件?
问题描述
我想了解如何将material ui
组件包装到redux-form
Field 组件中?
<FormControl variant='outlined' className={classes.textField}>
<InputLabel
ref={ref => {
this.InputLabelRef = ref
}}
htmlFor='outlined-county-simple'
>
{this.state.countryName}
</InputLabel>
<Select
value={this.state.country}
onChange={this.handleChangeCountry}
input={
<OutlinedInput
labelWidth={this.state.labelWidth}
name='country'
/>
}
>
{isData(country) ? country.map((entity, index) => {
return (
<MenuItem value={entity.code} key={`county-${index}`}>{entity.country}</MenuItem>)
}) : null}
</Select>
</FormControl>
结果我想从这个组件获取数据到 redux-form 存储,这种情况是用户将选择的。我可以通过使用 redux 的 change 函数来实现,但我认为这不是正确的工作方式。我有示例如何将 TextField 组件包装到 redux 表单组件中
import mapProps from 'recompose/mapProps'
import TextField from '@material-ui/core/TextField'
export default mapProps(({
input: {name, onChange, value},
meta: {touched, error},
helperText,
...restProps
}) => ({
error: !!(touched && error),
fullWidth: true,
helperText: (touched && error) ? error : helperText,
margin: 'normal',
onChange: event => onChange(event.target.value),
value,
...restProps
}))(TextField)
解决方案
这应该有效。我们可以将组件传递给 Field,如代码片段所示,在我的例子中是 renderSelectField。
<Field
name="favoriteColor"
component={renderSelectField}
label="Favorite Color"
>
推荐阅读
- c - 读取函数指针变量值
- c# - 参数不显示在预览报告页面上
- javascript - 如何更改会话内的数组的特定值
- java - OpenJDK11:安全提供程序设置
- fortran - 错误: (1) 处的 DATA 语句中的语法错误
- c# - 改进具有相关父 ID 的条目的递归计数
- c - 使用存储链表每个节点地址的数组查找两个singlyLinkedlist的合并点
- python - Sagemaker 使用经过处理的腌制 ndarray 而不是来自 S3 的 csv 文件
- .net-core - 如何在.net core api中传递多个实体或json作为参数
- nestjs - Nestjs Crud:过滤加入请求以仅返回当前用户的数据