necessarylion / country-list-with-dial-code-and-flag

Country Flag with Dial Number Code
46 stars 18 forks source link

Random flags #30

Closed Raghavj2000 closed 2 months ago

Raghavj2000 commented 1 year ago

When i search for a flag still random flag appears Screenshot 2023-06-28 145619

necessarylion commented 1 year ago

Can you provide the code you used to search?

Raghavj2000 commented 1 year ago

const selectCountry = (country) => { setSelectedCountry(country); setIsOpen(false); setPhoneNumber(country.dialCode); };

const renderCountryFlags = () => { const filteredCountries = countries.filter((country) => country.name.toLowerCase().includes(searchQuery.toLowerCase()) );

const sortedCountries = filteredCountries.sort((a, b) => {
  const aIndex = a.name.toLowerCase().indexOf(searchQuery.toLowerCase());
  const bIndex = b.name.toLowerCase().indexOf(searchQuery.toLowerCase());

  // Sort by the index of the search query in the country name
  if (aIndex < bIndex) return -1;
  if (aIndex > bIndex) return 1;
  // If the index is the same, sort by the length of the country name
  if (a.name.length < b.name.length) return -1;
  if (a.name.length > b.name.length) return 1;
  return 0;
});

const findByKeyword = (keyword) => {
  return sortedCountries.filter(
    (country) =>
      country.name.toLowerCase().includes(keyword.toLowerCase()) ||
      country.code.toLowerCase().includes(keyword.toLowerCase())
  );
};

const foundCountries = findByKeyword(searchQuery);

return foundCountries.map((country) => {
  const flagSvg = CountryFlagSvg[country.code];
  const flagString = flagSvg && flagSvg.toString();
Raghavj2000 commented 1 year ago

the problem is if i search India, all country that has letter I or letter N or A appears.

necessarylion commented 1 year ago
import CountryList from 'country-list-with-dial-code-and-flag'

CountryList.findByKeyword('india')