Skip to content

Commit 2e7aad2

Browse files
Implement compatibility with SSR (GH-30)
2 parents 52413ac + 58accc0 commit 2e7aad2

File tree

7 files changed

+18
-6
lines changed

7 files changed

+18
-6
lines changed

development/src/phone-hooks/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
import {ChangeEvent, KeyboardEvent, useCallback, useMemo, useRef, useState} from "react";
24

35
import {PhoneNumber, usePhoneOptions} from "./types";
@@ -10,11 +12,8 @@ const slots = new Set(".");
1012

1113
export const getMetadata = (rawValue: string, countriesList: typeof countries = countries, country: any = null) => {
1214
country = country == null && rawValue.startsWith("44") ? "gb" : country;
13-
if (country != null) {
14-
countriesList = countriesList.filter((c) => c[0] === country);
15-
countriesList = countriesList.sort((a, b) => b[2].length - a[2].length);
16-
}
17-
return countriesList.find((c) => rawValue.startsWith(c[2]));
15+
if (country != null) countriesList = countriesList.filter((c) => c[0] === country);
16+
return [...countriesList].sort((a, b) => b[2].length - a[2].length).find((c) => rawValue.startsWith(c[2]));
1817
}
1918

2019
export const getCountry = (countryCode: keyof typeof countries) => {
@@ -36,8 +35,9 @@ export const cleanInput = (input: any, pattern: string) => {
3635
return Array.from(pattern, c => input[0] === c || slots.has(c) ? input.shift() || c : c);
3736
}
3837

39-
export const getFormattedNumber = (rawValue: any, pattern: string) => {
38+
export const getFormattedNumber = (rawValue: any, pattern?: string) => {
4039
/** Returns the reformatted input value based on the given pattern */
40+
pattern = pattern || getMetadata(rawValue)?.[3] || "";
4141
return displayFormat(cleanInput(rawValue, pattern.replaceAll(/\d/g, ".")).join(""));
4242
}
4343

development/src/phone-hooks/styles.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
export const jsonToCss = (stylesheet: object) => {
24
/** Convert the given `stylesheet` object to raw CSS */
35
return Object.entries(stylesheet).map(([selector, rules]) => {

development/src/phone-hooks/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
export interface PhoneNumber {
24
countryCode?: number | null;
35
areaCode?: string | null;

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
import {ChangeEvent, KeyboardEvent, useCallback, useMemo, useRef, useState} from "react";
24

35
import {PhoneNumber, usePhoneOptions} from "./types";

src/styles.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
export const jsonToCss = (stylesheet: object) => {
24
/** Convert the given `stylesheet` object to raw CSS */
35
return Object.entries(stylesheet).map(([selector, rules]) => {

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"use client";
2+
13
export interface PhoneNumber {
24
countryCode?: number | null;
35
areaCode?: string | null;

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
"noImplicitAny": true,
44
"noUnusedLocals": true,
55
"noUnusedParameters": true,
6+
"noImplicitUseStrict": true,
67
"strictNullChecks": true,
78
"module": "esnext",
89
"moduleResolution": "node",
10+
"ignoreDeprecations": "5.0",
911
"resolveJsonModule": true,
1012
"outDir": ".",
1113
"esModuleInterop": true,

0 commit comments

Comments
 (0)