Skip to content

Commit ef3fc0f

Browse files
authored
feat(Money): add roundingMode prop (#215)
New feature: - Added support for the roundingMode string parameter ("ROUND_DOWN", "ROUND_UP", etc.) - Implemented the internal ROUNDING_MAP mapping, which maps string values ​​to the BigNumber rounding enum. - Set the default value to ROUND_DOWN. - The Money and Number components no longer require the use of bignumber.js on the consumer side.
1 parent c7a8871 commit ef3fc0f

5 files changed

Lines changed: 42 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cadolabs/sphere-ui",
3-
"version": "6.8.1",
3+
"version": "6.9.0",
44
"main": "dist/index.js",
55
"exports": {
66
".": "./dist/index.js",

src/components/Number/index.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
import BigNumber from "bignumber.js"
22

3+
import { ROUNDING_MAP } from "./rounding"
4+
35
const DEFAULT_PRECISION = 2
46
const DEFAULT_DELIMITER = ","
57
const DEFAULT_SEPARATOR = "\xA0"
8+
const DEFAULT_ROUNDING = "ROUND_DOWN"
9+
10+
export const Number = ({
11+
value,
12+
enforcePrecision = true,
13+
precision = DEFAULT_PRECISION,
14+
roundingMode = DEFAULT_ROUNDING,
15+
delimiter = DEFAULT_DELIMITER,
16+
separator = DEFAULT_SEPARATOR,
17+
}) => {
18+
const bnRounding =
19+
ROUNDING_MAP[roundingMode] ?? ROUNDING_MAP[DEFAULT_ROUNDING]
620

7-
export const Number = ({ value, enforcePrecision = true, ...options }) => {
8-
const instance = new BigNumber(value)
9-
const format = { groupSize: 3 }
21+
const instance = new BigNumber(value).decimalPlaces(
22+
precision,
23+
bnRounding,
24+
)
1025

11-
const precision = options.precision == null ? DEFAULT_PRECISION : options.precision
12-
format.decimalSeparator = options.delimiter == null ? DEFAULT_DELIMITER : options.delimiter
13-
format.groupSeparator = options.separator == null ? DEFAULT_SEPARATOR : options.separator
26+
const format = {
27+
groupSize: 3,
28+
decimalSeparator: delimiter,
29+
groupSeparator: separator,
30+
}
1431

1532
const string = instance.toFormat(precision, undefined, format)
1633

1734
return enforcePrecision
1835
? string
19-
: string.replace(new RegExp(`(\\${DEFAULT_DELIMITER}\\d+)0+`), "$1")
36+
: string.replace(new RegExp(`(\\${delimiter}\\d+)0+`), "$1")
2037
}

src/components/Number/rounding.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import BigNumber from "bignumber.js"
2+
3+
export const ROUNDING_MAP = {
4+
ROUND_UP: BigNumber.ROUND_UP,
5+
ROUND_DOWN: BigNumber.ROUND_DOWN,
6+
ROUND_CEIL: BigNumber.ROUND_CEIL,
7+
ROUND_FLOOR: BigNumber.ROUND_FLOOR,
8+
ROUND_HALF_UP: BigNumber.ROUND_HALF_UP,
9+
ROUND_HALF_DOWN: BigNumber.ROUND_HALF_DOWN,
10+
ROUND_HALF_EVEN: BigNumber.ROUND_HALF_EVEN,
11+
ROUND_HALF_CEIL: BigNumber.ROUND_HALF_CEIL,
12+
ROUND_HALF_FLOOR: BigNumber.ROUND_HALF_FLOOR,
13+
}

storybook/i18n/locales/stories/money/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ props:
66
delimiter: Delimiter for fractional portion.
77
separator: Separator for thousands.
88
currencySeparator: Separator between amount and currency.
9+
roundingMode: "Rounding mode for decimal values. Accepts one of: ROUND_UP, ROUND_DOWN, ROUND_CEIL, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_CEIL, ROUND_HALF_FLOOR"

storybook/stories/display/Money/money.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ function MoneyExample () {
1111
<div className="p-card s-container">
1212
<h3>Money component</h3>
1313
<Money
14-
money={[1000.1000, "USD"]}
14+
money={[102.25599999, "USD"]}
1515
precision={2}
1616
enforcePrecision
1717
delimiter={"."}
1818
separator={" "}
19+
roundingMode="ROUND_UP"
1920
/>
2021
2122
<h3>with currencySeparator</h3>
@@ -46,5 +47,6 @@ export const money = {
4647
{ name: "delimiter", type: "string", default: ",", description: `${I18N_PREFIX}.props.delimiter` },
4748
{ name: "separator", type: "string", default: "'\xA0'", description: `${I18N_PREFIX}.props.separator` },
4849
{ name: "currencySeparator", type: "string", default: "' '", description: `${I18N_PREFIX}.props.currencySeparator` },
50+
{ name: "roundingMode", type: "string", default: "ROUND_DOWN", description: `${I18N_PREFIX}.props.roundingMode` },
4951
],
5052
}

0 commit comments

Comments
 (0)