From ee69385b3dafa404885b5f82d4fcc9123eee7890 Mon Sep 17 00:00:00 2001 From: tintinthong Date: Mon, 2 Dec 2024 16:02:50 +0800 Subject: [PATCH] introduce country dropdown with cdn data --- .../addon/src/components/select/index.gts | 1 + .../86fee625-f9f0-460a-99aa-917d25657ec1.json | 20 +++++ packages/experiments-realm/country.gts | 78 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 packages/experiments-realm/CardWithCountryField/86fee625-f9f0-460a-99aa-917d25657ec1.json diff --git a/packages/boxel-ui/addon/src/components/select/index.gts b/packages/boxel-ui/addon/src/components/select/index.gts index fb60a546ee..d202dd1b36 100644 --- a/packages/boxel-ui/addon/src/components/select/index.gts +++ b/packages/boxel-ui/addon/src/components/select/index.gts @@ -33,6 +33,7 @@ const BoxelSelect: TemplateOnlyComponent = }; } + +function getCountryFlagEmoji(countryCode: string) { + const codePoints = countryCode + .toUpperCase() + .split('') + .map((char) => 127397 + char.charCodeAt(0)); + return String.fromCodePoint(...codePoints); +} + +interface CountryData { + code: string; + name: string; + emoji: string; +} + +class CountryFieldEdit extends Component { + @tracked countryDataFindLib: any; + @tracked country: CountryData | undefined; + @tracked countries: CountryData[] = []; + + constructor(owner: Owner, args: any) { + super(owner, args); + this.loadCountries.perform(); + } + + private loadCountries = restartableTask(async () => { + this.countries = countryDataFind.Array().map((country: any) => { + return { + code: country.ISO2_CODE, + name: country.LIST_OF_NAME.ENG, + emoji: getCountryFlagEmoji(country.ISO2_CODE), + } as CountryData; + }); + }); + + @action onSelectCountry(country: any) { + this.country = country; + } + + +} + +export class CountryField extends FieldDef { + static displayName = 'Country'; + @field name = contains(StringField); + @field code = contains(StringField); + static edit = CountryFieldEdit; +} + +export class CardWithCountryField extends CardDef { + static displayName = 'Card With Country Field'; + @field country = contains(CountryField); + + static isolated = class Isolated extends Component { + + }; +}