Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm using Angular version 17 and the plugin works fine on laptop / desktop, but then on iphone it causes a white screen #173

Open
tc-mark opened this issue Mar 1, 2024 · 1 comment

Comments

@tc-mark
Copy link

tc-mark commented Mar 1, 2024

Is this plugin compatible with Angular 17 for mobile?

Its strange how it works on desktop

@terence1990
Copy link

terence1990 commented Sep 7, 2024

I fixed it this way:

const getCountries = async () => {
  const Country = await import('country-state-city/lib/country');
  const {getAllCountries} = Country.default;
  return getAllCountries().sort((a, b) => a.name.localeCompare(b.name));
};

const getCities = async (country: string, state?: string) => {
  const City = await import('country-state-city/lib/city');
  const {getCitiesOfState, getCitiesOfCountry} = City.default;
  return state ? getCitiesOfState(country, state) : getCitiesOfCountry(country) ?? [];
}

Since it's now async, becomes troublesome as need to run a side affect in react, so used a hook:

import { ICity, ICountry, IState } from 'country-state-city/lib/interface';

export const useCountriesAndCities = (props?: {
  country?: string,
  state?: string
}) => {
  const [countries, setCountries] = useState<ICountry[]>([]);
  const [cities, setCities] = useState<ICity[]>([]);

  useEffect(() => {
    getCountries().then(setCountries);
  }, []);

  useEffect(() => {
    if (props?.country && props?.state) {
      getCities(props.country, props.state).then(setCities);
    }
  }, [props?.country, props?.state]);

  const phoneCodes = useMemo(() => countries
    .map((country) => ({
      value: country.phonecode.startsWith('+')
        ? country.phonecode
        : `+${country.phonecode}`,
      label: `${country.flag} ${
        country.phonecode.startsWith('+')
          ? country.phonecode
          : `+${country.phonecode}`
      }`,
    }))
    .filter((a, i, arr) => arr.findIndex((b) => b.value === a.value) === i), [countries]);

  return {
    countries,
    cities,
    phoneCodes
  };

};

Related:
#184
#123

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants