Skip to content

Commit

Permalink
fix: fix usePhoneInput national format
Browse files Browse the repository at this point in the history
  • Loading branch information
Thinh Trinh committed Mar 21, 2024
1 parent ee5c050 commit aba150d
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"clean": "rimraf .turbo && rimraf node_modules && rimraf .next"
},
"dependencies": {
"@react-awesome/components": "1.0.18",
"@react-awesome/components": "1.0.19",
"classnames": "^2.5.1",
"lodash": "^4.17.21",
"lucide-react": "^0.315.0",
Expand Down
3 changes: 2 additions & 1 deletion apps/docs/src/pages/docs/phone-input/use-phone-input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export const LocalExample = () => {
setValue(m);
},
mode: 'national',
defaultCountry: 'VN'
defaultCountry: 'VN',
value: value.formattedValue
});

return (
Expand Down
7 changes: 7 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @react-awesome/components

## 1.0.19

### Patch Changes

- Updated dependencies
- @react-awesome/phone-input@1.1.6

## 1.0.18

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-awesome/components",
"version": "1.0.18",
"version": "1.0.19",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down Expand Up @@ -39,7 +39,7 @@
"access": "public"
},
"dependencies": {
"@react-awesome/phone-input": "1.1.5",
"@react-awesome/phone-input": "1.1.6",
"@react-awesome/use-click-outside": "0.0.3",
"@react-awesome/use-preserve-input-caret-position": "0.0.3",
"@react-awesome/use-selection-range": "0.0.3",
Expand Down
6 changes: 6 additions & 0 deletions packages/phone-input/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @react-awesome/phone-input

## 1.1.6

### Patch Changes

- Fix usePhoneInput national format doesn't format the value.

## 1.1.5

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/phone-input/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-awesome/phone-input",
"version": "1.1.5",
"version": "1.1.6",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
6 changes: 3 additions & 3 deletions packages/phone-input/src/hooks/usePhoneInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ describe('usePhoneInput', () => {
})

// Should be treated as VN number +84 123 since country detector has been disabled.
expect(input.getAttribute('value')).toBe('123')
expect(input.getAttribute('value')).toBe('1 23')

expect(container.querySelector('#VN')).toBeVisible()
})
Expand All @@ -268,7 +268,7 @@ describe('usePhoneInput', () => {
await user.keyboard('{+},{1},{2},{3}')
})

expect(input.getAttribute('value')).toBe('123')
expect(input.getAttribute('value')).toBe('1 23')

expect(container.querySelector('#VN')).toBeVisible()
})
Expand Down Expand Up @@ -347,7 +347,7 @@ describe('usePhoneInput', () => {
await user.keyboard('{+},{1},{2},{3}')
})

expect(input.getAttribute('value')).toBe('123')
expect(input.getAttribute('value')).toBe('1 23')

expect(container.querySelector('#VN')).toBeVisible()
})
Expand Down
20 changes: 16 additions & 4 deletions packages/phone-input/src/hooks/usePhoneInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,20 @@ export const usePhoneInput = ({
asYouType.current.reset()
asYouType.current.input(value)

const formattedValue = formatIncompletePhoneNumber(value, country)
const phoneCode =
asYouType.current.getCallingCode() || getCountryCallingCode(country)
let formattedValue = formatIncompletePhoneNumber(value, country)

/**
* Since input value in national format won't include the country code so we have to manually append it.
*/
if (mode === 'national') {
formattedValue = formatIncompletePhoneNumber(
`${phoneCode}${value}`,
country,
)
formattedValue = formattedValue.replace(phoneCode, '').trim()
}

return {
isPossible: asYouType.current.isPossible(),
Expand All @@ -214,13 +227,12 @@ export const usePhoneInput = ({
fromCountry: country,
}) || '',
country,
phoneCode:
asYouType.current.getCallingCode() || getCountryCallingCode(country),
phoneCode,
formattedValue,
isSupported,
}
},
[defaultCountry, guessCountry, options, supportedCountries],
[defaultCountry, guessCountry, mode, options, supportedCountries],
)
const setSelectedCountry = React.useCallback(
(country: CountryCode) => {
Expand Down

0 comments on commit aba150d

Please sign in to comment.