【发布时间】:2017-11-24 11:08:37
【问题描述】:
所以我继承了一个项目,我需要在其中使用FieldArrays 来创建一个动态的项目列表。它所在的“表单”实际上不在<form> 中,因此我不得不将我的FieldArrays 代码分割成自己的组件,以便让redux 神奇地让它“工作”来做我想做的事情希望它做,例如用数据预填充字段,能够轻松添加/删除字段等。
问题是我似乎无法使用来自更高组件的道具预先填充组件,以便 redux FieldArray 可以将其拾取,无论我尝试了什么。
这是我的代码:
renderFlooring({ fields, meta: { touched, error, submitFailed } }) {
return (
<div>
<div>
<div className={styles.flooringHeading}>
<h3> Site Specifications </h3>
<button type="button" className={styles.add} onClick={() => fields.push({})}>Add Square Footage</button>
</div>
{(touched || submitFailed) && error && <span>{error}</span>}
{fields.length === 0 &&
<p className={styles.noFlooring}>No square footage or flooring type has been added.</p>
}
</div>
<ul className={styles.sqFtList}>
{fields.map((flooring, index) =>
<li className={styles.card} key={index}>
<div className="page-form__block">
<p> Estimate the total area of work in square feet </p>
<Field name={`${flooring}.square_footage`} onChange={this.changeFlooringSquareFootage} component={TextField} hintText="Square Feet" />
</div>
<div className="page-form__block">
<p> Enter Floor Type </p>
<Field name={`${flooring}.flooring_type`} onChange={this.changeFlooringType} component={TextField} hintText="Floor Type (VCT, Carpet, Vinyle)" />
</div>
<button
type="button"
title="Remove"
className={styles.remove}
onClick={() => {
fields.remove(index);
this.props.removeFlooring(index);
}}>Remove</button>
</li>
)}
</ul>
</div>
);
}
render() {
return (
<FieldArray name="flooring" component={this.renderFlooring}/>
);
}
}
export default reduxForm({
form: 'manageSquareFootageForm',
initialValues: this.props.flooring
})(ManageSquareFootageForm);
导入此特定组件的组件如下所示:
<ManageSquareFootageForm
changeFlooringType={this.changeFlooringType}
changeFlooringSquareFootage={this.changeFlooringSquareFootage}
removeFlooring={this.removeFlooring}
flooring={this.props.flooring}
/>
我什至尝试在我的<ManageSquareFootageForm> 上添加initialValues,但这最终导致我的fields.push({}) 功能停止工作。
我的数据如下所示:
[
{square_footage: "500", flooring_type: "carpet"},
{square_footage: "1000", flooring_type: "carpet"}
]
有什么我可以直接做的吗?
提前致谢!
【问题讨论】:
标签: reactjs redux redux-form