Skip to content

Commit 9788930

Browse files
committed
Tables number formatting respects user locale
1 parent d84f5c6 commit 9788930

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

browser/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This changelog covers all three packages, as they are (for now) updated as a who
1414
### Atomic Browser
1515

1616
- Added persistent scrollbar to table
17+
- Improved table header UX
18+
- Numbers in tables now respect user locale
1719

1820
### @tomic/lib
1921

browser/data-browser/src/views/TablePage/EditorCells/FloatCell.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,10 @@ import { styled } from 'styled-components';
1010
import { InputBase } from './InputBase';
1111
import { ProgressBar } from './ProgressBar';
1212
import { CellContainer, DisplayCellProps, EditCellProps } from './Type';
13+
import { formatNumber } from '../helpers/formatNumber';
1314

1415
const { numberFormats } = urls.instances;
1516

16-
function formatValue(
17-
value: number | undefined,
18-
numberFormatting: string | undefined,
19-
decimalPlaces: number | undefined,
20-
) {
21-
const isPercentage = numberFormatting === numberFormats.percentage;
22-
const suffix = isPercentage ? ' %' : '';
23-
24-
const formattedValue =
25-
decimalPlaces !== undefined ? value?.toFixed(decimalPlaces) : value;
26-
27-
return `${formattedValue}${suffix}`;
28-
}
29-
3017
function FloatCellEdit({
3118
value,
3219
onChange,
@@ -65,10 +52,10 @@ function FloatCellDisplay({
6552

6653
const isPercentage = numberFormatting === numberFormats.percentage;
6754

68-
const formattedValue = formatValue(
55+
const formattedValue = formatNumber(
6956
value as number | undefined,
57+
decimalPlaces ?? 2,
7058
numberFormatting,
71-
decimalPlaces,
7259
);
7360

7461
return (

browser/data-browser/src/views/TablePage/EditorCells/IntegerCell.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { styled } from 'styled-components';
44
import { InputBase } from './InputBase';
55
import { ProgressBar } from './ProgressBar';
66
import { CellContainer, DisplayCellProps, EditCellProps } from './Type';
7+
import { formatNumber } from '../helpers/formatNumber';
78

89
const { numberFormats } = urls.instances;
910

@@ -40,11 +41,12 @@ function IntegerCellDisplay({
4041
);
4142

4243
const isPercentage = numberFormatting === numberFormats.percentage;
43-
const suffix = isPercentage ? ' %' : '';
4444

4545
return (
4646
<>
47-
<Aligned>{value && `${value}${suffix}`}</Aligned>
47+
<Aligned>
48+
{formatNumber(value as number | undefined, 0, numberFormatting)}
49+
</Aligned>
4850
{isPercentage && <ProgressBar percentage={value as number} />}
4951
</>
5052
);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { urls } from '@tomic/react';
2+
3+
export function formatNumber(
4+
value: number | undefined,
5+
numberOfDecimalPlaces: number,
6+
formatting?: string,
7+
): string {
8+
if (value === undefined) {
9+
return '';
10+
}
11+
12+
if (formatting === urls.instances.numberFormats.percentage) {
13+
const formatter = new Intl.NumberFormat('default', {
14+
style: 'percent',
15+
minimumFractionDigits: numberOfDecimalPlaces,
16+
});
17+
18+
return formatter.format(value);
19+
}
20+
21+
const formatter = new Intl.NumberFormat('default', {
22+
style: 'decimal',
23+
minimumFractionDigits: numberOfDecimalPlaces,
24+
});
25+
26+
return formatter.format(value);
27+
}

0 commit comments

Comments
 (0)