【问题标题】:Is there a way to normalize values of a ant design input on submit?有没有办法在提交时标准化 ant 设计输入的值?
【发布时间】:2020-07-11 05:38:43
【问题描述】:

我正在使用带有 React JS 的 Ant Design,并且我使用 Form.Item normalize prop 来解析来自 api 的值以进行输入。

我现在想做相反的事情,在提交时从输入规范化为 api 格式。

例如:我有一个名为现金的输入,它接收浮点数并标准化为货币,但在提交表单上我想再次接收浮点值,有可能吗?我在 github 问题中搜索,但没有发现任何相关内容。

<Form onSubmit={values => send(values)}> // I'd like retrieve values.cash in float without parse directly every time
  <Input normalize={normalize_to_currency} name="cash" />
</Form>

PS:我用的是非受控蚂蚁设计形式。

【问题讨论】:

    标签: reactjs antd


    【解决方案1】:

    您可以使用 Form.Item 规范化道具

    <Form onFinish={values => console.log(values)}>
        <Form.Item name="currecny" normalize={e => parseFloat(e)}>
          <Input placeholder="Currency" type="number" />
        </Form.Item>
        <Button htmlType="submit">Submit</Button>
    </Form>
    

    你可以检查这个沙盒https://codesandbox.io/s/cranky-resonance-mtkxi

    如果您在 getFieldDecoder 中使用 Ant Design 3.x,您将获得 normalize props

    【讨论】:

    • 感谢您的回复,但我已经在使用 normalize。在您的示例中,您没有在输入上应用货币掩码。我需要用掩码显示值,不带掩码发送。
    • 哇,谢谢。你的回答太棒了。我发现这个需要 2-3 个小时。
    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多