【问题标题】:Calculate a field value using other 2 fields values using onChange使用 onChange 使用其他 2 个字段值计算字段值
【发布时间】:2017-03-28 14:32:49
【问题描述】:

场景:

考虑 3 个字段:quantity、singlePrice、totalPrice。 我需要它们都是我表单中的字段,但是每次我通过您可以想象的简单操作更改数量或 singlePrice 时都必须重新计算 totalPrice..

我做了什么:

  • 创建了一个由数量字段的 onChange 事件触发的函数和另一个用于 singlePrice 字段的函数。

  • 上面的函数调用了一个带有如下负载的 redux 操作:

    { name: name_of_the_updated_field, value: new_field_value }

  • 该操作由 formReducer plugin 进行计算并返回更新后的值对象。

问题: 我的 redux 商店没有更新,我的表单也没有更新。

我的表单缩减器(我必须更新的属性在另一个属性中)。

import { reducer as formReducer } from 'redux-form';

export default formReducer.plugin({
  formName: (state, action) => {
    switch (action.type) {
      case 'CALC_TOTAL_PRICE':
        return {
          ...state,
          values: {
            ...state.values,
            competences: state.values.competences.map((c, i) => {
              if (i === action.payload.index) {
                const { name, value } = action.payload;
                const next = Object.assign({}, c);
                next[name] = value;
                next.totalPrice = next.quantity * next.singlePrice
                return next;
              }
              return c;
            }),
          },
        };
      default:
        return state;
    }
  },
});

我错过了什么吗?如何解决这个问题? 有没有更简单的方法来得到这个结果?

非常感谢任何帮助!

【问题讨论】:

    标签: javascript forms reactjs redux-form


    【解决方案1】:

    您可能会考虑使用formValueSelector 来获取当前输入值并计算总和之类的东西-

    import React, { PropTypes } from 'react';
    import { Field, reduxForm, formValueSelector, FieldArray, SubmissionError } from 'redux-form';
    import { connect } from 'react-redux';
    
    class NewSaleForm extends React.Component {
    
       componentWillReceiveProps(nextProps) {
          if (nextProps.items && nextProps.items.length > 0) {
             nextProps.items.forEach((item) => {
                if (item.price && item.unit) {
                   if (item.unit_discount) {
                      item.total = (item.price - item.unit_discount) * item.unit;
                   } else {
                      item.total = item.price * item.unit;
                   }
                } else {
                   item.total = '';
                }
             });
          }
       }
    
    render() {
       ......
    }
    
    
    NewSaleForm = reduxForm({ // eslint-disable-line
       form: 'newSaleForm'
    })(NewSaleForm);
    
    const selector = formValueSelector('newSaleForm');
    
    NewSaleForm = connect(state => { // eslint-disable-line
       const items = selector(state, 'items');
       return {
          items,
       };
    })(NewSaleForm);
    
    export default NewSaleForm;
    

    【讨论】:

    • Fazal Rasel,感谢您的回复!我明白你的意思,但也许我的解释不清楚:我应该以某种方式将数据注入到 FieldArray 的 fields 属性中,因为在我从一个选择。这是一个“工作”示例..我可以请你建议我如何解决它吗? webpackbin.com/bins/-Kecgj77Gbxo_4am3WS1 当您从选择中选择产品时,它应该将数据提供给字段,就像 FIeldsArray 示例一样
    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2019-03-15
    • 2018-10-06
    • 2020-06-18
    • 2016-08-02
    • 1970-01-01
    • 2020-11-02
    相关资源
    最近更新 更多