Skip to content

Commit 4aa8b7f

Browse files
Merge pull request #17 from multiversx/rt/fix/format-amount
Updated format amount
2 parents db63d00 + 23e2fb5 commit 4aa8b7f

File tree

6 files changed

+67
-108
lines changed

6 files changed

+67
-108
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
## [[0.0.0-alpha.1](https://github.com/multiversx/mx-sdk-dapp-core-ui/pull/16)] - 2025-01-20
11+
12+
- [Updated formatAmount component and removed dependencies](https://github.com/multiversx/mx-sdk-dapp-core-ui/pull/17)
1113
- [Updated formatAmount component](https://github.com/multiversx/mx-sdk-dapp-core-ui/pull/15)

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@
5353
"dependencies": {
5454
"@fortawesome/fontawesome-svg-core": ">= 6.7.2",
5555
"@fortawesome/free-solid-svg-icons": ">= 6.7.2",
56-
"@multiversx/sdk-dapp-utils": ">= 1.0.3",
57-
"@multiversx/sdk-core": ">= 13.5.0",
58-
"bignumber.js": ">= 9.1.2",
56+
"@multiversx/sdk-dapp-utils": ">= 1.0.4",
5957
"classnames": ">= 2.5.1",
6058
"qrcode": ">= 1.5.4"
6159
},

src/components.d.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ export namespace Components {
2828
}
2929
interface FormatAmount {
3030
"class"?: string;
31-
"decimals"?: number;
32-
"digits"?: number;
33-
"egldLabel"?: string;
34-
"showLabel"?: boolean;
35-
"showLastNonZeroDecimal"?: boolean;
36-
"token"?: string;
37-
"value": string;
31+
"isValid": boolean;
32+
"label"?: string;
33+
"labelClass"?: string;
34+
"valueDecimal": string;
35+
"valueInteger": string;
3836
}
3937
interface FungibleComponent {
4038
}
@@ -293,13 +291,11 @@ declare namespace LocalJSX {
293291
}
294292
interface FormatAmount {
295293
"class"?: string;
296-
"decimals"?: number;
297-
"digits"?: number;
298-
"egldLabel"?: string;
299-
"showLabel"?: boolean;
300-
"showLastNonZeroDecimal"?: boolean;
301-
"token"?: string;
302-
"value"?: string;
294+
"isValid"?: boolean;
295+
"label"?: string;
296+
"labelClass"?: string;
297+
"valueDecimal"?: string;
298+
"valueInteger"?: string;
303299
}
304300
interface FungibleComponent {
305301
}
Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { Component, Prop, h } from '@stencil/core';
2-
import BigNumber from 'bignumber.js';
3-
import { formatAmount } from '@multiversx/sdk-dapp-utils/out/helpers';
4-
import { DECIMALS, DIGITS } from '@multiversx/sdk-dapp-utils/out/constants';
52

63
@Component({
74
tag: 'format-amount',
@@ -10,13 +7,11 @@ import { DECIMALS, DIGITS } from '@multiversx/sdk-dapp-utils/out/constants';
107
})
118
export class FormatAmount {
129
@Prop() class?: string;
13-
@Prop() decimals?: number = DECIMALS;
14-
@Prop() digits?: number = DIGITS;
15-
@Prop() egldLabel?: string;
16-
@Prop() showLabel?: boolean = true;
17-
@Prop() showLastNonZeroDecimal?: boolean = false;
18-
@Prop() token?: string;
19-
@Prop() value: string;
10+
@Prop() isValid: boolean;
11+
@Prop() label?: string;
12+
@Prop() labelClass?: string;
13+
@Prop() valueDecimal: string;
14+
@Prop() valueInteger: string;
2015

2116
private renderInvalid() {
2217
return (
@@ -29,44 +24,32 @@ export class FormatAmount {
2924
}
3025

3126
private renderValid() {
32-
const formattedValue = formatAmount({
33-
input: this.value,
34-
decimals: this.decimals,
35-
digits: this.digits,
36-
showLastNonZeroDecimal: this.showLastNonZeroDecimal,
37-
addCommas: true
38-
});
39-
40-
const valueParts = formattedValue.split('.');
41-
const label = ` ${this.token ?? this.egldLabel ?? ''}`.trimEnd();
42-
4327
return (
4428
<span data-testid='formatAmountComponent' class={this.class}>
4529
<span class='int-amount' data-testid='formatAmountInt'>
46-
{valueParts[0]}
30+
{this.valueInteger}
4731
</span>
48-
{valueParts.length > 1 && (
32+
{this.valueDecimal && (
4933
<span class='decimals' data-testid='formatAmountDecimals'>
50-
.{valueParts[1]}
34+
.{this.valueDecimal}
5135
</span>
5236
)}
53-
{this.showLabel && (
37+
{this.label && (
5438
<span
5539
class={{
5640
'symbol': true,
57-
[this.token]: !!this.token
41+
[this.labelClass]: Boolean(this.labelClass)
5842
}}
5943
data-testid='formatAmountSymbol'
6044
>
61-
{label}
45+
{this.label}
6246
</span>
6347
)}
6448
</span>
6549
);
6650
}
6751

6852
render() {
69-
const isInteger = new BigNumber(this.value).isInteger();
70-
return !isInteger ? this.renderInvalid() : this.renderValid();
53+
return this.isValid ? this.renderValid() : this.renderInvalid();
7154
}
7255
}

src/components/format-amount/readme.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77

88
## Properties
99

10-
| Property | Attribute | Description | Type | Default |
11-
| ------------------------ | ---------------------------- | ----------- | --------- | ----------- |
12-
| `class` | `class` | | `string` | `undefined` |
13-
| `decimals` | `decimals` | | `number` | `DECIMALS` |
14-
| `digits` | `digits` | | `number` | `DIGITS` |
15-
| `egldLabel` | `egld-label` | | `string` | `undefined` |
16-
| `showLabel` | `show-label` | | `boolean` | `true` |
17-
| `showLastNonZeroDecimal` | `show-last-non-zero-decimal` | | `boolean` | `false` |
18-
| `token` | `token` | | `string` | `undefined` |
19-
| `value` | `value` | | `string` | `undefined` |
10+
| Property | Attribute | Description | Type | Default |
11+
| -------------- | --------------- | ----------- | --------- | ----------- |
12+
| `class` | `class` | | `string` | `undefined` |
13+
| `isValid` | `is-valid` | | `boolean` | `undefined` |
14+
| `label` | `label` | | `string` | `undefined` |
15+
| `labelClass` | `label-class` | | `string` | `undefined` |
16+
| `valueDecimal` | `value-decimal` | | `string` | `undefined` |
17+
| `valueInteger` | `value-integer` | | `string` | `undefined` |
2018

2119

2220
----------------------------------------------
Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { newSpecPage } from '@stencil/core/testing';
22
import { FormatAmount } from '../format-amount';
33

4-
describe('Format amount component when digits = 2', () => {
4+
describe('FormatAmount component', () => {
55
const renderComponent = async (props: any) => {
66
const page = await newSpecPage({
77
components: [FormatAmount],
88
html: '<format-amount></format-amount>',
9-
supportsShadowDom: true
9+
supportsShadowDom: true,
1010
});
1111

1212
const component = page.root;
13-
// Set each property individually
14-
Object.keys(props).forEach(key => {
13+
Object.keys(props).forEach((key) => {
1514
component[key] = props[key];
1615
});
1716

@@ -31,81 +30,64 @@ describe('Format amount component when digits = 2', () => {
3130
.length;
3231
};
3332

34-
it('should show 2 non zero decimals', async () => {
33+
it('should render valid amount with decimals', async () => {
3534
const props = {
36-
value: '9999979999800000000000000',
37-
showLastNonZeroDecimal: false,
38-
showLabel: true,
39-
digits: 2,
40-
egldLabel: 'EGLD'
35+
isValid: true,
36+
valueInteger: '99999',
37+
valueDecimal: '99',
38+
label: 'EGLD',
4139
};
4240

4341
const page = await renderComponent(props);
44-
expect(await decimalsSelector(page)).toBe('.99');
42+
expect(page.root.shadowRoot.querySelector('span[data-testid="formatAmountInt"]').textContent).toBe('99999');
43+
expect(decimalsSelector(page)).toBe('.99');
4544
});
4645

47-
it('should show 2 zero decimals', async () => {
46+
it('should not render decimals when valueDecimal is empty', async () => {
4847
const props = {
49-
value: '9000000000000000000000000',
50-
showLastNonZeroDecimal: false,
51-
showLabel: true,
52-
digits: 2,
53-
egldLabel: 'EGLD'
48+
isValid: true,
49+
valueInteger: '90000',
50+
valueDecimal: '',
51+
label: 'EGLD',
5452
};
5553

5654
const page = await renderComponent(props);
57-
expect(await decimalsSelector(page)).toBe('.00');
55+
expect(decimalsSelector(page)).toBe(undefined);
5856
});
5957

60-
it('should show all non zero decimals when showLastNonZeroDecimal = true', async () => {
58+
it('should render invalid state when isValid is false', async () => {
6159
const props = {
62-
value: '100000000000000',
63-
showLastNonZeroDecimal: true,
64-
showLabel: false,
65-
digits: 2,
66-
egldLabel: 'EGLD'
60+
isValid: false,
61+
valueInteger: '',
62+
valueDecimal: '',
63+
label: '',
6764
};
6865

6966
const page = await renderComponent(props);
70-
expect(await decimalsSelector(page)).toBe('.0001');
67+
expect(page.root.shadowRoot.querySelector('span[data-testid="formatAmountInt"]').textContent).toBe('...');
7168
});
7269

73-
it('should not show decimals when value is 0', async () => {
70+
it('should render symbol when label is provided', async () => {
7471
const props = {
75-
value: '100000000000000',
76-
showLastNonZeroDecimal: false,
77-
showLabel: true,
78-
digits: 2,
79-
egldLabel: 'EGLD'
72+
isValid: true,
73+
valueInteger: '90000',
74+
valueDecimal: '00',
75+
label: 'EGLD',
8076
};
8177

8278
const page = await renderComponent(props);
83-
expect(await decimalsSelector(page)).toBe(undefined);
79+
expect(symbolSelector(page)).toBe(1);
8480
});
8581

86-
it('should show symbol', async () => {
82+
it('should not render symbol when label is not provided', async () => {
8783
const props = {
88-
value: '9000000000000000000000000',
89-
showLastNonZeroDecimal: false,
90-
showLabel: true,
91-
digits: 2,
92-
egldLabel: 'EGLD'
84+
isValid: true,
85+
valueInteger: '90000',
86+
valueDecimal: '00',
87+
label: '',
9388
};
9489

9590
const page = await renderComponent(props);
96-
expect(await symbolSelector(page)).toBe(1);
97-
});
98-
99-
it('should not show symbol', async () => {
100-
const props = {
101-
value: '9000000000000000000000000',
102-
showLastNonZeroDecimal: false,
103-
showLabel: false,
104-
digits: 2,
105-
egldLabel: 'EGLD'
106-
};
107-
108-
const page = await renderComponent(props);
109-
expect(await symbolSelector(page)).toBe(0);
91+
expect(symbolSelector(page)).toBe(0);
11092
});
11193
});

0 commit comments

Comments
 (0)