react-phone-input-2 icon indicating copy to clipboard operation
react-phone-input-2 copied to clipboard

There is no way to fully clear the input

Open Volekss opened this issue 6 years ago • 5 comments

I made the phone number field optional in my project. But is there a way to fully clear the input after starting entering the phone number? Because I can't remove the country code if it was already selected.

Volekss avatar Feb 12 '20 11:02 Volekss

You can remove the country code, but you can't remove the prefix. The functionality to hide prefix (default behavior without alternative option) if input is clear would be good to see. But to change default behavior of guessed country flag that is attached to empty input is not desirable. You may want to add the possibility to pass -1 as the country value, and if the condition is met (digits not present in the input value), clear country flag by passing -1 as the country value in the callback.

bl00mber avatar Feb 25 '20 13:02 bl00mber

Same issue, in my project phone field is optional as well but the inputs field is having a default value + as a prefix. Still looking for a way to clear the input. image

azmatrana avatar Mar 02 '20 12:03 azmatrana

If you pass null as value, the country will be cleared. If you pass empty string, country will not be cleared.

bl00mber avatar Apr 14 '20 12:04 bl00mber

Same issue, in my project phone field is optional as well but the inputs field is having a default value + as a prefix. Still looking for a way to clear the input. image

@azmatrana Were you able to find any solution to this? I do Command + a and then Backspace, it leaves a +. It's not clearing the entire input.

praneethEnphase avatar Apr 20 '23 18:04 praneethEnphase

@praneethEnphase I tried the ff and it clears the plus sign in the input:

onChange={(val, data, e, fmtVal) => {
  onChange(fmtVal === '' || fmtVal === '+' ? null : fmtVal);
}}

The inner onChange function is just a passed handler. The outer onChange function is the one needed for this library.

leejanmorado avatar Dec 06 '23 02:12 leejanmorado