Skip to content

Commit

Permalink
Implement compatibility with SSR (GH-30)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtyomVancyan authored May 18, 2024
2 parents 52413ac + 58accc0 commit 2e7aad2
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 6 deletions.
12 changes: 6 additions & 6 deletions development/src/phone-hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import {ChangeEvent, KeyboardEvent, useCallback, useMemo, useRef, useState} from "react";

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

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions development/src/phone-hooks/styles.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

export const jsonToCss = (stylesheet: object) => {
/** Convert the given `stylesheet` object to raw CSS */
return Object.entries(stylesheet).map(([selector, rules]) => {
Expand Down
2 changes: 2 additions & 0 deletions development/src/phone-hooks/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

export interface PhoneNumber {
countryCode?: number | null;
areaCode?: string | null;
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import {ChangeEvent, KeyboardEvent, useCallback, useMemo, useRef, useState} from "react";

import {PhoneNumber, usePhoneOptions} from "./types";
Expand Down
2 changes: 2 additions & 0 deletions src/styles.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

export const jsonToCss = (stylesheet: object) => {
/** Convert the given `stylesheet` object to raw CSS */
return Object.entries(stylesheet).map(([selector, rules]) => {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

export interface PhoneNumber {
countryCode?: number | null;
areaCode?: string | null;
Expand Down
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"noImplicitAny": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitUseStrict": true,
"strictNullChecks": true,
"module": "esnext",
"moduleResolution": "node",
"ignoreDeprecations": "5.0",
"resolveJsonModule": true,
"outDir": ".",
"esModuleInterop": true,
Expand Down

0 comments on commit 2e7aad2

Please sign in to comment.