reducer-form 数组字段 在removeField/removeField 之后 dirty 不改变的问题

reducer-form 数组字段 在removeField/removeField 之后 dirty 不改变

最近在用reducer-form 做项目时 如果字段类型是数组 , 在进行removeField/removeField 之后 this.props.dirty 死活不改变 于是google了一下 终于在github的issues上面找到了答案
原来这是reducer-form的一个bug 解决方案暂时只能用hack的方法做了

解决方法如下

添加一个字段touched来保存数组字段的dirty 并且在removeField/removeField 之后 调用 touched.onChange(true);

const LinkedProductsForm = reduxForm({
  form: formName,
  fields: [
    'id',
    'touched', // hack
    'product_ids[]'
  ]
}, undefined, {
  addProduct: () => addArrayValue(formName, 'product_ids')
})(Form);
  unlinkProduct(index) {
    const {fields: {touched, product_ids: productIds}} = this.props;
    return () => {
      productIds.removeField(index);
      touched.onChange(true);
    }
  }
  
<LinkedProduct
  key={`linked-product-${index}`}
  productId={productId}
  product={this.getProduct(productId)}
  unlink={this.unlinkProduct(index)}
  loadProducts={loadProducts} />
posted @ 2016-04-29 16:22  ElvinLong  阅读(320)  评论(0编辑  收藏  举报