Skip to content

Commit

Permalink
Refactor: phone-input component and use-phone-state hook
Browse files Browse the repository at this point in the history
  • Loading branch information
iPagar committed Feb 13, 2024
1 parent c7dc64a commit 3de54fb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/phone-input/phone-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,18 @@ export function PhoneInputRoot(
| React.ReactNode;
}
) {
const { children, onCountryChange, onPhoneChange, onValidationChange } =
props;
const state = usePhoneState();
const {
children,
initialCountry,
initialPhoneNumber,
onCountryChange,
onPhoneChange,
onValidationChange,
} = props;
const state = usePhoneState({
initialCountry,
initialPhoneNumber,
});
const [isOpen, setIsOpen] = React.useState(false);
const phoneProps = usePhone({ isDialogOpen: isOpen }, state);
const [onOpenChange, setOnOpenChange] = useState<(open: boolean) => void>();
Expand Down
17 changes: 14 additions & 3 deletions src/use-phone-state/use-phone-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AsYouType,
CountryCode,
getCountryCallingCode,
getPhoneCode,
isValidPhoneNumber,
parsePhoneNumberFromString,
} from 'libphonenumber-js';
Expand All @@ -27,10 +28,20 @@ export const phoneValidationSchema = (
}
}, invalid);

export function formatPhoneNumber(phoneNumber: string) {
export function formatPhoneNumber(phoneNumber: string, country?: string) {
const parsedNumber = parsePhoneNumberFromString(phoneNumber);
if (!parsedNumber) {
return phoneNumber;
try {
const phoneCode = getPhoneCode(country as CountryCode);

if (phoneCode) {
return `+${phoneCode}`;
}
} catch (error) {
return parsedNumber;
}

return parsedNumber;
}

return parsedNumber.formatInternational();
Expand All @@ -46,7 +57,7 @@ export const usePhoneState = ({
} = {}) => {
const [country, setCountry] = useState(initialCountry);
const [phoneNumber, setPhoneNumber] = useState(
formatPhoneNumber(initialPhoneNumber)
formatPhoneNumber(initialPhoneNumber, initialCountry)
);
const [isValid, setIsValid] = useState(
phoneValidationSchema().safeParse(initialPhoneNumber).success
Expand Down
1 change: 1 addition & 0 deletions stories/Documentation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const FormPhoneInput = forwardRef(
return (
<PhoneInput.Root
className={styles.formPhoneInput}
initialCountry={country}
onCountryChange={(newCountry) => {
setCountry(newCountry);
}}
Expand Down
10 changes: 8 additions & 2 deletions stories/phone-input.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ const FormPhoneInput = forwardRef(
return (
<PhoneInput.Root
className={styles.formPhoneInput}
initialCountry={country}
onCountryChange={(newCountry) => {
setCountry(newCountry);
}}
>
{({ countryList }) => (
{({ countryList, phone }) => (
<>
<PhoneInput.Trigger>
<CountryFlag
Expand Down Expand Up @@ -82,7 +83,12 @@ const FormPhoneInput = forwardRef(
</ul>
</>
</PhoneInput.Dialog>
<PhoneInput.NumberInput {...props} onChange={onChange} ref={ref} />
<PhoneInput.NumberInput
{...props}
onChange={onChange}
ref={ref}
value={phone}
/>
</>
)}
</PhoneInput.Root>
Expand Down

0 comments on commit 3de54fb

Please sign in to comment.