gristlabs_grist-core/app/client/widgets/NumericEditor.ts
Dmitry S 93a2d26182 (core) Fix a bug with editing numbers in some locales.
Summary:
Adds a new test for formatting and fix several related bugs it uncovered:
1. When editing a number with "," decimal separator, ensure it opens in
   the editor with "," (rather than ".", the original bug motivating this).
2. When guessing number format, set maxDecimals when it's needed
   (otherwise, e.g. "$1.234", or "4.5%" weren't guessed as numeric)
3. When guessing number format, ignore whitespace when deciding if
   guessed format is correct (otherwise percents can't be guessed in
   locales which add "%" with a non-breaking space before it).

Test Plan: Added a test case that exercises all fixed behaviors.

Reviewers: paulfitz

Reviewed By: paulfitz

Subscribers: paulfitz

Differential Revision: https://phab.getgrist.com/D4177
2024-02-02 22:48:05 -05:00

18 lines
853 B
TypeScript

import {FieldOptions} from 'app/client/widgets/NewBaseEditor';
import {NTextEditor} from 'app/client/widgets/NTextEditor';
export class NumericEditor extends NTextEditor {
constructor(protected options: FieldOptions) {
if (!options.editValue && typeof options.cellValue === 'number') {
// If opening a number for editing, we render it using the basic string representation (e.g.
// no currency symbols or groupings), but it's important to use the right locale so that the
// number can be parsed back (e.g. correct decimal separator).
const locale = options.field.documentSettings.peek().locale;
const fmt = new Intl.NumberFormat(locale, {useGrouping: false, maximumFractionDigits: 20});
const editValue = fmt.format(options.cellValue);
options = {...options, editValue};
}
super(options);
}
}