【发布时间】:2017-03-30 12:31:01
【问题描述】:
我想要完成的是一个类似于redux form example 中的动态 FieldArray 但不同之处在于我添加到列表中的每个对象都已经具有所有属性(我必须显示)但是一个我需要从其他两个计算。
看看这个例子:
import React from 'react'
import { compose } from 'redux';
import { connect } from 'react-redux';
import { Field, FieldArray, reduxForm, formValueSelector } from 'redux-form'
const renderField = ({ input, label, type, meta: { touched, error } }) => (
<div>
<label>{label}</label>
<div>
<input {...input} type={type} placeholder={label}/>
{touched && error && <span>{error}</span>}
</div>
</div>
)
const renderSelect = (products) => (
<div>
<label>Select Product to Add</label>
<div>
<Field name="products" component="select">
<option></option>
{products.map(p => <option value={p.id}>{p.name}</option>)}
</Field>
</div>
</div>
);
const renderCompetences = ({ fields, meta: { touched, error, submitFailed }}) => {
return (
<div>
{/* <div>
<button type="button" onClick={() => fields.push({})}>Add Competence</button>
{(touched || submitFailed) && error && <span>{error}</span>}
</div> */}
<ul>
{fields.map((competence, index) =>
<li key={index}>
<h4>Competence #{index + 1}</h4>
<Field
name={`${competence}.singlePrice`}
type="number"
component={renderField}
label="Single Price"/>
<Field
name={`${competence}.standardQuantity`}
type="number"
component={renderField}
label="Standard Quantity"/>
<Field
name={`${competence}.totalPrice`}
type="number"
component={renderField}
label="Total Price"/>
<button
type="button"
onClick={() => fields.remove(index)}
style={{color: 'red'}}
>
✘
</button>
</li>
)}
</ul>
</div>
);
}
const FieldArraysForm = (props) => {
const { handleSubmit, pristine, reset, submitting, products, productsValue } = props;
return (
<form onSubmit={handleSubmit}>
<Field name="recipientName" type="text" component={renderField} label="Recipient Name"/>
<FieldArray name="competences" component={renderCompetences} />
{renderSelect(products)}
<div>
<button type="submit" disabled={submitting}>Submit</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>Clear Values</button>
</div>
</form>
)
}
const selector = formValueSelector('fieldArrays');
const mapStateToProps = (state) => {
const productsValue = selector(state, 'products');
return { productsValue };
};
export default compose(
connect(mapStateToProps),
reduxForm({
form: 'fieldArrays'
})
)(FieldArraysForm);
我应该在我的无序列表中显示我从选择中选择的产品,每次我选择一个新对象时,我都应该将它添加到列表中。
这里你可以看到 products 数组的一个例子:
const products = [
{ id: '0', name: 'Product One', singlePrice: 101, standardQuantity: 1},
{ id: '1', name: 'Product Two', singlePrice: 39, standardQuantity: 6},
{ id: '2', name: 'Product Three', singlePrice: 85, standardQuantity: 4},
{ id: '3', name: 'Product Four', singlePrice: 1, standardQuantity: 20}
];
这是一个测试一些解决方案的工作示例: https://www.webpackbin.com/bins/-Kecgj77Gbxo_4am3WS1
非常感谢您提供的任何帮助!
【问题讨论】:
-
你为什么不在任何地方使用
productsValue?我不明白该值的用途。 -
我提出它是因为我认为它可能有用,但经过几次尝试......我不知道它是否很有用
-
@ErikR。有这方面的消息吗?我的问题清楚了吗?任何帮助将不胜感激。再次感谢
-
Webpackbin 不适合我。 :-( 它运行,但不允许我选择其他文件或编辑现有文件。
标签: reactjs redux redux-form