@sincpro/web-ui 2.22.0 → 2.23.0
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/primitives/value/Price.d.ts +4 -2
- package/dist/primitives/value/Price.js +1 -1
- package/dist/primitives/value/format.d.ts +5 -1
- package/dist/primitives/value/format.js +5 -1
- package/dist/primitives/value/priceFormat.d.ts +4 -3
- package/dist/primitives/value/priceFormat.js +9 -4
- package/package.json +1 -1
|
@@ -4,12 +4,14 @@ declare const SIZE: {
|
|
|
4
4
|
readonly lg: "text-display";
|
|
5
5
|
};
|
|
6
6
|
export type PriceProps = {
|
|
7
|
-
|
|
7
|
+
/** A number, or a decimal as its text (`"120.50"`, what a backend sends for money): written as
|
|
8
|
+
* it is, never through a float. */
|
|
9
|
+
value: number | string;
|
|
8
10
|
/** ISO 4217 — `BOB`, `USD`. Without it the number goes with two decimals and no symbol. */
|
|
9
11
|
currency?: string;
|
|
10
12
|
/** The price before: drawn struck through, beside the discount it makes. Not drawn when
|
|
11
13
|
* it is not higher — a «before» that is not more is not a discount. */
|
|
12
|
-
compareAt?: number;
|
|
14
|
+
compareAt?: number | string;
|
|
13
15
|
/** What the price is for — «mes», «kg», «persona» — written after it: «Bs 120,00 / mes». */
|
|
14
16
|
per?: string;
|
|
15
17
|
size?: keyof typeof SIZE;
|
|
@@ -28,5 +28,5 @@ export function Price({ value, currency, compareAt, per, size = "md", format })
|
|
|
28
28
|
const write = (n) => formatPrice(n, { currency, format }, copy.locale);
|
|
29
29
|
const off = discountPercent(value, compareAt);
|
|
30
30
|
const before = off === undefined ? undefined : compareAt;
|
|
31
|
-
return (_jsxs("span", { "data-ui": "price", "data-size": size, className: "inline-flex min-w-0 flex-wrap items-baseline gap-x-2 gap-y-0.5", children: [_jsxs("span", { className: `font-semibold whitespace-nowrap text-ink tabular-nums ${SIZE[size]}`, children: [before === undefined ? null : _jsxs("span", { className: "sr-only", children: [copy.price.now, " "] }), value === 0 ? copy.price.free : write(value), per === undefined ? null : (_jsxs("span", { className: "text-meta font-normal text-muted", children: [_jsx("span", { "aria-hidden": true, children: " / " }), _jsxs("span", { className: "sr-only", children: [" ", copy.price.per, " "] }), per] }))] }), before === undefined || off === undefined ? null : (_jsxs(_Fragment, { children: [_jsxs("del", { className: "text-meta whitespace-nowrap text-muted tabular-nums", "data-ui": "price.before", children: [_jsxs("span", { className: "sr-only", children: [copy.price.before, " "] }), write(before)] }), _jsx(Tag, { tone: "good", title: fillCopy(copy.price.off, { percent: String(off) }), children: formatWith(-off / 100, { style: "percent", signDisplay: "always" }, copy.locale) })] }))] }));
|
|
31
|
+
return (_jsxs("span", { "data-ui": "price", "data-size": size, className: "inline-flex min-w-0 flex-wrap items-baseline gap-x-2 gap-y-0.5", children: [_jsxs("span", { className: `font-semibold whitespace-nowrap text-ink tabular-nums ${SIZE[size]}`, children: [before === undefined ? null : _jsxs("span", { className: "sr-only", children: [copy.price.now, " "] }), Number(value) === 0 ? copy.price.free : write(value), per === undefined ? null : (_jsxs("span", { className: "text-meta font-normal text-muted", children: [_jsx("span", { "aria-hidden": true, children: " / " }), _jsxs("span", { className: "sr-only", children: [" ", copy.price.per, " "] }), per] }))] }), before === undefined || off === undefined ? null : (_jsxs(_Fragment, { children: [_jsxs("del", { className: "text-meta whitespace-nowrap text-muted tabular-nums", "data-ui": "price.before", children: [_jsxs("span", { className: "sr-only", children: [copy.price.before, " "] }), write(before)] }), _jsx(Tag, { tone: "good", title: fillCopy(copy.price.off, { percent: String(off) }), children: formatWith(-off / 100, { style: "percent", signDisplay: "always" }, copy.locale) })] }))] }));
|
|
32
32
|
}
|
|
@@ -5,7 +5,11 @@ export declare const formatDistance: (value: number, locale?: string) => string;
|
|
|
5
5
|
/** A number written with any `Intl.NumberFormatOptions` — a currency, a percentage, fixed
|
|
6
6
|
* decimals — in the session locale. The same options `NumberInput` takes, so a value reads
|
|
7
7
|
* the same in its form and in the cell or the figure that shows it elsewhere. */
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* A number in the locale — or a decimal as its text (`"1299.50"`, what a backend sends for money):
|
|
10
|
+
* `Intl` writes the text as it is, every digit, never through a float.
|
|
11
|
+
*/
|
|
12
|
+
export declare const formatWith: (value: number | string, options: Intl.NumberFormatOptions, locale?: string) => string;
|
|
9
13
|
/** The day alone, in digits and in the locale's order. Exported for `DateValue`'s `date`
|
|
10
14
|
* format; not public. */
|
|
11
15
|
export declare function dateFormat(locale: string): Intl.DateTimeFormat;
|
|
@@ -31,7 +31,11 @@ export const formatDistance = (value, locale = currentCopyLocale()) => distanceF
|
|
|
31
31
|
/** A number written with any `Intl.NumberFormatOptions` — a currency, a percentage, fixed
|
|
32
32
|
* decimals — in the session locale. The same options `NumberInput` takes, so a value reads
|
|
33
33
|
* the same in its form and in the cell or the figure that shows it elsewhere. */
|
|
34
|
-
|
|
34
|
+
/**
|
|
35
|
+
* A number in the locale — or a decimal as its text (`"1299.50"`, what a backend sends for money):
|
|
36
|
+
* `Intl` writes the text as it is, every digit, never through a float.
|
|
37
|
+
*/
|
|
38
|
+
export const formatWith = (value, options, locale = currentCopyLocale()) => new Intl.NumberFormat(locale, options).format(typeof value === "number" ? value : value);
|
|
35
39
|
/* ── Dates — see `DateValue.tsx` for why the relative stops at a week ── */
|
|
36
40
|
function relativeFormat(locale) {
|
|
37
41
|
return new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Context: rounded DOWN, never to the nearest: 80 for 99 is 19.2 % — «−20 %» would promise
|
|
5
5
|
* more than the shelf gives, and a price is a promise. */
|
|
6
|
-
export declare function discountPercent(
|
|
6
|
+
export declare function discountPercent(amount: number | string, before: number | string | undefined): number | undefined;
|
|
7
7
|
/** A price written in the session's locale: with its currency when there is one («Bs 80,00»),
|
|
8
|
-
* with two decimals when there is none. `format` overrides either.
|
|
9
|
-
|
|
8
|
+
* with two decimals when there is none. `format` overrides either. A decimal's text
|
|
9
|
+
* (`"80.50"`) is written as it is, never through a float. */
|
|
10
|
+
export declare function formatPrice(value: number | string, { currency, format }?: {
|
|
10
11
|
currency?: string;
|
|
11
12
|
format?: Intl.NumberFormatOptions;
|
|
12
13
|
}, locale?: string): string;
|
|
@@ -5,15 +5,20 @@ import { formatWith } from "./format.js";
|
|
|
5
5
|
*
|
|
6
6
|
* Context: rounded DOWN, never to the nearest: 80 for 99 is 19.2 % — «−20 %» would promise
|
|
7
7
|
* more than the shelf gives, and a price is a promise. */
|
|
8
|
-
export function discountPercent(
|
|
9
|
-
if (
|
|
8
|
+
export function discountPercent(amount, before) {
|
|
9
|
+
if (before === undefined)
|
|
10
|
+
return undefined;
|
|
11
|
+
// A ratio to show, never an amount to keep: a float is enough for a whole percentage.
|
|
12
|
+
const value = Number(amount);
|
|
13
|
+
const compareAt = Number(before);
|
|
14
|
+
if (!(compareAt > 0) || !(value < compareAt) || value < 0)
|
|
10
15
|
return undefined;
|
|
11
|
-
}
|
|
12
16
|
const off = Math.floor(((compareAt - value) / compareAt) * 100);
|
|
13
17
|
return off > 0 ? off : undefined;
|
|
14
18
|
}
|
|
15
19
|
/** A price written in the session's locale: with its currency when there is one («Bs 80,00»),
|
|
16
|
-
* with two decimals when there is none. `format` overrides either.
|
|
20
|
+
* with two decimals when there is none. `format` overrides either. A decimal's text
|
|
21
|
+
* (`"80.50"`) is written as it is, never through a float. */
|
|
17
22
|
export function formatPrice(value, { currency, format } = {}, locale = currentCopyLocale()) {
|
|
18
23
|
const options = currency === undefined
|
|
19
24
|
? { minimumFractionDigits: 2, maximumFractionDigits: 2, ...format }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sincpro/web-ui",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.23.0",
|
|
4
4
|
"description": "An enterprise web design system for business software — ERPs, back offices, management tools — in React and Tailwind: tokens, primitives, components and the two views every management screen is made of.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|