@janbox/storefront-ui 3.0.2 → 3.0.4
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/hooks/use-formatter/index.d.ts +5 -3
- package/dist/hooks/use-formatter/index.js +21 -7
- package/dist/lib/count-up/README.md +1 -1
- package/dist/lib/count-up/count-up.js +1 -1
- package/dist/utils/common.d.ts +2 -2
- package/dist/utils/index.d.ts +26 -6
- package/dist/utils/index.js +26 -6
- package/package.json +1 -1
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { FormatPriceOptions, FormatDateTimeOptions, FormatNumberOptions } from '../../utils';
|
|
1
|
+
import { FormatPriceOptions, FormatDateTimeOptions, FormatNumberOptions, GetCurrencyDisplayNameOptions, GetLanguageDisplayNameOptions } from '../../utils';
|
|
2
2
|
import { SetOptional } from 'type-fest';
|
|
3
3
|
export declare const useFormatter: () => {
|
|
4
4
|
formatNumber: (value: number, options?: Partial<FormatNumberOptions>) => string;
|
|
5
|
-
formatDateTime: (value: Date | string | number, options
|
|
6
|
-
formatPrice: (value: number, options: SetOptional<FormatPriceOptions, "
|
|
5
|
+
formatDateTime: (value: Date | string | number, options?: SetOptional<FormatDateTimeOptions, "locale">) => string;
|
|
6
|
+
formatPrice: (value: number, options: SetOptional<FormatPriceOptions, "locale">) => string;
|
|
7
|
+
getLanguageDisplayName: (code: string, options?: GetLanguageDisplayNameOptions) => string;
|
|
8
|
+
getCurrencyDisplayName: (code: string, options?: SetOptional<GetCurrencyDisplayNameOptions, "locale">) => string;
|
|
7
9
|
};
|
|
@@ -1,29 +1,41 @@
|
|
|
1
|
-
import { formatNumber, formatDateTime, formatPrice } from "../../utils/index.js";
|
|
1
|
+
import { formatNumber, formatDateTime, formatPrice, getLanguageDisplayName, getCurrencyDisplayName } from "../../utils/index.js";
|
|
2
2
|
import { useCallback, useMemo } from "react";
|
|
3
3
|
import { useTranslation } from "../../i18n/index.js";
|
|
4
4
|
const useFormatter = () => {
|
|
5
5
|
const { locale } = useTranslation();
|
|
6
6
|
const formatNumberFn = useCallback(
|
|
7
7
|
(value, options) => {
|
|
8
|
-
return formatNumber(value, {
|
|
8
|
+
return formatNumber(value, { ...options, locale: (options == null ? void 0 : options.locale) ?? locale });
|
|
9
9
|
},
|
|
10
10
|
[locale]
|
|
11
11
|
);
|
|
12
12
|
const formatDateTimeFn = useCallback(
|
|
13
|
-
(value, options) => {
|
|
13
|
+
(value, options = {}) => {
|
|
14
14
|
const hasStyleOption = options.dateStyle !== void 0 || options.timeStyle !== void 0;
|
|
15
15
|
const defaults = hasStyleOption ? {} : { day: "2-digit", month: "2-digit", year: "numeric", hour: "numeric", minute: "2-digit", hour12: false };
|
|
16
16
|
return formatDateTime(value, {
|
|
17
17
|
...defaults,
|
|
18
18
|
...options,
|
|
19
|
-
|
|
19
|
+
locale: options.locale ?? locale
|
|
20
20
|
});
|
|
21
21
|
},
|
|
22
22
|
[locale]
|
|
23
23
|
);
|
|
24
24
|
const formatPriceFn = useCallback(
|
|
25
25
|
(value, options) => {
|
|
26
|
-
return formatPrice(value, { ...options,
|
|
26
|
+
return formatPrice(value, { ...options, locale: options.locale ?? locale });
|
|
27
|
+
},
|
|
28
|
+
[locale]
|
|
29
|
+
);
|
|
30
|
+
const getLanguageDisplayNameFn = useCallback(
|
|
31
|
+
(code, options = {}) => {
|
|
32
|
+
return getLanguageDisplayName(code, { ...options, locale: (options == null ? void 0 : options.locale) ?? locale });
|
|
33
|
+
},
|
|
34
|
+
[locale]
|
|
35
|
+
);
|
|
36
|
+
const getCurrencyDisplayNameFn = useCallback(
|
|
37
|
+
(code, options = {}) => {
|
|
38
|
+
return getCurrencyDisplayName(code, { ...options, locale: (options == null ? void 0 : options.locale) ?? locale });
|
|
27
39
|
},
|
|
28
40
|
[locale]
|
|
29
41
|
);
|
|
@@ -31,9 +43,11 @@ const useFormatter = () => {
|
|
|
31
43
|
() => ({
|
|
32
44
|
formatNumber: formatNumberFn,
|
|
33
45
|
formatDateTime: formatDateTimeFn,
|
|
34
|
-
formatPrice: formatPriceFn
|
|
46
|
+
formatPrice: formatPriceFn,
|
|
47
|
+
getLanguageDisplayName: getLanguageDisplayNameFn,
|
|
48
|
+
getCurrencyDisplayName: getCurrencyDisplayNameFn
|
|
35
49
|
}),
|
|
36
|
-
[formatNumberFn, formatDateTimeFn, formatPriceFn]
|
|
50
|
+
[formatNumberFn, formatDateTimeFn, formatPriceFn, getLanguageDisplayNameFn, getCurrencyDisplayNameFn]
|
|
37
51
|
);
|
|
38
52
|
};
|
|
39
53
|
export {
|
|
@@ -15,7 +15,7 @@ import { CountUp } from '@janbox/storefront-ui';
|
|
|
15
15
|
| `to*` | `number` | - | Số đích cần đếm tới |
|
|
16
16
|
| `from` | `number` | 0 | Số bắt đầu đếm |
|
|
17
17
|
| `duration` | `number` | 2 | Thời gian animation tính bằng giây |
|
|
18
|
-
| `formatNumberOptions` | `FormatNumberOptions` | - | Tùy chọn format số (mở rộng từ `Intl.NumberFormatOptions` với thuộc tính `
|
|
18
|
+
| `formatNumberOptions` | `FormatNumberOptions` | - | Tùy chọn format số (mở rộng từ `Intl.NumberFormatOptions` với thuộc tính `locale`). Kiểm soát số chữ số thập phân, format tiền tệ và các tùy chọn hiển thị số theo locale |
|
|
19
19
|
| `ref` | `React.Ref<HTMLSpanElement>` | - | Ref tới phần tử span bên dưới |
|
|
20
20
|
|
|
21
21
|
*Prop bắt buộc
|
package/dist/utils/common.d.ts
CHANGED
|
@@ -8,9 +8,9 @@ export declare const normalizeSearchText: (str?: string, options?: {
|
|
|
8
8
|
}) => string;
|
|
9
9
|
export declare const matchSearch: (text: string | string[], searchText: string, options?: Parameters<typeof normalizeSearchText>[1]) => boolean;
|
|
10
10
|
export type FormatNumberOptions = Intl.NumberFormatOptions & {
|
|
11
|
-
|
|
11
|
+
locale: string;
|
|
12
12
|
};
|
|
13
|
-
export declare const formatNumber: (value: number, {
|
|
13
|
+
export declare const formatNumber: (value: number, { locale, ...options }: FormatNumberOptions) => string;
|
|
14
14
|
export declare const getDecimalDigits: (value: number) => number;
|
|
15
15
|
export declare const parseNumber: (text?: string | number, type?: "int" | "float", separator?: string) => number | undefined;
|
|
16
16
|
export declare function omitEmpty<T extends object>(obj?: T): T;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -39,14 +39,34 @@ export declare const getResponsiveProp: <O extends object, P extends Paths<O>, R
|
|
|
39
39
|
condition?: (value?: R) => boolean;
|
|
40
40
|
}) => R;
|
|
41
41
|
export type FormatNumberOptions = Intl.NumberFormatOptions & {
|
|
42
|
-
|
|
42
|
+
locale: string;
|
|
43
43
|
};
|
|
44
|
-
export declare const formatNumber: (value: number, {
|
|
44
|
+
export declare const formatNumber: (value: number, { locale, ...options }: FormatNumberOptions) => string;
|
|
45
45
|
export type FormatPriceOptions = SetRequired<Intl.NumberFormatOptions, 'currency'> & {
|
|
46
|
-
|
|
46
|
+
locale: string;
|
|
47
47
|
};
|
|
48
|
-
export declare const formatPrice: (value: number, {
|
|
48
|
+
export declare const formatPrice: (value: number, { locale, ...options }: FormatPriceOptions) => string;
|
|
49
49
|
export type FormatDateTimeOptions = Intl.DateTimeFormatOptions & {
|
|
50
|
-
|
|
50
|
+
locale: string;
|
|
51
51
|
};
|
|
52
|
-
export
|
|
52
|
+
export type GetLanguageDisplayNameOptions = Omit<Intl.DisplayNamesOptions, 'type'> & {
|
|
53
|
+
/** Ngôn ngữ hiển thị tên, mặc định là chính code đó (self-naming) */
|
|
54
|
+
locale?: string;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Trả về tên đầy đủ của ngôn ngữ dựa trên language code.
|
|
58
|
+
* VD: getLanguageDisplayName('vi') → 'Tiếng Việt'
|
|
59
|
+
* getLanguageDisplayName('en', { locale: 'vi' }) → 'Tiếng Anh'
|
|
60
|
+
*/
|
|
61
|
+
export declare const getLanguageDisplayName: (code: string, options?: GetLanguageDisplayNameOptions) => string;
|
|
62
|
+
/**
|
|
63
|
+
* Trả về tên đầy đủ của tiền tệ dựa trên currency code.
|
|
64
|
+
* VD: getCurrencyDisplayName('VND', { locale: 'vi' }) → 'Đồng Việt Nam'
|
|
65
|
+
* getCurrencyDisplayName('USD', { locale: 'en' }) → 'US Dollar'
|
|
66
|
+
*/
|
|
67
|
+
export type GetCurrencyDisplayNameOptions = Omit<Intl.DisplayNamesOptions, 'type'> & {
|
|
68
|
+
/** Ngôn ngữ hiển thị tên */
|
|
69
|
+
locale: string;
|
|
70
|
+
};
|
|
71
|
+
export declare const getCurrencyDisplayName: (code: string, options: GetCurrencyDisplayNameOptions) => string;
|
|
72
|
+
export declare const formatDateTime: (value: Date | string | number, { locale, ...options }: FormatDateTimeOptions) => string;
|
package/dist/utils/index.js
CHANGED
|
@@ -167,9 +167,9 @@ const getResponsiveProp = (record, field, screen, options) => {
|
|
|
167
167
|
});
|
|
168
168
|
return getByScreen(smallerScreen ?? screen);
|
|
169
169
|
};
|
|
170
|
-
const formatNumber = (value, {
|
|
170
|
+
const formatNumber = (value, { locale, ...options }) => {
|
|
171
171
|
try {
|
|
172
|
-
return new Intl.NumberFormat(
|
|
172
|
+
return new Intl.NumberFormat(locale, {
|
|
173
173
|
minimumFractionDigits: 0,
|
|
174
174
|
...options
|
|
175
175
|
}).format(value);
|
|
@@ -177,9 +177,9 @@ const formatNumber = (value, { lng, ...options }) => {
|
|
|
177
177
|
return value.toString();
|
|
178
178
|
}
|
|
179
179
|
};
|
|
180
|
-
const formatPrice = (value, {
|
|
180
|
+
const formatPrice = (value, { locale, ...options }) => {
|
|
181
181
|
try {
|
|
182
|
-
return new Intl.NumberFormat(
|
|
182
|
+
return new Intl.NumberFormat(locale, {
|
|
183
183
|
style: "currency",
|
|
184
184
|
minimumFractionDigits: 0,
|
|
185
185
|
currencyDisplay: "narrowSymbol",
|
|
@@ -189,7 +189,25 @@ const formatPrice = (value, { lng, ...options }) => {
|
|
|
189
189
|
return [value, options.currency].filter(Boolean).join(" ");
|
|
190
190
|
}
|
|
191
191
|
};
|
|
192
|
-
const
|
|
192
|
+
const getLanguageDisplayName = (code, options) => {
|
|
193
|
+
const { locale, ...rest } = options ?? {};
|
|
194
|
+
try {
|
|
195
|
+
const displayNames = new Intl.DisplayNames([locale ?? code], { ...rest, type: "language" });
|
|
196
|
+
return displayNames.of(code) ?? code;
|
|
197
|
+
} catch {
|
|
198
|
+
return code;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
const getCurrencyDisplayName = (code, options) => {
|
|
202
|
+
const { locale, ...rest } = options;
|
|
203
|
+
try {
|
|
204
|
+
const displayNames = new Intl.DisplayNames([locale], { ...rest, type: "currency" });
|
|
205
|
+
return displayNames.of(code.toUpperCase()) ?? code;
|
|
206
|
+
} catch {
|
|
207
|
+
return code;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
const formatDateTime = (value, { locale, ...options }) => {
|
|
193
211
|
const dateObject = (() => {
|
|
194
212
|
if (isString(value)) {
|
|
195
213
|
const hasTimezone = /([zZ]|[+-]\d{2}:\d{2})$/.test(value);
|
|
@@ -201,7 +219,7 @@ const formatDateTime = (value, { lng, ...options }) => {
|
|
|
201
219
|
return value;
|
|
202
220
|
})();
|
|
203
221
|
try {
|
|
204
|
-
return new Intl.DateTimeFormat(
|
|
222
|
+
return new Intl.DateTimeFormat(locale, {
|
|
205
223
|
day: "2-digit",
|
|
206
224
|
month: "2-digit",
|
|
207
225
|
year: "numeric",
|
|
@@ -229,8 +247,10 @@ export {
|
|
|
229
247
|
formatNumber,
|
|
230
248
|
formatPrice,
|
|
231
249
|
getColorShadesByVariant,
|
|
250
|
+
getCurrencyDisplayName,
|
|
232
251
|
getDecimalDigits,
|
|
233
252
|
getEnumValues,
|
|
253
|
+
getLanguageDisplayName,
|
|
234
254
|
getResponsiveProp,
|
|
235
255
|
getSizeVariantState,
|
|
236
256
|
matchSearch,
|