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 country: CountryData | undefined = + this.args.model.name && this.args.model.code + ? { + name: this.args.model.name, + code: this.args.model.code, + } + : 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[0], + emoji: getCountryFlagEmoji(country.ISO2_CODE), + } as CountryData; + }); + }); + + @action onSelectCountry(country: CountryData) { + this.country = country; + this.args.model.name = country.name; + this.args.model.code = country.code; + } + + @action countryEmoji(countryCode: string) { + return this.countries?.find((country) => country.code === countryCode) + ?.emoji; + } + + +} + +export class CountryField extends FieldDef { + static displayName = 'Country'; + @field name = contains(StringField); + @field code = contains(StringField); + static edit = CountryFieldEdit; + + static atom = class Atom extends Component { + + }; +} + +export class CardWithCountryField extends CardDef { + static displayName = 'Card With Country Field'; + @field country = contains(CountryField); + + static isolated = class Isolated extends Component { + + }; +}