mirror of
https://github.com/Pandipipas/scoreko-dev.git
synced 2026-06-06 11:42:06 +00:00
547f9ab95f
* Add country dropdown and flags * Fix build for country flags * Improve country select filtering and scoreboard teams * Fix country select display value * Fix country select input display
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { getData, type CountryRecord } from 'country-list';
|
|
|
|
export interface CountryOption {
|
|
value: string;
|
|
label: string;
|
|
}
|
|
|
|
const baseCountries = getData();
|
|
|
|
export const countryOptions: CountryOption[] = baseCountries
|
|
.map((country: CountryRecord) => ({
|
|
value: country.code,
|
|
label: country.name,
|
|
}))
|
|
.sort((a: CountryOption, b: CountryOption) => a.label.localeCompare(b.label));
|
|
|
|
const countryByCode = new Map(
|
|
countryOptions.map((country) => [country.value.toUpperCase(), country.label]),
|
|
);
|
|
const countryByName = new Map(
|
|
countryOptions.map((country) => [country.label.toLowerCase(), country.value]),
|
|
);
|
|
|
|
export const resolveCountryCode = (value?: string) => {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
const trimmed = value.trim();
|
|
if (!trimmed) {
|
|
return '';
|
|
}
|
|
const upper = trimmed.toUpperCase();
|
|
if (countryByCode.has(upper)) {
|
|
return upper;
|
|
}
|
|
const byName = countryByName.get(trimmed.toLowerCase());
|
|
return byName ?? '';
|
|
};
|
|
|
|
export const getCountryLabel = (value?: string) => {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
const resolved = resolveCountryCode(value);
|
|
if (!resolved) {
|
|
return value;
|
|
}
|
|
return countryByCode.get(resolved) ?? value;
|
|
};
|