Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tables number formatting respects user locale #796

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions browser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ This changelog covers all three packages, as they are (for now) updated as a who
### Atomic Browser

- Added persistent scrollbar to table
- Improved table header UX
- Numbers in tables now respect user locale

### @tomic/lib

Expand Down
19 changes: 3 additions & 16 deletions browser/data-browser/src/views/TablePage/EditorCells/FloatCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,10 @@ import { styled } from 'styled-components';
import { InputBase } from './InputBase';
import { ProgressBar } from './ProgressBar';
import { CellContainer, DisplayCellProps, EditCellProps } from './Type';
import { formatNumber } from '../helpers/formatNumber';

const { numberFormats } = urls.instances;

function formatValue(
value: number | undefined,
numberFormatting: string | undefined,
decimalPlaces: number | undefined,
) {
const isPercentage = numberFormatting === numberFormats.percentage;
const suffix = isPercentage ? ' %' : '';

const formattedValue =
decimalPlaces !== undefined ? value?.toFixed(decimalPlaces) : value;

return `${formattedValue}${suffix}`;
}

function FloatCellEdit({
value,
onChange,
Expand Down Expand Up @@ -65,10 +52,10 @@ function FloatCellDisplay({

const isPercentage = numberFormatting === numberFormats.percentage;

const formattedValue = formatValue(
const formattedValue = formatNumber(
value as number | undefined,
decimalPlaces ?? 2,
numberFormatting,
decimalPlaces,
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { styled } from 'styled-components';
import { InputBase } from './InputBase';
import { ProgressBar } from './ProgressBar';
import { CellContainer, DisplayCellProps, EditCellProps } from './Type';
import { formatNumber } from '../helpers/formatNumber';

const { numberFormats } = urls.instances;

Expand Down Expand Up @@ -40,11 +41,12 @@ function IntegerCellDisplay({
);

const isPercentage = numberFormatting === numberFormats.percentage;
const suffix = isPercentage ? ' %' : '';

return (
<>
<Aligned>{value && `${value}${suffix}`}</Aligned>
<Aligned>
{formatNumber(value as number | undefined, 0, numberFormatting)}
</Aligned>
{isPercentage && <ProgressBar percentage={value as number} />}
</>
);
Expand Down
27 changes: 27 additions & 0 deletions browser/data-browser/src/views/TablePage/helpers/formatNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { urls } from '@tomic/react';

export function formatNumber(
value: number | undefined,
numberOfDecimalPlaces: number,
formatting?: string,
): string {
if (value === undefined) {
return '';
}

if (formatting === urls.instances.numberFormats.percentage) {
const formatter = new Intl.NumberFormat('default', {
style: 'percent',
minimumFractionDigits: numberOfDecimalPlaces,
});

return formatter.format(value);
}

const formatter = new Intl.NumberFormat('default', {
style: 'decimal',
minimumFractionDigits: numberOfDecimalPlaces,
});

return formatter.format(value);
}
Loading