admin管理员组文章数量:1336075
I have an AntD form with an Input field for phone numbers, and another to capture SSN's. As a user types a phone number or SSN, I would like the fields to format accordingly.
For example, instead of the phone number being 1234567890 as typed into the field, it should format as (123) 456-7890 with it initially being (___) ___-____
. Similarly, in the SSN field if a user types their SSN such as 123456789 it should format as 123-45-6789 with it initially being ###-##-####
.
What I'm really looking for is the proper way to do this in AntD. So not necessarily fixing the approach I'm trying to get to work.
I've been trying a variety of approaches to solve this, but none ever seem to work for me. For example, I've tried:
- Use onChange and create a normalizeInput function as outlined here.
- After thinking to myself that I shouldn't need to reinvent the wheel and there must be an npm package that I can simply leverage, I tried installing and using react-number-format. It was formatting the field and contents the way that I wanted but I lost the AntD UI (the input field no longer looked like an AntD input field).
- Tried playing with a Form's getFieldsValue and setFieldsValue tags but got nowhere fast. I probably just didn't know what I was doing.
- Tried using AntD's InputNumber function instead once i saw it had a formatter field that'd do the trick. But I abandoned that approach given I didn't want the up and down arrow controls that e with an InputNumber widget.
Currently #1 sort of works but not quite and I'm not sure why. It uses onChange to read in the current value, and adjust it accordingly, but there are two problems..
- previousValue bees undefined on the 6th, 8th, and 10th digit. Why? You'll see it in console.log in my codesandbox below.
- The UI's field never sets using the value tag. Why? I can see the value is changing via console.log, but would've thought the setState function would've set the state within onChange, and therefore the field would show the formatted version due to the value tag..?
Here is my codesandbox illustrating the problem (MyForm.js file).
I have an AntD form with an Input field for phone numbers, and another to capture SSN's. As a user types a phone number or SSN, I would like the fields to format accordingly.
For example, instead of the phone number being 1234567890 as typed into the field, it should format as (123) 456-7890 with it initially being (___) ___-____
. Similarly, in the SSN field if a user types their SSN such as 123456789 it should format as 123-45-6789 with it initially being ###-##-####
.
What I'm really looking for is the proper way to do this in AntD. So not necessarily fixing the approach I'm trying to get to work.
I've been trying a variety of approaches to solve this, but none ever seem to work for me. For example, I've tried:
- Use onChange and create a normalizeInput function as outlined here.
- After thinking to myself that I shouldn't need to reinvent the wheel and there must be an npm package that I can simply leverage, I tried installing and using react-number-format. It was formatting the field and contents the way that I wanted but I lost the AntD UI (the input field no longer looked like an AntD input field).
- Tried playing with a Form's getFieldsValue and setFieldsValue tags but got nowhere fast. I probably just didn't know what I was doing.
- Tried using AntD's InputNumber function instead once i saw it had a formatter field that'd do the trick. But I abandoned that approach given I didn't want the up and down arrow controls that e with an InputNumber widget.
Currently #1 sort of works but not quite and I'm not sure why. It uses onChange to read in the current value, and adjust it accordingly, but there are two problems..
- previousValue bees undefined on the 6th, 8th, and 10th digit. Why? You'll see it in console.log in my codesandbox below.
- The UI's field never sets using the value tag. Why? I can see the value is changing via console.log, but would've thought the setState function would've set the state within onChange, and therefore the field would show the formatted version due to the value tag..?
Here is my codesandbox illustrating the problem (MyForm.js file).
Share Improve this question edited Dec 31, 2020 at 23:23 Gerald Wichmann asked Dec 31, 2020 at 22:37 Gerald WichmannGerald Wichmann 4572 gold badges9 silver badges25 bronze badges1 Answer
Reset to default 4check this https://github./antoniopresto/antd-mask-input
is a library to make it. The ponent is
<MaskedInput mask="11/1111" name="expiry" placeholder="mm/yyyy" onChange={this._onChange}/>
where you give the prop "mask", where you can make the format.
in your case it would be
<MaskedInput mask="(111) 111-1111" name="expiry" placeholder="mm/yyyy" onChange={this._onChange}/>
版权声明:本文标题:javascript - How do I format AntD form input fields such as phone numbers and SSNs in React? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742353324a2458924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论