forked from cardstack/boxel
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurrency-amount.gts
132 lines (117 loc) · 3.22 KB
/
currency-amount.gts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import {
Component,
FieldDef,
contains,
field,
} from 'https://cardstack.com/base/card-api';
import StringField from 'https://cardstack.com/base/string';
import NumberField from 'https://cardstack.com/base/number';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
import {
BoxelInput,
BoxelSelect,
FieldContainer,
} from '@cardstack/boxel-ui/components';
const formatNumber = (val: number) => {
let formatter = new Intl.NumberFormat('en-US', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
return formatter.format(val);
};
class EmbeddedSecForAmount extends Component<typeof CurrencyAmount> {
get getFormattedAmount() {
if (!this.args.model.totalAmount) return null;
const formattedNumber = formatNumber(this.args.model.totalAmount);
return formattedNumber;
}
<template>
{{this.getFormattedAmount}}
</template>
}
class EditSecForAmount extends Component<typeof CurrencyAmount> {
@tracked currencyOptions = ['Select', 'RM', 'USD'];
get selectedCurrency() {
return this.args.model.currency || this.currencyOptions[0] || 'Select';
}
@action
updateCurrency(item: string) {
this.args.model.currency = item;
}
@action
updateAmount(val: number) {
this.args.model.totalAmount = val;
}
get getFormattedAmount() {
if (!this.args.model.totalAmount) return null;
const formattedNumber = formatNumber(this.args.model.totalAmount);
return formattedNumber;
}
<template>
<div class='container'>
<div class='form-row-full'>
<FieldContainer @tag='label' @vertical={{true}} class='left-input'>
<BoxelSelect
@selected={{this.selectedCurrency}}
@onChange={{this.updateCurrency}}
@options={{this.currencyOptions}}
class='select'
aria-label='Select Currency'
as |item|
>
<div>{{item}}</div>
</BoxelSelect>
</FieldContainer>
<FieldContainer @tag='label' @vertical={{true}} class='right-input'>
<BoxelInput
@value={{this.args.model.totalAmount}}
@onInput={{this.updateAmount}}
@helperText={{this.getFormattedAmount}}
/>
</FieldContainer>
</div>
</div>
<style>
.container {
container-type: inline-size;
}
.form-row-full {
width: 100%;
display: flex;
flex-wrap: wrap;
gap: var(--boxel-sp-xs);
}
.left-input {
display: inline-block;
min-width: 100px;
flex: auto;
}
.right-input {
display: inline-block;
flex-grow: 1;
width: 100%;
}
.select {
padding: var(--boxel-sp-xxs);
background-color: white;
}
@container (min-width: 640px) {
.form-row-full {
flex-wrap: nowrap;
}
}
</style>
</template>
}
export class CurrencyAmount extends FieldDef {
static displayName = 'Currency Amount';
@field currency = contains(StringField, {
description: `Currency`,
});
@field totalAmount = contains(NumberField, {
description: `Total Amount`,
});
static embedded = EmbeddedSecForAmount;
static edit = EditSecForAmount;
}