Skip to content

Commit 8797baf

Browse files
Pollepsjoepio
authored andcommitted
Fix some number formatting issues in tables
1 parent 03acd17 commit 8797baf

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function FloatCellDisplay({
5454

5555
const formattedValue = formatNumber(
5656
value as number | undefined,
57-
decimalPlaces ?? 2,
57+
decimalPlaces,
5858
numberFormatting,
5959
);
6060

browser/data-browser/src/views/TablePage/PropertyForm/Inputs/DecimalPlacesInput.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function DecimalPlacesInput({
2121
commit: true,
2222
});
2323

24-
const [__, setDecimalPlaces] = useNumber(
24+
const [decimalPlaces, setDecimalPlaces] = useNumber(
2525
resource,
2626
urls.properties.constraints.decimalPlaces,
2727
{ commit: true },
@@ -32,8 +32,10 @@ export function DecimalPlacesInput({
3232
const newValue = e.target.value;
3333
const num = Number.parseInt(newValue, 10);
3434

35-
if (num < 0) {
36-
setError('Value must be eighter positive, zero or empty.');
35+
if (num < 0 || num > 20) {
36+
setError('Value must be between 0 and 20.', true);
37+
38+
return;
3739
} else {
3840
setError(undefined);
3941
}
@@ -63,7 +65,9 @@ export function DecimalPlacesInput({
6365
<InputStyled
6466
id={id}
6567
type='number'
68+
defaultValue={decimalPlaces}
6669
min={0}
70+
max={20}
6771
onBlur={onBlur}
6872
onChange={handleDecimalPointChange}
6973
/>

browser/data-browser/src/views/TablePage/helpers/formatNumber.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,40 @@ import { urls } from '@tomic/react';
22

33
export function formatNumber(
44
value: number | undefined,
5-
numberOfDecimalPlaces: number,
5+
fractionDigits: number | undefined,
66
formatting?: string,
77
): string {
88
if (value === undefined) {
99
return '';
1010
}
1111

12+
// Bad data like negative values will cause a crash so we need to make it valid before formatting.
13+
const fixedFractionDigits = fixInvalidFractionDigits(fractionDigits);
14+
1215
if (formatting === urls.instances.numberFormats.percentage) {
1316
const formatter = new Intl.NumberFormat('default', {
1417
style: 'percent',
15-
minimumFractionDigits: numberOfDecimalPlaces,
18+
minimumFractionDigits: fixedFractionDigits,
1619
});
1720

18-
return formatter.format(value);
21+
return formatter.format(value / 100);
1922
}
2023

2124
const formatter = new Intl.NumberFormat('default', {
2225
style: 'decimal',
23-
minimumFractionDigits: numberOfDecimalPlaces,
26+
minimumFractionDigits: fixedFractionDigits,
2427
});
2528

2629
return formatter.format(value);
2730
}
31+
32+
function fixInvalidFractionDigits(
33+
fractionDigits: number | undefined,
34+
): number | undefined {
35+
if (fractionDigits === undefined) {
36+
return undefined;
37+
}
38+
39+
// INTL only supports 0-20 fraction digits
40+
return Math.min(20, Math.max(0, fractionDigits));
41+
}

0 commit comments

Comments
 (0)