@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.
@@ -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: SetOptional<FormatDateTimeOptions, "lng">) => string;
6
- formatPrice: (value: number, options: SetOptional<FormatPriceOptions, "lng">) => string;
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, { lng: locale, ...options });
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
- lng: options.lng ?? locale
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, lng: options.lng ?? locale });
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 `lng`). 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 |
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
@@ -15,7 +15,7 @@ const CountUp = ({ from = 0, to, duration = 2, formatNumberOptions, ref, ...prop
15
15
  const rounded = useTransform(
16
16
  count,
17
17
  (latest) => formatNumber(latest, {
18
- lng: locale,
18
+ locale,
19
19
  ...formatNumberOptions,
20
20
  maximumFractionDigits
21
21
  })
@@ -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
- lng: string;
11
+ locale: string;
12
12
  };
13
- export declare const formatNumber: (value: number, { lng, ...options }: FormatNumberOptions) => string;
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;
@@ -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
- lng: string;
42
+ locale: string;
43
43
  };
44
- export declare const formatNumber: (value: number, { lng, ...options }: FormatNumberOptions) => string;
44
+ export declare const formatNumber: (value: number, { locale, ...options }: FormatNumberOptions) => string;
45
45
  export type FormatPriceOptions = SetRequired<Intl.NumberFormatOptions, 'currency'> & {
46
- lng: string;
46
+ locale: string;
47
47
  };
48
- export declare const formatPrice: (value: number, { lng, ...options }: FormatPriceOptions) => string;
48
+ export declare const formatPrice: (value: number, { locale, ...options }: FormatPriceOptions) => string;
49
49
  export type FormatDateTimeOptions = Intl.DateTimeFormatOptions & {
50
- lng: string;
50
+ locale: string;
51
51
  };
52
- export declare const formatDateTime: (value: Date | string | number, { lng, ...options }: FormatDateTimeOptions) => string;
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;
@@ -167,9 +167,9 @@ const getResponsiveProp = (record, field, screen, options) => {
167
167
  });
168
168
  return getByScreen(smallerScreen ?? screen);
169
169
  };
170
- const formatNumber = (value, { lng, ...options }) => {
170
+ const formatNumber = (value, { locale, ...options }) => {
171
171
  try {
172
- return new Intl.NumberFormat(lng, {
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, { lng, ...options }) => {
180
+ const formatPrice = (value, { locale, ...options }) => {
181
181
  try {
182
- return new Intl.NumberFormat(lng, {
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 formatDateTime = (value, { lng, ...options }) => {
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(lng, {
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,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@janbox/storefront-ui",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
4
4
  "description": "Storefront UI component library for Janbox",
5
5
  "author": "Janbox",
6
6
  "keywords": [