Enhance CountryFlag to accept country names and codes
Build And Push Image / docker (push) Successful in 3m20s
Details
Build And Push Image / docker (push) Successful in 3m20s
Details
- Add actualCountryCode computed property to handle both 2-letter codes and country names - Extend parseCountryInput with common country name variations (USA, UK, etc.) - Import parseCountryInput utility in CountryFlag component - Update template to use actualCountryCode instead of direct countryCode prop
This commit is contained in:
parent
b043648db6
commit
f096a22824
|
|
@ -2,24 +2,24 @@
|
||||||
<span class="country-flag" :class="{ 'country-flag--small': size === 'small' }">
|
<span class="country-flag" :class="{ 'country-flag--small': size === 'small' }">
|
||||||
<ClientOnly>
|
<ClientOnly>
|
||||||
<VueCountryFlag
|
<VueCountryFlag
|
||||||
v-if="countryCode"
|
v-if="actualCountryCode"
|
||||||
:country="countryCode"
|
:country="actualCountryCode"
|
||||||
:size="flagSize"
|
:size="flagSize"
|
||||||
:title="getCountryName(countryCode)"
|
:title="getCountryName(actualCountryCode)"
|
||||||
/>
|
/>
|
||||||
<template #fallback>
|
<template #fallback>
|
||||||
<span class="flag-placeholder" :style="placeholderStyle">🏳️</span>
|
<span class="flag-placeholder" :style="placeholderStyle">🏳️</span>
|
||||||
</template>
|
</template>
|
||||||
</ClientOnly>
|
</ClientOnly>
|
||||||
<span v-if="showName && countryCode" class="country-name">
|
<span v-if="showName && actualCountryCode" class="country-name">
|
||||||
{{ getCountryName(countryCode) }}
|
{{ getCountryName(actualCountryCode) }}
|
||||||
</span>
|
</span>
|
||||||
</span>
|
</span>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import VueCountryFlag from 'vue-country-flag-next';
|
import VueCountryFlag from 'vue-country-flag-next';
|
||||||
import { getCountryName } from '~/utils/countries';
|
import { getCountryName, parseCountryInput } from '~/utils/countries';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
countryCode?: string;
|
countryCode?: string;
|
||||||
|
|
@ -35,6 +35,20 @@ const props = withDefaults(defineProps<Props>(), {
|
||||||
square: false
|
square: false
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Convert country name to country code if needed
|
||||||
|
const actualCountryCode = computed(() => {
|
||||||
|
if (!props.countryCode) return '';
|
||||||
|
|
||||||
|
// If it's already a 2-letter code, use it
|
||||||
|
if (props.countryCode.length === 2) {
|
||||||
|
return props.countryCode.toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try to parse country name to get the code
|
||||||
|
const parsed = parseCountryInput(props.countryCode);
|
||||||
|
return parsed || '';
|
||||||
|
});
|
||||||
|
|
||||||
const flagSize = computed(() => {
|
const flagSize = computed(() => {
|
||||||
const sizeMap = {
|
const sizeMap = {
|
||||||
small: 'sm',
|
small: 'sm',
|
||||||
|
|
|
||||||
|
|
@ -321,8 +321,30 @@ export const parseCountryInput = (input: string): string | null => {
|
||||||
return trimmed.toUpperCase();
|
return trimmed.toUpperCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle common variations and abbreviations
|
||||||
|
const commonVariations: Record<string, string> = {
|
||||||
|
'usa': 'US',
|
||||||
|
'uk': 'GB',
|
||||||
|
'england': 'GB',
|
||||||
|
'britain': 'GB',
|
||||||
|
'great britain': 'GB',
|
||||||
|
'america': 'US',
|
||||||
|
'united states of america': 'US',
|
||||||
|
'south korea': 'KR',
|
||||||
|
'north korea': 'KP',
|
||||||
|
'czech republic': 'CZ',
|
||||||
|
'czechia': 'CZ',
|
||||||
|
'russia': 'RU',
|
||||||
|
'russian federation': 'RU',
|
||||||
|
};
|
||||||
|
|
||||||
|
const lowerInput = trimmed.toLowerCase();
|
||||||
|
if (commonVariations[lowerInput]) {
|
||||||
|
return commonVariations[lowerInput];
|
||||||
|
}
|
||||||
|
|
||||||
// Search by name
|
// Search by name
|
||||||
const found = countriesByName.get(trimmed.toLowerCase());
|
const found = countriesByName.get(lowerInput);
|
||||||
return found ? found.code : null;
|
return found ? found.code : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue