use Intl.NumberFormat to format currency

This commit is contained in:
Haben Amare 2020-05-25 22:05:06 +00:00
parent d26786a628
commit 8732e4b18f
7 changed files with 70 additions and 25 deletions

View file

@ -85,11 +85,16 @@ CardCustomField.register('cardCustomField');
onCreated() { onCreated() {
super.onCreated(); super.onCreated();
this.currencySymbol = this.data().definition.settings.currencySymbol; this.currencyCode = this.data().definition.settings.currencyCode;
} }
formattedValue() { formattedValue() {
return `${this.currencySymbol}${this.data().value}`; const locale = TAPi18n.getLanguage();
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: this.currencyCode,
}).format(this.data().value);
} }
events() { events() {

View file

@ -16,7 +16,11 @@ BlazeComponent.extendComponent({
const customFieldTrueValue = const customFieldTrueValue =
customField && customField.trueValue ? customField.trueValue : ''; customField && customField.trueValue ? customField.trueValue : '';
return `${definition.settings.currencySymbol}${customFieldTrueValue}`; const locale = TAPi18n.getLanguage();
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: definition.settings.currencyCode,
}).format(customFieldTrueValue);
}, },
events() { events() {

View file

@ -38,7 +38,7 @@ template(name="createCustomFieldPopup")
label label
| {{_ 'custom-field-currency-option'}} | {{_ 'custom-field-currency-option'}}
select.js-field-currency select.js-field-currency
each getCurrencySymbols each getCurrencyCodes
if selected if selected
option(value=value selected="selected") {{name}} option(value=value selected="selected") {{name}}
else else

View file

@ -18,11 +18,47 @@ BlazeComponent.extendComponent({
const CreateCustomFieldPopup = BlazeComponent.extendComponent({ const CreateCustomFieldPopup = BlazeComponent.extendComponent({
_types: ['text', 'number', 'date', 'dropdown', 'currency'], _types: ['text', 'number', 'date', 'dropdown', 'currency'],
_defaultCurrencySymbols: [ _currencyList: [
{ symbol: '$' }, {
{ symbol: '€' }, name: 'US Dollar',
{ symbol: '£' }, code: 'USD',
{ symbol: '¥' }, },
{
name: 'Euro',
code: 'EUR',
},
{
name: 'Yen',
code: 'JPY',
},
{
name: 'Pound Sterling',
code: 'GBP',
},
{
name: 'Australian Dollar',
code: 'AUD',
},
{
name: 'Canadian Dollar',
code: 'CAD',
},
{
name: 'Swiss Franc',
code: 'CHF',
},
{
name: 'Yuan Renminbi',
code: 'CNY',
},
{
name: 'Hong Kong Dollar',
code: 'HKD',
},
{
name: 'New Zealand Dollar',
code: 'NZD',
},
], ],
onCreated() { onCreated() {
@ -30,10 +66,10 @@ const CreateCustomFieldPopup = BlazeComponent.extendComponent({
this.data().type ? this.data().type : this._types[0], this.data().type ? this.data().type : this._types[0],
); );
this.currencySymbol = new ReactiveVar( this.currencyCode = new ReactiveVar(
this.data().settings && this.data().settings.currencySymbol this.data().settings && this.data().settings.currencyCode
? this.data().settings.currencySymbol ? this.data().settings.currencyCode
: this._defaultCurrencySymbols[0].symbol, : this._currencyList[0].code,
); );
this.dropdownItems = new ReactiveVar( this.dropdownItems = new ReactiveVar(
@ -58,14 +94,14 @@ const CreateCustomFieldPopup = BlazeComponent.extendComponent({
return this.type.get() !== type; return this.type.get() !== type;
}, },
getCurrencySymbols() { getCurrencyCodes() {
const currentSymbol = this.currencySymbol.get(); const currentCode = this.currencyCode.get();
return this._defaultCurrencySymbols.map(({ symbol }) => { return this._currencyList.map(({ name, code }) => {
return { return {
name: symbol, name: `${code} - ${name}`,
value: symbol, value: code,
selected: symbol === currentSymbol, selected: code === currentCode,
}; };
}); });
}, },
@ -89,8 +125,8 @@ const CreateCustomFieldPopup = BlazeComponent.extendComponent({
const settings = {}; const settings = {};
switch (this.type.get()) { switch (this.type.get()) {
case 'currency': { case 'currency': {
const currencySymbol = this.currencySymbol.get(); const currencyCode = this.currencyCode.get();
settings.currencySymbol = currencySymbol; settings.currencyCode = currencyCode;
break; break;
} }
case 'dropdown': { case 'dropdown': {
@ -113,7 +149,7 @@ const CreateCustomFieldPopup = BlazeComponent.extendComponent({
}, },
'change .js-field-currency'(evt) { 'change .js-field-currency'(evt) {
const value = evt.target.value; const value = evt.target.value;
this.currencySymbol.set(value); this.currencyCode.set(value);
}, },
'keydown .js-dropdown-item.last'(evt) { 'keydown .js-dropdown-item.last'(evt) {
if (evt.target.value.trim() && evt.keyCode === 13) { if (evt.target.value.trim() && evt.keyCode === 13) {

View file

@ -257,7 +257,7 @@
"custom-field-delete-pop": "There is no undo. This will remove this custom field from all cards and destroy its history.", "custom-field-delete-pop": "There is no undo. This will remove this custom field from all cards and destroy its history.",
"custom-field-checkbox": "Checkbox", "custom-field-checkbox": "Checkbox",
"custom-field-currency": "Currency", "custom-field-currency": "Currency",
"custom-field-currency-option": "Currency Symbol", "custom-field-currency-option": "Currency Code",
"custom-field-date": "Date", "custom-field-date": "Date",
"custom-field-dropdown": "Dropdown List", "custom-field-dropdown": "Dropdown List",
"custom-field-dropdown-none": "(none)", "custom-field-dropdown-none": "(none)",

View file

@ -257,7 +257,7 @@
"custom-field-delete-pop": "There is no undo. This will remove this custom field from all cards and destroy its history.", "custom-field-delete-pop": "There is no undo. This will remove this custom field from all cards and destroy its history.",
"custom-field-checkbox": "Checkbox", "custom-field-checkbox": "Checkbox",
"custom-field-currency": "Currency", "custom-field-currency": "Currency",
"custom-field-currency-option": "Currency Symbol", "custom-field-currency-option": "Currency Code",
"custom-field-date": "Date", "custom-field-date": "Date",
"custom-field-dropdown": "Dropdown List", "custom-field-dropdown": "Dropdown List",
"custom-field-dropdown-none": "(none)", "custom-field-dropdown-none": "(none)",

View file

@ -30,7 +30,7 @@ CustomFields.attachSchema(
*/ */
type: Object, type: Object,
}, },
'settings.currencySymbol': { 'settings.currencyCode': {
type: String, type: String,
optional: true, optional: true,
}, },