-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce country dropdown with cdn data
- Loading branch information
1 parent
0b43793
commit ee69385
Showing
3 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/experiments-realm/CardWithCountryField/86fee625-f9f0-460a-99aa-917d25657ec1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"data": { | ||
"type": "card", | ||
"attributes": { | ||
"country": { | ||
"name": null, | ||
"code": null | ||
}, | ||
"title": null, | ||
"description": null, | ||
"thumbnailURL": null | ||
}, | ||
"meta": { | ||
"adoptsFrom": { | ||
"module": "../country", | ||
"name": "CardWithCountryField" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,16 @@ import { | |
field, | ||
Component, | ||
CardDef, | ||
FieldDef, | ||
} from 'https://cardstack.com/base/card-api'; | ||
import StringField from 'https://cardstack.com/base/string'; | ||
import World from '@cardstack/boxel-icons/world'; | ||
import { BoxelSelect } from '@cardstack/boxel-ui/components'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { action } from '@ember/object'; | ||
import { restartableTask } from 'ember-concurrency'; | ||
import type Owner from '@ember/owner'; | ||
import countryDataFind from 'https://esm.run/[email protected]'; | ||
|
||
export class Country extends CardDef { | ||
static displayName = 'Country'; | ||
|
@@ -23,3 +30,74 @@ export class Country extends CardDef { | |
</template> | ||
}; | ||
} | ||
|
||
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<typeof CountryField> { | ||
@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; | ||
} | ||
|
||
<template> | ||
<BoxelSelect | ||
@options={{this.countries}} | ||
@selected={{this.country}} | ||
@onSelect={{@set}} | ||
@loadingMessage='Loading countries...' | ||
@onChange={{this.onSelectCountry}} | ||
as |country| | ||
> | ||
{{country.emoji}} | ||
{{country.name}} | ||
</BoxelSelect> | ||
</template> | ||
} | ||
|
||
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<typeof this> { | ||
<template> | ||
<@fields.country /> | ||
</template> | ||
}; | ||
} |