简体   繁体   English

如何使用 antd-mask-input 库获取 antd 表单字段的原始值?

[英]How to get the raw value of an antd form field using antd-mask-input library?

I am using antd-mask-input library on my Ant Design app to put mask into a field on my antd form .我在 Ant Design 应用程序上使用antd-mask-input库将掩码放入我的antd 表单上的字段中。 This antd-mask-input library was built using this inputmask-core library.这个 antd-mask-input 库是使用这个inputmask-core库构建的。

Here is an working example on codesandbox.io.这是codesandbox.io上的一个工作示例 Fill in any value on both fields and open console.在两个字段中填写任何值并打开控制台。 Once you click on Confirm the value of the fields will be logged on console output.单击“ Confirm ”后,这些字段的值将记录在控制台输出中。

As you can see, the phone const have the value of the field with the mask, but I need it without the mask.如您所见, phone常量具有带掩码的字段值,但我需要不带掩码的值。 There is a getRawValue() method on inputmask-core but I can't figure out how to use it with the MaskedInput component provided by antd-mask-input library. inputmask-core 上有一个getRawValue()方法,但我不知道如何将它与antd-mask-input库提供的MaskedInput组件一起使用。

import React, { Component } from "react";
import { Form, Icon, Input, Button } from "antd";
import { MaskedInput } from "antd-mask-input";

const INPUT_ICON_COLOR = "rgba(0,0,0,.25)";

const FormFields = Object.freeze({
  EMAIL: "email",
  PHONE: "phone"
});

class Signup extends Component {
  handleSubmit = e => {
    const { form } = this.props;
    const { validateFields } = form;

    e.preventDefault();

    validateFields((err, values) => {
      if (!err) {
        const a = form.getFieldValue(FormFields.PHONE);
        debugger;

        const phone = values[FormFields.PHONE];
        const email = values[FormFields.EMAIL];

        console.log("phone", phone);
        console.log("email", email);
      }
    });
  };

  render() {
    const { form } = this.props;
    const { getFieldDecorator } = form;

    return (
      <div
        style={{
          display: "flex",
          alignItems: "center",
          width: "100%",
          flexDirection: "column"
        }}
      >
        <Form onSubmit={this.handleSubmit} className={"login-form"}>
          <Form.Item>
            {getFieldDecorator(FormFields.PHONE, {
              rules: [
                {
                  required: true,
                  message: "Please type value"
                }
              ]
            })(
              <MaskedInput
                mask="(11) 1 1111-1111"
                placeholderChar={" "}
                prefix={
                  <Icon type="phone" style={{ color: INPUT_ICON_COLOR }} />
                }
                placeholder="Phone"
              />
            )}
          </Form.Item>
          <Form.Item>
            {getFieldDecorator(FormFields.EMAIL, {
              rules: [{ required: true, message: "Please type value" }]
            })(
              <Input
                type={"email"}
                prefix={
                  <Icon type="mail" style={{ color: INPUT_ICON_COLOR }} />
                }
                placeholder="Email"
              />
            )}
          </Form.Item>
          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              className={"login-form-button"}
            >
              Confirm
            </Button>
          </Form.Item>
        </Form>
      </div>
    );
  }
}

export default Form.create({ name: "signup" })(Signup);

编辑粗糙的tdd-b48pk

Looking on the source code :查看源代码

export default class MaskedInput extends Component<MaskedInputProps> {
  mask: InputMask;
  input!: HTMLInputElement;
....
}

The InputMask reference is under the mask value, therefore you can get the function like so: InputMask引用位于mask值之下,因此您可以获得如下函数:

this.inputMaskRef.current.mask.getRawValue()

Example:例子:

class Signup extends Component {
  inputMaskRef = React.createRef();

  handleSubmit = e => {
    ...
    validateFields((err, values) => {
      if (!err) {
        ...
        console.log(this.inputMaskRef.current.mask.getRawValue());
      }
    });
  };

  render() {
    ...
    return (
      <Form ...>
        <Form.Item>
          {getFieldDecorator(FormFields.PHONE, {...})(
            <MaskedInput
              ref={this.inputMaskRef}
              mask="(11) 1 1111-1111"
              ...
            />,
          )}
        </Form.Item>
        ...
      </Form>
    );
  }
}

编辑滚滚空地-q024p

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM