@alihbuzaid/ember-ui
Version:
Fleetbase UI provides all the interface components, helpers, services and utilities for building a Fleetbase extension into the Console.
70 lines (58 loc) • 1.88 kB
JavaScript
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import { guidFor } from '@ember/object/internals';
import { task } from 'ember-concurrency';
export default class CountrySelectComponent extends Component {
fetch;
countries = [];
selected;
disabled = false;
value;
id = guidFor(this);
constructor(owner, { value = null, disabled = false }) {
super(...arguments);
this.disabled = disabled;
this.value = value;
this.fetchCountries.perform(value);
}
*fetchCountries(value = null) {
try {
this.countries = yield this.fetch.get('lookup/countries', { columns: ['name', 'cca2', 'flag', 'emoji'] });
if (value) {
this.selected = this.findCountry(value);
}
} catch (error) {
this.countries = [];
}
}
changed(value) {
const country = this.findCountry(value);
if (country) {
this.selectCountry(country);
}
}
listenForInputChanges(element) {
setInterval(() => {
const { value } = element;
if (this.value !== value) {
this.value = value;
this.changed(value);
}
}, 100);
}
selectCountry(country) {
const { onChange } = this.args;
this.selected = country;
if (typeof onChange === 'function') {
onChange(country.cca2, country);
}
}
findCountry(iso2) {
if (typeof iso2 === 'string') {
return this.countries.find((country) => country.cca2 === iso2.toUpperCase());
}
return null;
}
}