-
Notifications
You must be signed in to change notification settings - Fork 44
fix(intl-phone-input): remove changing unclearable country code #730
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3f2270a
fix(intl-phone-input): remove changing unclearable country code
koretskyhub b2211c3
fix(intl-phone-input): add logic to getPhoneDiff
koretskyhub 6684d52
Merge branch 'master' of github.com:alfa-laboratory/core-components i…
koretskyhub 1588bc2
fix(intl-phone-input): permit caret on unchangable countryCode
koretskyhub f89cff0
fix(intl-phone-input): change event to handle caret moving
koretskyhub 90a4f41
fix(intl-phone-input): optimize listeners, fix caret animation
koretskyhub dd22987
Merge branch 'master' into fix/intl-phone-input
reme3d2y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 8 additions & 19 deletions
27
packages/intl-phone-input/src/utils/format-phone-with-unclearable-country-code.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,21 @@ | ||
import { Country } from '@alfalab/utils'; | ||
import { getPhoneDiff } from './get-phone-diff'; | ||
|
||
const RUSSIAN_DIAL_CODE = '7'; | ||
const RUSSIAN_NATIONAL_DIAL_CODE = '8'; | ||
|
||
/** | ||
* Форматирует телефон с неудаляемым кодом страны | ||
*/ | ||
export const formatPhoneWithUnclearableCountryCode = (phone: string, country: Country) => { | ||
const defaultValue = `+${country.dialCode}`; | ||
// При попытке стереть код страны возвращаем дефолтное значение | ||
if (phone.length < defaultValue.length) { | ||
return defaultValue; | ||
} | ||
const countryPrefix = `+${country.dialCode}`; | ||
|
||
// Если код страны совпадает, даем вводить значение | ||
if (phone.substr(1, country.dialCode.length) === country.dialCode) { | ||
if (phone.startsWith(countryPrefix)) { | ||
return phone; | ||
} | ||
|
||
const lengthDiff = phone.substr(1).length - country.dialCode.length; | ||
// Если разница длины нового значения и длины кода страны равна 1, то определяем отличающийся символ и ставим его после кода | ||
if (lengthDiff === 1) { | ||
const diff = getPhoneDiff(phone, country); | ||
// Если не смогли вычислить отличающийся символ, то возвращаем дефолтное значение | ||
if (!diff) { | ||
return defaultValue; | ||
} | ||
|
||
return `+${country.dialCode}${diff}`; | ||
if (country.dialCode === RUSSIAN_DIAL_CODE && phone.startsWith(RUSSIAN_NATIONAL_DIAL_CODE)) { | ||
return phone.replace(RUSSIAN_NATIONAL_DIAL_CODE, countryPrefix); | ||
} | ||
|
||
return phone; | ||
return countryPrefix; | ||
}; |
30 changes: 0 additions & 30 deletions
30
packages/intl-phone-input/src/utils/get-phone-diff.test.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
packages/intl-phone-input/src/utils/permit-caret-on-country-code.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function permitCaretOnCountryCode(input: HTMLInputElement, countryCodeLength: number) { | ||
function resetCaret() { | ||
const selectionStart = input.selectionStart || 0; | ||
|
||
if (selectionStart < countryCodeLength) { | ||
input.focus(); | ||
input.setSelectionRange(countryCodeLength, countryCodeLength); | ||
} | ||
} | ||
|
||
input.addEventListener('keyup', resetCaret); | ||
input.addEventListener('click', resetCaret); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Обработчики тогда ведь при каждом ререндере будут создаваться?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
да похоже