@open-mercato/ui 0.10.1-develop.8289.1.9a25111d49 → 0.10.1-develop.8291.1.6ec3aee41c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/backend/CrudForm.js +12 -8
- package/dist/backend/CrudForm.js.map +2 -2
- package/dist/utils/format.js +46 -7
- package/dist/utils/format.js.map +2 -2
- package/package.json +3 -3
- package/src/backend/CrudForm.tsx +19 -9
- package/src/backend/__tests__/CrudForm.accessibleLabels.test.tsx +12 -0
- package/src/utils/__tests__/format.test.ts +64 -0
- package/src/utils/format.ts +82 -6
package/dist/utils/format.js
CHANGED
|
@@ -1,19 +1,58 @@
|
|
|
1
|
-
|
|
1
|
+
import {
|
|
2
|
+
countDecimalPlaces,
|
|
3
|
+
decimalToString,
|
|
4
|
+
parseDecimal,
|
|
5
|
+
resolveCurrencyDisplayDecimalPlaces
|
|
6
|
+
} from "@open-mercato/shared/lib/decimal";
|
|
7
|
+
function formatCurrency(value, currency, locale, decimalPlaces, options) {
|
|
2
8
|
if (value === null || value === void 0 || value === "") return null;
|
|
3
|
-
const numeric = typeof value === "number" ? value : Number(value);
|
|
4
|
-
if (!Number.isFinite(numeric)) {
|
|
5
|
-
return typeof value === "string" ? value : null;
|
|
6
|
-
}
|
|
7
9
|
const code = currency && currency.length === 3 ? currency.toUpperCase() : void 0;
|
|
10
|
+
const exact = parseDecimal(value);
|
|
11
|
+
if (!exact) return formatUnparsedAmount(value, code, locale);
|
|
12
|
+
const amount = decimalToString(exact);
|
|
13
|
+
const digits = resolveFractionDigits(exact, code, decimalPlaces, options?.showAllDigits === true);
|
|
14
|
+
return formatAmount(amount, code, locale, digits);
|
|
15
|
+
}
|
|
16
|
+
function formatAmount(amount, code, locale, digits) {
|
|
8
17
|
try {
|
|
9
18
|
if (code) {
|
|
10
|
-
return new Intl.NumberFormat(locale, { style: "currency", currency: code }).format(
|
|
19
|
+
return new Intl.NumberFormat(locale, { style: "currency", currency: code, ...digits }).format(amount);
|
|
11
20
|
}
|
|
12
21
|
} catch {
|
|
13
22
|
}
|
|
14
|
-
const formatted = new Intl.NumberFormat(locale).format(
|
|
23
|
+
const formatted = new Intl.NumberFormat(locale, digits).format(amount);
|
|
15
24
|
return code ? `${formatted} ${code}` : formatted;
|
|
16
25
|
}
|
|
26
|
+
function formatUnparsedAmount(value, code, locale) {
|
|
27
|
+
const numeric = typeof value === "number" ? value : Number(value);
|
|
28
|
+
if (!Number.isFinite(numeric)) return typeof value === "string" ? value : null;
|
|
29
|
+
return formatAmount(numeric, code, locale, {});
|
|
30
|
+
}
|
|
31
|
+
const DEFAULT_CURRENCY_FRACTION_DIGITS = 2;
|
|
32
|
+
const LEGACY_MAX_INTL_FRACTION_DIGITS = 20;
|
|
33
|
+
const EXTENDED_MAX_INTL_FRACTION_DIGITS = 100;
|
|
34
|
+
let maxIntlFractionDigits = null;
|
|
35
|
+
function resolveMaxIntlFractionDigits() {
|
|
36
|
+
if (maxIntlFractionDigits !== null) return maxIntlFractionDigits;
|
|
37
|
+
try {
|
|
38
|
+
new Intl.NumberFormat("en", { maximumFractionDigits: EXTENDED_MAX_INTL_FRACTION_DIGITS });
|
|
39
|
+
maxIntlFractionDigits = EXTENDED_MAX_INTL_FRACTION_DIGITS;
|
|
40
|
+
} catch {
|
|
41
|
+
maxIntlFractionDigits = LEGACY_MAX_INTL_FRACTION_DIGITS;
|
|
42
|
+
}
|
|
43
|
+
return maxIntlFractionDigits;
|
|
44
|
+
}
|
|
45
|
+
function resolveFractionDigits(exact, code, decimalPlaces, showAllDigits) {
|
|
46
|
+
const configured = typeof decimalPlaces === "number" && Number.isInteger(decimalPlaces) && decimalPlaces >= 0 ? decimalPlaces : null;
|
|
47
|
+
const displayDigits = code ? resolveCurrencyDisplayDecimalPlaces(code) : null;
|
|
48
|
+
if (!code && configured === null && !showAllDigits) return {};
|
|
49
|
+
const currencyDigits = code ? displayDigits ?? DEFAULT_CURRENCY_FRACTION_DIGITS : configured === null ? 0 : DEFAULT_CURRENCY_FRACTION_DIGITS;
|
|
50
|
+
const minimum = configured === null ? currencyDigits : Math.min(currencyDigits, configured);
|
|
51
|
+
const precision = configured ?? displayDigits;
|
|
52
|
+
const valueDigits = showAllDigits || precision === null ? countDecimalPlaces(exact) : precision;
|
|
53
|
+
const maximum = Math.min(Math.max(minimum, valueDigits), resolveMaxIntlFractionDigits());
|
|
54
|
+
return { minimumFractionDigits: Math.min(minimum, maximum), maximumFractionDigits: maximum };
|
|
55
|
+
}
|
|
17
56
|
function formatDate(value, locale) {
|
|
18
57
|
if (!value) return null;
|
|
19
58
|
const date = new Date(value);
|
package/dist/utils/format.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/utils/format.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Format a monetary value with an optional ISO-4217 currency code.\n *\n * Returns `null` for empty input, the original string for non-numeric input,\n * and falls back to a plain number format (optionally suffixed with the code)\n * when the currency code is missing or rejected by `Intl.NumberFormat`.\n *\n * Pass `locale` to format in the application locale (`useLocale()` in client\n * components). Omitting it keeps the runtime default, which varies per machine\n * and is therefore not assertable in tests.\n */\nexport function formatCurrency(\n value: string | number | null | undefined,\n currency?: string | null,\n locale?: string,\n): string | null {\n if (value === null || value === undefined || value === '') return null\n const
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import {\n countDecimalPlaces,\n decimalToString,\n parseDecimal,\n resolveCurrencyDisplayDecimalPlaces,\n type DecimalValue,\n} from '@open-mercato/shared/lib/decimal'\n\nexport type FormatCurrencyOptions = {\n /** Show every decimal of the value instead of capping at the currency precision. */\n showAllDigits?: boolean\n}\n\n/**\n * Format a monetary value with an optional ISO-4217 currency code.\n *\n * Returns `null` for empty input, the original string for non-numeric input,\n * and falls back to a plain number format (optionally suffixed with the code)\n * when the currency code is missing or rejected by `Intl.NumberFormat`.\n *\n * Decimal strings are formatted exactly (no float round-trip) and numbers from\n * their shortest decimal representation (`9007199254740991` keeps every digit),\n * so only the display precision below rounds the value. The value is\n * rounded to the currency precision when it is known: `decimalPlaces` (the\n * currency's configured precision), else the digits `Intl.NumberFormat` displays\n * for the code (`8.1301` USD shows `$8.13`, `1234.56` HUF shows `HUF 1,235`).\n * Shorter values are padded to that precision (`5.000` KWD, `5` JPY). For codes without a known precision (`ETH`) every\n * decimal is kept and shorter values are padded to 2; pass\n * `{ showAllDigits: true }` to keep every decimal for any currency. Without a\n * currency code the value is formatted as a plain number.\n *\n * Pass `locale` to format in the application locale (`useLocale()` in client\n * components). Omitting it keeps the runtime default, which varies per machine\n * and is therefore not assertable in tests.\n */\nexport function formatCurrency(\n value: string | number | null | undefined,\n currency?: string | null,\n locale?: string,\n decimalPlaces?: number | null,\n options?: FormatCurrencyOptions,\n): string | null {\n if (value === null || value === undefined || value === '') return null\n const code = currency && currency.length === 3 ? currency.toUpperCase() : undefined\n const exact = parseDecimal(value)\n if (!exact) return formatUnparsedAmount(value, code, locale)\n const amount = decimalToString(exact) as Intl.StringNumericLiteral\n const digits = resolveFractionDigits(exact, code, decimalPlaces, options?.showAllDigits === true)\n return formatAmount(amount, code, locale, digits)\n}\n\nfunction formatAmount(\n amount: number | Intl.StringNumericLiteral,\n code: string | undefined,\n locale: string | undefined,\n digits: Intl.NumberFormatOptions,\n): string {\n try {\n if (code) {\n return new Intl.NumberFormat(locale, { style: 'currency', currency: code, ...digits }).format(amount)\n }\n } catch {\n // fall through to plain number formatting\n }\n const formatted = new Intl.NumberFormat(locale, digits).format(amount)\n return code ? `${formatted} ${code}` : formatted\n}\n\nfunction formatUnparsedAmount(value: string | number, code: string | undefined, locale: string | undefined): string | null {\n const numeric = typeof value === 'number' ? value : Number(value)\n if (!Number.isFinite(numeric)) return typeof value === 'string' ? value : null\n return formatAmount(numeric, code, locale, {})\n}\n\nconst DEFAULT_CURRENCY_FRACTION_DIGITS = 2\nconst LEGACY_MAX_INTL_FRACTION_DIGITS = 20\nconst EXTENDED_MAX_INTL_FRACTION_DIGITS = 100\n\nlet maxIntlFractionDigits: number | null = null\n\nfunction resolveMaxIntlFractionDigits(): number {\n if (maxIntlFractionDigits !== null) return maxIntlFractionDigits\n try {\n new Intl.NumberFormat('en', { maximumFractionDigits: EXTENDED_MAX_INTL_FRACTION_DIGITS })\n maxIntlFractionDigits = EXTENDED_MAX_INTL_FRACTION_DIGITS\n } catch {\n maxIntlFractionDigits = LEGACY_MAX_INTL_FRACTION_DIGITS\n }\n return maxIntlFractionDigits\n}\n\nfunction resolveFractionDigits(\n exact: DecimalValue,\n code: string | undefined,\n decimalPlaces: number | null | undefined,\n showAllDigits: boolean,\n): Intl.NumberFormatOptions {\n const configured =\n typeof decimalPlaces === 'number' && Number.isInteger(decimalPlaces) && decimalPlaces >= 0 ? decimalPlaces : null\n const displayDigits = code ? resolveCurrencyDisplayDecimalPlaces(code) : null\n if (!code && configured === null && !showAllDigits) return {}\n const currencyDigits = code ? displayDigits ?? DEFAULT_CURRENCY_FRACTION_DIGITS : configured === null ? 0 : DEFAULT_CURRENCY_FRACTION_DIGITS\n const minimum = configured === null ? currencyDigits : Math.min(currencyDigits, configured)\n const precision = configured ?? displayDigits\n const valueDigits = showAllDigits || precision === null ? countDecimalPlaces(exact) : precision\n const maximum = Math.min(Math.max(minimum, valueDigits), resolveMaxIntlFractionDigits())\n return { minimumFractionDigits: Math.min(minimum, maximum), maximumFractionDigits: maximum }\n}\n\n/**\n * Format an ISO date string as a localized short date (e.g. `Jun 9, 2026`).\n *\n * Returns `null` for empty input and echoes the original value back when it is\n * not a parseable date. Pass `locale` to format in the application locale.\n */\nexport function formatDate(value: string | null | undefined, locale?: string): string | null {\n if (!value) return null\n const date = new Date(value)\n if (Number.isNaN(date.getTime())) return value\n return date.toLocaleDateString(locale, { year: 'numeric', month: 'short', day: 'numeric' })\n}\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AA6BA,SAAS,eACd,OACA,UACA,QACA,eACA,SACe;AACf,MAAI,UAAU,QAAQ,UAAU,UAAa,UAAU,GAAI,QAAO;AAClE,QAAM,OAAO,YAAY,SAAS,WAAW,IAAI,SAAS,YAAY,IAAI;AAC1E,QAAM,QAAQ,aAAa,KAAK;AAChC,MAAI,CAAC,MAAO,QAAO,qBAAqB,OAAO,MAAM,MAAM;AAC3D,QAAM,SAAS,gBAAgB,KAAK;AACpC,QAAM,SAAS,sBAAsB,OAAO,MAAM,eAAe,SAAS,kBAAkB,IAAI;AAChG,SAAO,aAAa,QAAQ,MAAM,QAAQ,MAAM;AAClD;AAEA,SAAS,aACP,QACA,MACA,QACA,QACQ;AACR,MAAI;AACF,QAAI,MAAM;AACR,aAAO,IAAI,KAAK,aAAa,QAAQ,EAAE,OAAO,YAAY,UAAU,MAAM,GAAG,OAAO,CAAC,EAAE,OAAO,MAAM;AAAA,IACtG;AAAA,EACF,QAAQ;AAAA,EAER;AACA,QAAM,YAAY,IAAI,KAAK,aAAa,QAAQ,MAAM,EAAE,OAAO,MAAM;AACrE,SAAO,OAAO,GAAG,SAAS,IAAI,IAAI,KAAK;AACzC;AAEA,SAAS,qBAAqB,OAAwB,MAA0B,QAA2C;AACzH,QAAM,UAAU,OAAO,UAAU,WAAW,QAAQ,OAAO,KAAK;AAChE,MAAI,CAAC,OAAO,SAAS,OAAO,EAAG,QAAO,OAAO,UAAU,WAAW,QAAQ;AAC1E,SAAO,aAAa,SAAS,MAAM,QAAQ,CAAC,CAAC;AAC/C;AAEA,MAAM,mCAAmC;AACzC,MAAM,kCAAkC;AACxC,MAAM,oCAAoC;AAE1C,IAAI,wBAAuC;AAE3C,SAAS,+BAAuC;AAC9C,MAAI,0BAA0B,KAAM,QAAO;AAC3C,MAAI;AACF,QAAI,KAAK,aAAa,MAAM,EAAE,uBAAuB,kCAAkC,CAAC;AACxF,4BAAwB;AAAA,EAC1B,QAAQ;AACN,4BAAwB;AAAA,EAC1B;AACA,SAAO;AACT;AAEA,SAAS,sBACP,OACA,MACA,eACA,eAC0B;AAC1B,QAAM,aACJ,OAAO,kBAAkB,YAAY,OAAO,UAAU,aAAa,KAAK,iBAAiB,IAAI,gBAAgB;AAC/G,QAAM,gBAAgB,OAAO,oCAAoC,IAAI,IAAI;AACzE,MAAI,CAAC,QAAQ,eAAe,QAAQ,CAAC,cAAe,QAAO,CAAC;AAC5D,QAAM,iBAAiB,OAAO,iBAAiB,mCAAmC,eAAe,OAAO,IAAI;AAC5G,QAAM,UAAU,eAAe,OAAO,iBAAiB,KAAK,IAAI,gBAAgB,UAAU;AAC1F,QAAM,YAAY,cAAc;AAChC,QAAM,cAAc,iBAAiB,cAAc,OAAO,mBAAmB,KAAK,IAAI;AACtF,QAAM,UAAU,KAAK,IAAI,KAAK,IAAI,SAAS,WAAW,GAAG,6BAA6B,CAAC;AACvF,SAAO,EAAE,uBAAuB,KAAK,IAAI,SAAS,OAAO,GAAG,uBAAuB,QAAQ;AAC7F;AAQO,SAAS,WAAW,OAAkC,QAAgC;AAC3F,MAAI,CAAC,MAAO,QAAO;AACnB,QAAM,OAAO,IAAI,KAAK,KAAK;AAC3B,MAAI,OAAO,MAAM,KAAK,QAAQ,CAAC,EAAG,QAAO;AACzC,SAAO,KAAK,mBAAmB,QAAQ,EAAE,MAAM,WAAW,OAAO,SAAS,KAAK,UAAU,CAAC;AAC5F;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@open-mercato/ui",
|
|
3
|
-
"version": "0.10.1-develop.
|
|
3
|
+
"version": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -156,14 +156,14 @@
|
|
|
156
156
|
"remark-gfm": "^4.0.1"
|
|
157
157
|
},
|
|
158
158
|
"peerDependencies": {
|
|
159
|
-
"@open-mercato/shared": "0.10.1-develop.
|
|
159
|
+
"@open-mercato/shared": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
160
160
|
"react": ">=18.0.0",
|
|
161
161
|
"react-dom": ">=18.0.0",
|
|
162
162
|
"react-is": ">=18.0.0"
|
|
163
163
|
},
|
|
164
164
|
"devDependencies": {
|
|
165
165
|
"@figma/code-connect": "^1.3.4",
|
|
166
|
-
"@open-mercato/shared": "0.10.1-develop.
|
|
166
|
+
"@open-mercato/shared": "0.10.1-develop.8291.1.6ec3aee41c",
|
|
167
167
|
"@testing-library/dom": "^10.4.1",
|
|
168
168
|
"@testing-library/jest-dom": "^7.0.1",
|
|
169
169
|
"@testing-library/react": "^16.3.3",
|
package/src/backend/CrudForm.tsx
CHANGED
|
@@ -96,7 +96,8 @@ import { useInjectionSpotEvents, InjectionSpot, useInjectionWidgets } from './in
|
|
|
96
96
|
import { dispatchBackendMutationError } from './injection/mutationEvents'
|
|
97
97
|
import { VersionHistoryAction } from './version-history/VersionHistoryAction'
|
|
98
98
|
import { parseBooleanWithDefault } from '@open-mercato/shared/lib/boolean'
|
|
99
|
-
import { parseLocaleNumber, resolveLocaleNumberSeparators } from '@open-mercato/shared/lib/number'
|
|
99
|
+
import { parseLocaleDecimal, parseLocaleNumber, resolveLocaleNumberSeparators } from '@open-mercato/shared/lib/number'
|
|
100
|
+
import { addDecimals, decimalToString } from '@open-mercato/shared/lib/decimal'
|
|
100
101
|
import { cn } from '@open-mercato/shared/lib/utils'
|
|
101
102
|
import { createLogger } from '@open-mercato/shared/lib/logger'
|
|
102
103
|
import { getTelemetryRuntime } from '@open-mercato/shared/lib/telemetry/runtime'
|
|
@@ -242,6 +243,7 @@ export type CrudBuiltinField = CrudFieldBase & {
|
|
|
242
243
|
| 'checkbox'
|
|
243
244
|
| 'select'
|
|
244
245
|
| 'number'
|
|
246
|
+
| 'decimal'
|
|
245
247
|
| 'date'
|
|
246
248
|
| 'datepicker'
|
|
247
249
|
| 'datetime-local'
|
|
@@ -4406,15 +4408,17 @@ function NumberInput({
|
|
|
4406
4408
|
placeholder,
|
|
4407
4409
|
autoFocus,
|
|
4408
4410
|
onSubmit,
|
|
4411
|
+
exact = false,
|
|
4409
4412
|
disabled,
|
|
4410
4413
|
readOnly,
|
|
4411
4414
|
}: {
|
|
4412
4415
|
id?: string
|
|
4413
4416
|
value: number | string | null | undefined
|
|
4414
|
-
onChange: (v: number | undefined) => void
|
|
4417
|
+
onChange: (v: number | string | undefined) => void
|
|
4415
4418
|
placeholder?: string
|
|
4416
4419
|
autoFocus?: boolean
|
|
4417
4420
|
onSubmit?: () => void
|
|
4421
|
+
exact?: boolean
|
|
4418
4422
|
disabled?: boolean
|
|
4419
4423
|
readOnly?: boolean
|
|
4420
4424
|
}) {
|
|
@@ -4429,11 +4433,11 @@ function NumberInput({
|
|
|
4429
4433
|
// Users type the separator the surrounding UI displays, which follows the application
|
|
4430
4434
|
// locale — `110,70` under Polish. `Number()` only ever accepted `.` (issue #5552).
|
|
4431
4435
|
const parse = React.useCallback(
|
|
4432
|
-
(raw: string): number | undefined => {
|
|
4436
|
+
(raw: string): number | string | undefined => {
|
|
4433
4437
|
if (raw === '') return undefined
|
|
4434
|
-
return parseLocaleNumber(raw, locale) ?? undefined
|
|
4438
|
+
return (exact ? parseLocaleDecimal(raw, locale) : parseLocaleNumber(raw, locale)) ?? undefined
|
|
4435
4439
|
},
|
|
4436
|
-
[locale],
|
|
4440
|
+
[exact, locale],
|
|
4437
4441
|
)
|
|
4438
4442
|
const commitIfChanged = React.useCallback(() => {
|
|
4439
4443
|
if (local === serializedValue) return
|
|
@@ -4467,14 +4471,17 @@ function NumberInput({
|
|
|
4467
4471
|
// Binary floating point puts 8.2 - 1 at 7.199999999999999; the native spinner
|
|
4468
4472
|
// re-serialized against the step rather than committing that to form state.
|
|
4469
4473
|
const base = parse(local) ?? 0
|
|
4470
|
-
const
|
|
4474
|
+
const step = e.key === 'ArrowUp' ? 1 : -1
|
|
4475
|
+
const stepped = exact
|
|
4476
|
+
? decimalToString(addDecimals(base, step))
|
|
4477
|
+
: Number((Number(base) + step).toFixed(10))
|
|
4471
4478
|
// The box must keep showing the separator the rest of the UI displays, which is
|
|
4472
4479
|
// the application locale's — not the dot String() always emits (issue #5552).
|
|
4473
4480
|
const { decimal } = resolveLocaleNumberSeparators(locale)
|
|
4474
4481
|
setLocal(decimal === '.' ? String(stepped) : String(stepped).replace('.', decimal))
|
|
4475
4482
|
onChange(stepped)
|
|
4476
4483
|
}
|
|
4477
|
-
}, [commitIfChanged, local, locale, onChange, onSubmit, parse])
|
|
4484
|
+
}, [commitIfChanged, exact, local, locale, onChange, onSubmit, parse])
|
|
4478
4485
|
|
|
4479
4486
|
const handleFocus = React.useCallback(() => {
|
|
4480
4487
|
isFocusedRef.current = true
|
|
@@ -4700,7 +4707,8 @@ function supportsWrapperBlurValidation(field: CrudField): boolean {
|
|
|
4700
4707
|
field.type === 'textarea' ||
|
|
4701
4708
|
field.type === 'checkbox' ||
|
|
4702
4709
|
field.type === 'select' ||
|
|
4703
|
-
field.type === 'number'
|
|
4710
|
+
field.type === 'number' ||
|
|
4711
|
+
field.type === 'decimal'
|
|
4704
4712
|
)
|
|
4705
4713
|
}
|
|
4706
4714
|
|
|
@@ -4861,6 +4869,7 @@ const FieldControl = React.memo(function FieldControlImpl({
|
|
|
4861
4869
|
field.type === 'text' ||
|
|
4862
4870
|
field.type === 'password' ||
|
|
4863
4871
|
field.type === 'number' ||
|
|
4872
|
+
field.type === 'decimal' ||
|
|
4864
4873
|
field.type === 'textarea' ||
|
|
4865
4874
|
field.type === 'combobox' ||
|
|
4866
4875
|
(field.type === 'select' && !builtin?.multiple)
|
|
@@ -4913,7 +4922,7 @@ const FieldControl = React.memo(function FieldControlImpl({
|
|
|
4913
4922
|
inputType="password"
|
|
4914
4923
|
/>
|
|
4915
4924
|
)}
|
|
4916
|
-
{field.type === 'number' && (
|
|
4925
|
+
{(field.type === 'number' || field.type === 'decimal') && (
|
|
4917
4926
|
<NumberInput
|
|
4918
4927
|
id={controlId}
|
|
4919
4928
|
value={typeof value === 'number' || typeof value === 'string' ? value : null}
|
|
@@ -4921,6 +4930,7 @@ const FieldControl = React.memo(function FieldControlImpl({
|
|
|
4921
4930
|
onChange={fieldSetValue}
|
|
4922
4931
|
autoFocus={autoFocusField}
|
|
4923
4932
|
onSubmit={onSubmitRequest}
|
|
4933
|
+
exact={field.type === 'decimal'}
|
|
4924
4934
|
disabled={disabled}
|
|
4925
4935
|
readOnly={readOnly}
|
|
4926
4936
|
/>
|
|
@@ -46,6 +46,18 @@ describe('CrudForm accessible field labels (#5360)', () => {
|
|
|
46
46
|
expect(document.activeElement).toBe(input)
|
|
47
47
|
})
|
|
48
48
|
|
|
49
|
+
it('associates a decimal field label with its input via htmlFor/id', () => {
|
|
50
|
+
const fields: CrudField[] = [{ id: 'rate', label: 'Exchange Rate', type: 'decimal' }]
|
|
51
|
+
const { getByLabelText } = renderWithProviders(
|
|
52
|
+
<CrudForm title="Form" fields={fields} initialValues={{ rate: '' }} onSubmit={() => {}} />,
|
|
53
|
+
{ dict },
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
const input = getByLabelText('Exchange Rate') as HTMLInputElement
|
|
57
|
+
expect(input.tagName).toBe('INPUT')
|
|
58
|
+
expect(document.querySelector(`label[for="${input.id}"]`)).not.toBeNull()
|
|
59
|
+
})
|
|
60
|
+
|
|
49
61
|
it('associates a select field label with its trigger via htmlFor/id', () => {
|
|
50
62
|
const fields: CrudField[] = [
|
|
51
63
|
{
|
|
@@ -13,6 +13,68 @@ const PL = 'pl-PL'
|
|
|
13
13
|
const LOCAL_MIDDAY = '2026-06-09T12:00:00'
|
|
14
14
|
|
|
15
15
|
describe('formatCurrency', () => {
|
|
16
|
+
it('formats decimal strings exactly with the currency precision', () => {
|
|
17
|
+
expect(formatCurrency('0.000000000000000001', 'ETH', 'en-US', 18)?.replace(/\s/g, ' ')).toBe('ETH 0.000000000000000001')
|
|
18
|
+
expect(formatCurrency('12345678901234567890.12', 'USD', 'en-US')).toBe('$12,345,678,901,234,567,890.12')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('rounds to the iso digits of a known currency', () => {
|
|
22
|
+
expect(formatCurrency('8.1301', 'USD', EN)).toBe('$8.13')
|
|
23
|
+
expect(formatCurrency('12.3456', 'USD', EN)).toBe('$12.35')
|
|
24
|
+
expect(formatCurrency('5.1234', 'KWD', EN)?.replace(/\s/g, ' ')).toBe('KWD 5.123')
|
|
25
|
+
expect(formatCurrency('1234.5', 'JPY', EN)).toBe('¥1,235')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('rounds to the Intl display digits, not the ISO 4217 minor units', () => {
|
|
29
|
+
expect(formatCurrency('1234.56', 'HUF', EN)?.replace(/\s/g, ' ')).toBe('HUF 1,235')
|
|
30
|
+
expect(formatCurrency('1234', 'HUF', EN)?.replace(/\s/g, ' ')).toBe('HUF 1,234')
|
|
31
|
+
expect(formatCurrency('1234.56', 'HUF', EN, 2)?.replace(/\s/g, ' ')).toBe('HUF 1,234.56')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('rounds to the configured precision before the iso digits', () => {
|
|
35
|
+
expect(formatCurrency('8.1301', 'USD', EN, 4)).toBe('$8.1301')
|
|
36
|
+
expect(formatCurrency('5.25', 'USD', EN, 0)).toBe('$5')
|
|
37
|
+
expect(formatCurrency('5.1234', 'KWD', EN, 2)?.replace(/\s/g, ' ')).toBe('KWD 5.12')
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('shows every digit when the currency precision is unknown', () => {
|
|
41
|
+
expect(formatCurrency('0.000000000000000003', 'ETH', EN)?.replace(/\s/g, ' ')).toBe('ETH 0.000000000000000003')
|
|
42
|
+
expect(formatCurrency('5.123456', 'XYZ', EN)?.replace(/\s/g, ' ')).toBe('XYZ 5.123456')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('shows every digit when the caller opts in', () => {
|
|
46
|
+
expect(formatCurrency('12.3456', 'USD', EN, null, { showAllDigits: true })).toBe('$12.3456')
|
|
47
|
+
expect(formatCurrency('0.000000000000000000000000000001', 'USD', EN, null, { showAllDigits: true })).toBe(
|
|
48
|
+
'$0.000000000000000000000000000001',
|
|
49
|
+
)
|
|
50
|
+
expect(formatCurrency('5', 'KWD', EN, null, { showAllDigits: true })?.replace(/\s/g, ' ')).toBe('KWD 5.000')
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('pads to the ISO digits of the currency', () => {
|
|
54
|
+
expect(formatCurrency('5', 'KWD', EN)?.replace(/\s/g, ' ')).toBe('KWD 5.000')
|
|
55
|
+
expect(formatCurrency('1234', 'JPY', EN)).toBe('¥1,234')
|
|
56
|
+
expect(formatCurrency('5', 'XYZ', EN)?.replace(/\s/g, ' ')).toBe('XYZ 5.00')
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('pads to the configured precision when it is lower than the ISO digits', () => {
|
|
60
|
+
expect(formatCurrency('5', 'USD', EN, 0)).toBe('$5')
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('hides float noise of a number value', () => {
|
|
64
|
+
expect(formatCurrency(0.1 + 0.2, 'USD', EN)).toBe('$0.30')
|
|
65
|
+
expect(formatCurrency(1.5 * 33.33, 'USD', EN)).toBe('$50.00')
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('keeps every significant digit of a large number value', () => {
|
|
69
|
+
expect(formatCurrency(9007199254740991, 'USD', EN)).toBe('$9,007,199,254,740,991.00')
|
|
70
|
+
expect(formatCurrency(99999999999999.98, 'USD', EN)).toBe('$99,999,999,999,999.98')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('formats numbers too large for an exact decimal', () => {
|
|
74
|
+
expect(formatCurrency(1e300, 'USD', EN)).toMatch(/^\$1,000,000/)
|
|
75
|
+
expect(formatCurrency(1e300, null, EN)).toMatch(/^1,000,000/)
|
|
76
|
+
})
|
|
77
|
+
|
|
16
78
|
it('returns null for empty input', () => {
|
|
17
79
|
expect(formatCurrency(null)).toBeNull()
|
|
18
80
|
expect(formatCurrency(undefined)).toBeNull()
|
|
@@ -37,6 +99,8 @@ describe('formatCurrency', () => {
|
|
|
37
99
|
|
|
38
100
|
it('falls back to a plain number without a currency code', () => {
|
|
39
101
|
expect(formatCurrency(1000, null, EN)).toBe('1,000')
|
|
102
|
+
expect(formatCurrency(1.23456, null, EN)).toBe('1.235')
|
|
103
|
+
expect(formatCurrency('1.23456', undefined, EN)).toBe('1.235')
|
|
40
104
|
})
|
|
41
105
|
|
|
42
106
|
it('ignores currency codes that are not three characters', () => {
|
package/src/utils/format.ts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
import {
|
|
2
|
+
countDecimalPlaces,
|
|
3
|
+
decimalToString,
|
|
4
|
+
parseDecimal,
|
|
5
|
+
resolveCurrencyDisplayDecimalPlaces,
|
|
6
|
+
type DecimalValue,
|
|
7
|
+
} from '@open-mercato/shared/lib/decimal'
|
|
8
|
+
|
|
9
|
+
export type FormatCurrencyOptions = {
|
|
10
|
+
/** Show every decimal of the value instead of capping at the currency precision. */
|
|
11
|
+
showAllDigits?: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
1
14
|
/**
|
|
2
15
|
* Format a monetary value with an optional ISO-4217 currency code.
|
|
3
16
|
*
|
|
@@ -5,6 +18,17 @@
|
|
|
5
18
|
* and falls back to a plain number format (optionally suffixed with the code)
|
|
6
19
|
* when the currency code is missing or rejected by `Intl.NumberFormat`.
|
|
7
20
|
*
|
|
21
|
+
* Decimal strings are formatted exactly (no float round-trip) and numbers from
|
|
22
|
+
* their shortest decimal representation (`9007199254740991` keeps every digit),
|
|
23
|
+
* so only the display precision below rounds the value. The value is
|
|
24
|
+
* rounded to the currency precision when it is known: `decimalPlaces` (the
|
|
25
|
+
* currency's configured precision), else the digits `Intl.NumberFormat` displays
|
|
26
|
+
* for the code (`8.1301` USD shows `$8.13`, `1234.56` HUF shows `HUF 1,235`).
|
|
27
|
+
* Shorter values are padded to that precision (`5.000` KWD, `5` JPY). For codes without a known precision (`ETH`) every
|
|
28
|
+
* decimal is kept and shorter values are padded to 2; pass
|
|
29
|
+
* `{ showAllDigits: true }` to keep every decimal for any currency. Without a
|
|
30
|
+
* currency code the value is formatted as a plain number.
|
|
31
|
+
*
|
|
8
32
|
* Pass `locale` to format in the application locale (`useLocale()` in client
|
|
9
33
|
* components). Omitting it keeps the runtime default, which varies per machine
|
|
10
34
|
* and is therefore not assertable in tests.
|
|
@@ -13,24 +37,76 @@ export function formatCurrency(
|
|
|
13
37
|
value: string | number | null | undefined,
|
|
14
38
|
currency?: string | null,
|
|
15
39
|
locale?: string,
|
|
40
|
+
decimalPlaces?: number | null,
|
|
41
|
+
options?: FormatCurrencyOptions,
|
|
16
42
|
): string | null {
|
|
17
43
|
if (value === null || value === undefined || value === '') return null
|
|
18
|
-
const numeric = typeof value === 'number' ? value : Number(value)
|
|
19
|
-
if (!Number.isFinite(numeric)) {
|
|
20
|
-
return typeof value === 'string' ? value : null
|
|
21
|
-
}
|
|
22
44
|
const code = currency && currency.length === 3 ? currency.toUpperCase() : undefined
|
|
45
|
+
const exact = parseDecimal(value)
|
|
46
|
+
if (!exact) return formatUnparsedAmount(value, code, locale)
|
|
47
|
+
const amount = decimalToString(exact) as Intl.StringNumericLiteral
|
|
48
|
+
const digits = resolveFractionDigits(exact, code, decimalPlaces, options?.showAllDigits === true)
|
|
49
|
+
return formatAmount(amount, code, locale, digits)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function formatAmount(
|
|
53
|
+
amount: number | Intl.StringNumericLiteral,
|
|
54
|
+
code: string | undefined,
|
|
55
|
+
locale: string | undefined,
|
|
56
|
+
digits: Intl.NumberFormatOptions,
|
|
57
|
+
): string {
|
|
23
58
|
try {
|
|
24
59
|
if (code) {
|
|
25
|
-
return new Intl.NumberFormat(locale, { style: 'currency', currency: code }).format(
|
|
60
|
+
return new Intl.NumberFormat(locale, { style: 'currency', currency: code, ...digits }).format(amount)
|
|
26
61
|
}
|
|
27
62
|
} catch {
|
|
28
63
|
// fall through to plain number formatting
|
|
29
64
|
}
|
|
30
|
-
const formatted = new Intl.NumberFormat(locale).format(
|
|
65
|
+
const formatted = new Intl.NumberFormat(locale, digits).format(amount)
|
|
31
66
|
return code ? `${formatted} ${code}` : formatted
|
|
32
67
|
}
|
|
33
68
|
|
|
69
|
+
function formatUnparsedAmount(value: string | number, code: string | undefined, locale: string | undefined): string | null {
|
|
70
|
+
const numeric = typeof value === 'number' ? value : Number(value)
|
|
71
|
+
if (!Number.isFinite(numeric)) return typeof value === 'string' ? value : null
|
|
72
|
+
return formatAmount(numeric, code, locale, {})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const DEFAULT_CURRENCY_FRACTION_DIGITS = 2
|
|
76
|
+
const LEGACY_MAX_INTL_FRACTION_DIGITS = 20
|
|
77
|
+
const EXTENDED_MAX_INTL_FRACTION_DIGITS = 100
|
|
78
|
+
|
|
79
|
+
let maxIntlFractionDigits: number | null = null
|
|
80
|
+
|
|
81
|
+
function resolveMaxIntlFractionDigits(): number {
|
|
82
|
+
if (maxIntlFractionDigits !== null) return maxIntlFractionDigits
|
|
83
|
+
try {
|
|
84
|
+
new Intl.NumberFormat('en', { maximumFractionDigits: EXTENDED_MAX_INTL_FRACTION_DIGITS })
|
|
85
|
+
maxIntlFractionDigits = EXTENDED_MAX_INTL_FRACTION_DIGITS
|
|
86
|
+
} catch {
|
|
87
|
+
maxIntlFractionDigits = LEGACY_MAX_INTL_FRACTION_DIGITS
|
|
88
|
+
}
|
|
89
|
+
return maxIntlFractionDigits
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function resolveFractionDigits(
|
|
93
|
+
exact: DecimalValue,
|
|
94
|
+
code: string | undefined,
|
|
95
|
+
decimalPlaces: number | null | undefined,
|
|
96
|
+
showAllDigits: boolean,
|
|
97
|
+
): Intl.NumberFormatOptions {
|
|
98
|
+
const configured =
|
|
99
|
+
typeof decimalPlaces === 'number' && Number.isInteger(decimalPlaces) && decimalPlaces >= 0 ? decimalPlaces : null
|
|
100
|
+
const displayDigits = code ? resolveCurrencyDisplayDecimalPlaces(code) : null
|
|
101
|
+
if (!code && configured === null && !showAllDigits) return {}
|
|
102
|
+
const currencyDigits = code ? displayDigits ?? DEFAULT_CURRENCY_FRACTION_DIGITS : configured === null ? 0 : DEFAULT_CURRENCY_FRACTION_DIGITS
|
|
103
|
+
const minimum = configured === null ? currencyDigits : Math.min(currencyDigits, configured)
|
|
104
|
+
const precision = configured ?? displayDigits
|
|
105
|
+
const valueDigits = showAllDigits || precision === null ? countDecimalPlaces(exact) : precision
|
|
106
|
+
const maximum = Math.min(Math.max(minimum, valueDigits), resolveMaxIntlFractionDigits())
|
|
107
|
+
return { minimumFractionDigits: Math.min(minimum, maximum), maximumFractionDigits: maximum }
|
|
108
|
+
}
|
|
109
|
+
|
|
34
110
|
/**
|
|
35
111
|
* Format an ISO date string as a localized short date (e.g. `Jun 9, 2026`).
|
|
36
112
|
*
|