【问题标题】:Conditionally format value in React-Final-Form based on data in form?根据表单中的数据有条件地格式化 React-Final-Form 中的值?
【发布时间】:2018-05-10 14:37:51
【问题描述】:

我正在以 react-final-form 显示货币值。我在现场使用格式功能。

const formatMoney = (value) => {
  if (!value) return value;
  const formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    minimumFractionDigits: 2,
    currency: 'USD'
  });
  return formatter.format(value);
};

问题是货币是表单中的另一个选项。我没有看到一种干净的方法来访问格式函数中的其余值。

【问题讨论】:

    标签: react-final-form final-form


    【解决方案1】:

    在提出问题后,我想出了这个解决方法,将值传递给表单的渲染函数,然后传递给格式函数,如下所示:

         <Field
          name="total"
          type="text"
          readOnly
          component={LabeledInput}
          placeholder="10"
          label="Total"
          help="Estimated total including tax"
          format={v => formatMoney(v, values.currency)}
        />
    

    【讨论】:

    • 您好。我也有同样的问题。你能分享一个codeandbox的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 2013-11-29
    • 2022-11-07
    • 2014-04-27
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多