react-intl 10.1.0 → 10.1.2

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/server.d.ts CHANGED
@@ -1,10 +1,34 @@
1
- import { type MessageDescriptor } from "@formatjs/intl";
2
- export { createIntlCache, type IntlCache, type MessageDescriptor } from "@formatjs/intl";
3
- export { createIntl } from "./src/components/createIntl.js";
4
- export type { IntlConfig, IntlShape, ResolvedIntlConfig } from "./src/types.js";
5
- export declare function defineMessages<
6
- K extends keyof any,
7
- T = MessageDescriptor,
8
- U extends Record<K, T> = Record<K, T>
9
- >(msgs: U): U;
10
- export declare function defineMessage<T extends MessageDescriptor>(msg: T): T;
1
+ import { CreateIntlFn, Formatters, IntlCache, IntlFormatters, MessageDescriptor, MessageDescriptor as MessageDescriptor$1, ResolvedIntlConfig as ResolvedIntlConfig$1, createIntlCache } from "@formatjs/intl";
2
+ import { FormatXMLElementFn, Options, PrimitiveType } from "intl-messageformat";
3
+ import * as React from "react";
4
+
5
+ //#region packages/react-intl/utils.d.ts
6
+ type DefaultIntlConfig = Pick<ResolvedIntlConfig, "fallbackOnEmptyString" | "formats" | "messages" | "timeZone" | "textComponent" | "defaultLocale" | "defaultFormats" | "onError">;
7
+ declare const DEFAULT_INTL_CONFIG$1: DefaultIntlConfig;
8
+ //#endregion
9
+ //#region packages/react-intl/types.d.ts
10
+ type IntlConfig = Omit<ResolvedIntlConfig, keyof typeof DEFAULT_INTL_CONFIG$1> & Partial<typeof DEFAULT_INTL_CONFIG$1>;
11
+ interface ResolvedIntlConfig extends ResolvedIntlConfig$1<React.ReactNode> {
12
+ textComponent?: React.ComponentType | keyof React.JSX.IntrinsicElements;
13
+ wrapRichTextChunksInFragment?: boolean;
14
+ }
15
+ interface IntlShape extends ResolvedIntlConfig, IntlFormatters<React.ReactNode> {
16
+ formatMessage(this: void, descriptor: MessageDescriptor$1, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: Options): string;
17
+ formatMessage(this: void, descriptor: MessageDescriptor$1, values?: Record<string, React.ReactNode | PrimitiveType | FormatXMLElementFn<string, React.ReactNode>>, opts?: Options): Array<React.ReactNode>;
18
+ formatters: Formatters;
19
+ }
20
+ //#endregion
21
+ //#region packages/react-intl/components/createIntl.d.ts
22
+ /**
23
+ * Create intl object
24
+ * @param config intl config
25
+ * @param cache cache for formatter instances to prevent memory leak
26
+ */
27
+ declare const createIntl: CreateIntlFn<React.ReactNode, IntlConfig, IntlShape>;
28
+ //#endregion
29
+ //#region packages/react-intl/server.d.ts
30
+ declare function defineMessages<K extends keyof any, T = MessageDescriptor$1, U extends Record<K, T> = Record<K, T>>(msgs: U): U;
31
+ declare function defineMessage<T extends MessageDescriptor$1>(msg: T): T;
32
+ //#endregion
33
+ export { type IntlCache, type IntlConfig, type IntlShape, type MessageDescriptor, type ResolvedIntlConfig, createIntl, createIntlCache, defineMessage, defineMessages };
34
+ //# sourceMappingURL=server.d.ts.map
package/server.js CHANGED
@@ -1,10 +1,92 @@
1
- import "@formatjs/intl";
2
- export { createIntlCache } from "@formatjs/intl";
3
- export { createIntl } from "./src/components/createIntl.js";
4
- // Identity functions duplicated here to avoid importing from "use client" index
5
- export function defineMessages(msgs) {
1
+ import { DEFAULT_INTL_CONFIG, createIntl as createIntl$1, createIntlCache, formatMessage } from "@formatjs/intl";
2
+ import { isFormatXMLElementFn } from "intl-messageformat";
3
+ import * as React from "react";
4
+ import { jsx } from "react/jsx-runtime";
5
+ //#region packages/react-intl/utils.tsx
6
+ const DEFAULT_INTL_CONFIG$1 = {
7
+ ...DEFAULT_INTL_CONFIG,
8
+ textComponent: React.Fragment
9
+ };
10
+ /**
11
+ * Builds an array of {@link React.ReactNode}s with index-based keys, similar to
12
+ * {@link React.Children.toArray}. However, this function tells React that it
13
+ * was intentional, so they won't produce a bunch of warnings about it.
14
+ *
15
+ * React doesn't recommend doing this because it makes reordering inefficient,
16
+ * but we mostly need this for message chunks, which don't tend to reorder to
17
+ * begin with.
18
+ *
19
+ */
20
+ const toKeyedReactNodeArray = (children) => {
21
+ return React.Children.toArray(children).map((child, index) => {
22
+ if (React.isValidElement(child)) return /* @__PURE__ */ jsx(React.Fragment, { children: child }, index);
23
+ return child;
24
+ });
25
+ };
26
+ /**
27
+ * Takes a `formatXMLElementFn`, and composes it in function, which passes
28
+ * argument `parts` through, assigning unique key to each part, to prevent
29
+ * "Each child in a list should have a unique "key"" React error.
30
+ * @param formatXMLElementFn
31
+ */
32
+ function assignUniqueKeysToParts(formatXMLElementFn) {
33
+ return function(parts) {
34
+ return formatXMLElementFn(toKeyedReactNodeArray(parts));
35
+ };
36
+ }
37
+ //#endregion
38
+ //#region packages/react-intl/components/createIntl.ts
39
+ function assignUniqueKeysToFormatXMLElementFnArgument(values) {
40
+ if (!values) return values;
41
+ return Object.keys(values).reduce((acc, k) => {
42
+ const v = values[k];
43
+ acc[k] = isFormatXMLElementFn(v) ? assignUniqueKeysToParts(v) : v;
44
+ return acc;
45
+ }, {});
46
+ }
47
+ const formatMessage$1 = (config, formatters, descriptor, rawValues, ...rest) => {
48
+ const chunks = formatMessage(config, formatters, descriptor, assignUniqueKeysToFormatXMLElementFnArgument(rawValues), ...rest);
49
+ if (Array.isArray(chunks)) return toKeyedReactNodeArray(chunks);
50
+ return chunks;
51
+ };
52
+ /**
53
+ * Create intl object
54
+ * @param config intl config
55
+ * @param cache cache for formatter instances to prevent memory leak
56
+ */
57
+ const createIntl = ({ defaultRichTextElements: rawDefaultRichTextElements, ...config }, cache) => {
58
+ const defaultRichTextElements = assignUniqueKeysToFormatXMLElementFnArgument(rawDefaultRichTextElements);
59
+ const coreIntl = createIntl$1({
60
+ ...DEFAULT_INTL_CONFIG$1,
61
+ ...config,
62
+ defaultRichTextElements
63
+ }, cache);
64
+ const resolvedConfig = {
65
+ locale: coreIntl.locale,
66
+ timeZone: coreIntl.timeZone,
67
+ fallbackOnEmptyString: coreIntl.fallbackOnEmptyString,
68
+ formats: coreIntl.formats,
69
+ defaultLocale: coreIntl.defaultLocale,
70
+ defaultFormats: coreIntl.defaultFormats,
71
+ messages: coreIntl.messages,
72
+ onError: coreIntl.onError,
73
+ defaultRichTextElements
74
+ };
75
+ return {
76
+ ...coreIntl,
77
+ formatMessage: formatMessage$1.bind(null, resolvedConfig, coreIntl.formatters),
78
+ $t: formatMessage$1.bind(null, resolvedConfig, coreIntl.formatters)
79
+ };
80
+ };
81
+ //#endregion
82
+ //#region packages/react-intl/server.ts
83
+ function defineMessages(msgs) {
6
84
  return msgs;
7
85
  }
8
- export function defineMessage(msg) {
86
+ function defineMessage(msg) {
9
87
  return msg;
10
88
  }
89
+ //#endregion
90
+ export { createIntl, createIntlCache, defineMessage, defineMessages };
91
+
92
+ //# sourceMappingURL=server.js.map
package/server.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","names":["DEFAULT_INTL_CONFIG","CORE_DEFAULT_INTL_CONFIG","formatMessage","coreFormatMessage","coreCreateIntl","DEFAULT_INTL_CONFIG"],"sources":["../utils.tsx","../components/createIntl.ts","../server.ts"],"sourcesContent":["import {type FormatXMLElementFn} from 'intl-messageformat'\nimport * as React from 'react'\nimport {type ResolvedIntlConfig} from '#packages/react-intl/types.js'\n\nimport {DEFAULT_INTL_CONFIG as CORE_DEFAULT_INTL_CONFIG} from '@formatjs/intl'\n\nexport function invariant(\n condition: boolean,\n message: string,\n Err: any = Error\n): asserts condition {\n if (!condition) {\n throw new Err(message)\n }\n}\n\nexport function invariantIntlContext(intl?: any): asserts intl {\n invariant(\n intl,\n '[React Intl] Could not find required `intl` object. ' +\n '<IntlProvider> needs to exist in the component ancestry.'\n )\n}\n\nexport type DefaultIntlConfig = Pick<\n ResolvedIntlConfig,\n | 'fallbackOnEmptyString'\n | 'formats'\n | 'messages'\n | 'timeZone'\n | 'textComponent'\n | 'defaultLocale'\n | 'defaultFormats'\n | 'onError'\n>\n\nexport const DEFAULT_INTL_CONFIG: DefaultIntlConfig = {\n ...CORE_DEFAULT_INTL_CONFIG,\n textComponent: React.Fragment,\n}\n\n/**\n * Builds an array of {@link React.ReactNode}s with index-based keys, similar to\n * {@link React.Children.toArray}. However, this function tells React that it\n * was intentional, so they won't produce a bunch of warnings about it.\n *\n * React doesn't recommend doing this because it makes reordering inefficient,\n * but we mostly need this for message chunks, which don't tend to reorder to\n * begin with.\n *\n */\nexport const toKeyedReactNodeArray: typeof React.Children.toArray =\n children => {\n const childrenArray = React.Children.toArray(children)\n\n return childrenArray.map((child, index) => {\n // For React elements, wrap in a keyed Fragment\n // This creates a new element with a key rather than trying to add one after creation\n if (React.isValidElement(child)) {\n return <React.Fragment key={index}>{child}</React.Fragment>\n }\n return child\n })\n }\n\n/**\n * Takes a `formatXMLElementFn`, and composes it in function, which passes\n * argument `parts` through, assigning unique key to each part, to prevent\n * \"Each child in a list should have a unique \"key\"\" React error.\n * @param formatXMLElementFn\n */\nexport function assignUniqueKeysToParts(\n formatXMLElementFn: FormatXMLElementFn<React.ReactNode>\n): FormatXMLElementFn<React.ReactNode> {\n return function (parts: any) {\n // eslint-disable-next-line prefer-rest-params\n return formatXMLElementFn(toKeyedReactNodeArray(parts)) as any\n }\n}\n\nexport function shallowEqual<\n T extends Record<string, unknown> = Record<string, unknown>,\n>(objA?: T, objB?: T): boolean {\n if (objA === objB) {\n return true\n }\n\n if (!objA || !objB) {\n return false\n }\n\n var aKeys = Object.keys(objA)\n var bKeys = Object.keys(objB)\n var len = aKeys.length\n\n if (bKeys.length !== len) {\n return false\n }\n\n for (var i = 0; i < len; i++) {\n var key = aKeys[i]\n\n if (\n objA[key] !== objB[key] ||\n !Object.prototype.hasOwnProperty.call(objB, key)\n ) {\n return false\n }\n }\n\n return true\n}\n","/*\n * Copyright 2015, Yahoo Inc.\n * Copyrights licensed under the New BSD License.\n * See the accompanying LICENSE file for terms.\n */\n\nimport {\n type CreateIntlFn,\n type FormatMessageFn,\n createIntl as coreCreateIntl,\n formatMessage as coreFormatMessage,\n} from '@formatjs/intl'\nimport {\n type FormatXMLElementFn,\n type PrimitiveType,\n isFormatXMLElementFn,\n} from 'intl-messageformat'\nimport * as React from 'react'\nimport type {\n IntlConfig,\n IntlShape,\n ResolvedIntlConfig,\n} from '#packages/react-intl/types.js'\nimport {\n DEFAULT_INTL_CONFIG,\n assignUniqueKeysToParts,\n toKeyedReactNodeArray,\n} from '#packages/react-intl/utils.js'\n\nfunction assignUniqueKeysToFormatXMLElementFnArgument<\n T extends Record<\n string,\n | PrimitiveType\n | React.ReactNode\n | FormatXMLElementFn<React.ReactNode, React.ReactNode>\n > = Record<\n string,\n | PrimitiveType\n | React.ReactNode\n | FormatXMLElementFn<React.ReactNode, React.ReactNode>\n >,\n>(values?: T): T | undefined {\n if (!values) {\n return values\n }\n return Object.keys(values).reduce((acc: T, k) => {\n const v = values[k]\n ;(acc as any)[k] = isFormatXMLElementFn<React.ReactNode>(v)\n ? assignUniqueKeysToParts(v)\n : v\n return acc\n }, {} as T)\n}\n\nconst formatMessage: FormatMessageFn<React.ReactNode> = (\n config,\n formatters,\n descriptor,\n rawValues,\n ...rest\n) => {\n const values = assignUniqueKeysToFormatXMLElementFnArgument(rawValues)\n const chunks = coreFormatMessage(\n config,\n formatters,\n descriptor,\n values as any,\n ...rest\n )\n if (Array.isArray(chunks)) {\n return toKeyedReactNodeArray(chunks)\n }\n return chunks\n}\n\n/**\n * Create intl object\n * @param config intl config\n * @param cache cache for formatter instances to prevent memory leak\n */\nexport const createIntl: CreateIntlFn<\n React.ReactNode,\n IntlConfig,\n IntlShape\n> = (\n {defaultRichTextElements: rawDefaultRichTextElements, ...config},\n cache\n) => {\n const defaultRichTextElements = assignUniqueKeysToFormatXMLElementFnArgument(\n rawDefaultRichTextElements\n )\n const coreIntl = coreCreateIntl<React.ReactNode>(\n {\n ...DEFAULT_INTL_CONFIG,\n ...config,\n defaultRichTextElements,\n },\n cache\n )\n\n const resolvedConfig: ResolvedIntlConfig = {\n locale: coreIntl.locale,\n timeZone: coreIntl.timeZone,\n fallbackOnEmptyString: coreIntl.fallbackOnEmptyString,\n formats: coreIntl.formats,\n defaultLocale: coreIntl.defaultLocale,\n defaultFormats: coreIntl.defaultFormats,\n messages: coreIntl.messages,\n onError: coreIntl.onError,\n defaultRichTextElements,\n }\n\n return {\n ...coreIntl,\n formatMessage: formatMessage.bind(\n null,\n resolvedConfig,\n coreIntl.formatters\n ),\n $t: formatMessage.bind(null, resolvedConfig, coreIntl.formatters),\n } as any\n}\n","import {type MessageDescriptor} from '@formatjs/intl'\n\nexport {\n createIntlCache,\n type IntlCache,\n type MessageDescriptor,\n} from '@formatjs/intl'\nexport {createIntl} from '#packages/react-intl/components/createIntl.js'\nexport type {\n IntlConfig,\n IntlShape,\n ResolvedIntlConfig,\n} from '#packages/react-intl/types.js'\n\n// Identity functions — duplicated here to avoid importing from \"use client\" index\nexport function defineMessages<\n K extends keyof any,\n T = MessageDescriptor,\n U extends Record<K, T> = Record<K, T>,\n>(msgs: U): U {\n return msgs\n}\n\nexport function defineMessage<T extends MessageDescriptor>(msg: T): T {\n return msg\n}\n"],"mappings":";;;;;AAoCA,MAAaA,wBAAyC;CACpD,GAAGC;CACH,eAAe,MAAM;CACtB;;;;;;;;;;;AAYD,MAAa,yBACX,aAAY;AAGV,QAFsB,MAAM,SAAS,QAAQ,SAAS,CAEjC,KAAK,OAAO,UAAU;AAGzC,MAAI,MAAM,eAAe,MAAM,CAC7B,QAAO,oBAAC,MAAM,UAAP,EAAA,UAA6B,OAAuB,EAA/B,MAA+B;AAE7D,SAAO;GACP;;;;;;;;AASN,SAAgB,wBACd,oBACqC;AACrC,QAAO,SAAU,OAAY;AAE3B,SAAO,mBAAmB,sBAAsB,MAAM,CAAC;;;;;AC/C3D,SAAS,6CAYP,QAA2B;AAC3B,KAAI,CAAC,OACH,QAAO;AAET,QAAO,OAAO,KAAK,OAAO,CAAC,QAAQ,KAAQ,MAAM;EAC/C,MAAM,IAAI,OAAO;AACf,MAAY,KAAK,qBAAsC,EAAE,GACvD,wBAAwB,EAAE,GAC1B;AACJ,SAAO;IACN,EAAE,CAAM;;AAGb,MAAMC,mBACJ,QACA,YACA,YACA,WACA,GAAG,SACA;CAEH,MAAM,SAASC,cACb,QACA,YACA,YAJa,6CAA6C,UAAU,EAMpE,GAAG,KACJ;AACD,KAAI,MAAM,QAAQ,OAAO,CACvB,QAAO,sBAAsB,OAAO;AAEtC,QAAO;;;;;;;AAQT,MAAa,cAKX,EAAC,yBAAyB,4BAA4B,GAAG,UACzD,UACG;CACH,MAAM,0BAA0B,6CAC9B,2BACD;CACD,MAAM,WAAWC,aACf;EACE,GAAGC;EACH,GAAG;EACH;EACD,EACD,MACD;CAED,MAAM,iBAAqC;EACzC,QAAQ,SAAS;EACjB,UAAU,SAAS;EACnB,uBAAuB,SAAS;EAChC,SAAS,SAAS;EAClB,eAAe,SAAS;EACxB,gBAAgB,SAAS;EACzB,UAAU,SAAS;EACnB,SAAS,SAAS;EAClB;EACD;AAED,QAAO;EACL,GAAG;EACH,eAAeH,gBAAc,KAC3B,MACA,gBACA,SAAS,WACV;EACD,IAAIA,gBAAc,KAAK,MAAM,gBAAgB,SAAS,WAAW;EAClE;;;;ACzGH,SAAgB,eAId,MAAY;AACZ,QAAO;;AAGT,SAAgB,cAA2C,KAAW;AACpE,QAAO"}
@@ -1,5 +0,0 @@
1
- import * as React from "react";
2
- import { type IntlShape } from "../types.js";
3
- declare const IntlContext: React.Context<IntlShape>;
4
- declare const Provider: React.Provider<IntlShape>;
5
- export { Provider, IntlContext as Context };
@@ -1,5 +0,0 @@
1
- import * as React from "react";
2
- import "../types.js";
3
- const IntlContext = React.createContext(null);
4
- const Provider = IntlContext.Provider;
5
- export { Provider, IntlContext as Context };
@@ -1,27 +0,0 @@
1
- import { type FormatDateOptions, type FormatDisplayNameOptions, type FormatListOptions, type FormatNumberOptions } from "@formatjs/intl";
2
- import * as React from "react";
3
- import { type IntlShape } from "../types.js";
4
- type Formatter = {
5
- formatDate: FormatDateOptions;
6
- formatTime: FormatDateOptions;
7
- formatNumber: FormatNumberOptions;
8
- formatList: FormatListOptions;
9
- formatDisplayName: FormatDisplayNameOptions;
10
- };
11
- export declare const FormattedNumberParts: React.FC<Formatter["formatNumber"] & {
12
- value: Parameters<IntlShape["formatNumber"]>[0];
13
- children(val: Intl.NumberFormatPart[]): React.ReactElement | null;
14
- }>;
15
- export declare const FormattedListParts: React.FC<Formatter["formatList"] & {
16
- value: Parameters<IntlShape["formatList"]>[0];
17
- children(val: ReturnType<Intl.ListFormat["formatToParts"]>): React.ReactElement | null;
18
- }>;
19
- export declare function createFormattedDateTimePartsComponent<Name extends "formatDate" | "formatTime">(name: Name): React.FC<Formatter[Name] & {
20
- value: Parameters<IntlShape[Name]>[0];
21
- children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null;
22
- }>;
23
- export declare function createFormattedComponent<Name extends keyof Formatter>(name: Name): React.FC<Formatter[Name] & {
24
- value: Parameters<IntlShape[Name]>[0];
25
- children?(val: string): React.ReactElement | null;
26
- }>;
27
- export {};
@@ -1,60 +0,0 @@
1
- import "@formatjs/intl";
2
- import * as React from "react";
3
- import "../types.js";
4
- import useIntl from "./useIntl.js";
5
- import { jsx as _jsx } from "react/jsx-runtime";
6
- var DisplayName = /* @__PURE__ */ function(DisplayName) {
7
- DisplayName["formatDate"] = "FormattedDate";
8
- DisplayName["formatTime"] = "FormattedTime";
9
- DisplayName["formatNumber"] = "FormattedNumber";
10
- DisplayName["formatList"] = "FormattedList";
11
- // Note that this DisplayName is the locale display name, not to be confused with
12
- // the name of the enum, which is for React component display name in dev tools.
13
- DisplayName["formatDisplayName"] = "FormattedDisplayName";
14
- return DisplayName;
15
- }(DisplayName || {});
16
- var DisplayNameParts = /* @__PURE__ */ function(DisplayNameParts) {
17
- DisplayNameParts["formatDate"] = "FormattedDateParts";
18
- DisplayNameParts["formatTime"] = "FormattedTimeParts";
19
- DisplayNameParts["formatNumber"] = "FormattedNumberParts";
20
- DisplayNameParts["formatList"] = "FormattedListParts";
21
- return DisplayNameParts;
22
- }(DisplayNameParts || {});
23
- export const FormattedNumberParts = (props) => {
24
- const intl = useIntl();
25
- const { value, children, ...formatProps } = props;
26
- return children(intl.formatNumberToParts(value, formatProps));
27
- };
28
- FormattedNumberParts.displayName = "FormattedNumberParts";
29
- export const FormattedListParts = (props) => {
30
- const intl = useIntl();
31
- const { value, children, ...formatProps } = props;
32
- return children(intl.formatListToParts(value, formatProps));
33
- };
34
- FormattedNumberParts.displayName = "FormattedNumberParts";
35
- export function createFormattedDateTimePartsComponent(name) {
36
- const ComponentParts = (props) => {
37
- const intl = useIntl();
38
- const { value, children, ...formatProps } = props;
39
- const date = typeof value === "string" ? new Date(value || 0) : value;
40
- const formattedParts = name === "formatDate" ? intl.formatDateToParts(date, formatProps) : intl.formatTimeToParts(date, formatProps);
41
- return children(formattedParts);
42
- };
43
- ComponentParts.displayName = DisplayNameParts[name];
44
- return ComponentParts;
45
- }
46
- export function createFormattedComponent(name) {
47
- const Component = (props) => {
48
- const intl = useIntl();
49
- const { value, children, ...formatProps } = props;
50
- // TODO: fix TS type definition for localeMatcher upstream
51
- const formattedValue = intl[name](value, formatProps);
52
- if (typeof children === "function") {
53
- return children(formattedValue);
54
- }
55
- const Text = intl.textComponent || React.Fragment;
56
- return /* @__PURE__ */ _jsx(Text, { children: formattedValue });
57
- };
58
- Component.displayName = DisplayName[name];
59
- return Component;
60
- }
@@ -1,9 +0,0 @@
1
- import { type CreateIntlFn } from "@formatjs/intl";
2
- import * as React from "react";
3
- import type { IntlConfig, IntlShape } from "../types.js";
4
- /**
5
- * Create intl object
6
- * @param config intl config
7
- * @param cache cache for formatter instances to prevent memory leak
8
- */
9
- export declare const createIntl: CreateIntlFn<React.ReactNode, IntlConfig, IntlShape>;
@@ -1,56 +0,0 @@
1
- /*
2
- * Copyright 2015, Yahoo Inc.
3
- * Copyrights licensed under the New BSD License.
4
- * See the accompanying LICENSE file for terms.
5
- */
6
- import { createIntl as coreCreateIntl, formatMessage as coreFormatMessage } from "@formatjs/intl";
7
- import { isFormatXMLElementFn } from "intl-messageformat";
8
- import * as React from "react";
9
- import { DEFAULT_INTL_CONFIG, assignUniqueKeysToParts, toKeyedReactNodeArray } from "../utils.js";
10
- function assignUniqueKeysToFormatXMLElementFnArgument(values) {
11
- if (!values) {
12
- return values;
13
- }
14
- return Object.keys(values).reduce((acc, k) => {
15
- const v = values[k];
16
- acc[k] = isFormatXMLElementFn(v) ? assignUniqueKeysToParts(v) : v;
17
- return acc;
18
- }, {});
19
- }
20
- const formatMessage = (config, formatters, descriptor, rawValues, ...rest) => {
21
- const values = assignUniqueKeysToFormatXMLElementFnArgument(rawValues);
22
- const chunks = coreFormatMessage(config, formatters, descriptor, values, ...rest);
23
- if (Array.isArray(chunks)) {
24
- return toKeyedReactNodeArray(chunks);
25
- }
26
- return chunks;
27
- };
28
- /**
29
- * Create intl object
30
- * @param config intl config
31
- * @param cache cache for formatter instances to prevent memory leak
32
- */
33
- export const createIntl = ({ defaultRichTextElements: rawDefaultRichTextElements, ...config }, cache) => {
34
- const defaultRichTextElements = assignUniqueKeysToFormatXMLElementFnArgument(rawDefaultRichTextElements);
35
- const coreIntl = coreCreateIntl({
36
- ...DEFAULT_INTL_CONFIG,
37
- ...config,
38
- defaultRichTextElements
39
- }, cache);
40
- const resolvedConfig = {
41
- locale: coreIntl.locale,
42
- timeZone: coreIntl.timeZone,
43
- fallbackOnEmptyString: coreIntl.fallbackOnEmptyString,
44
- formats: coreIntl.formats,
45
- defaultLocale: coreIntl.defaultLocale,
46
- defaultFormats: coreIntl.defaultFormats,
47
- messages: coreIntl.messages,
48
- onError: coreIntl.onError,
49
- defaultRichTextElements
50
- };
51
- return {
52
- ...coreIntl,
53
- formatMessage: formatMessage.bind(null, resolvedConfig, coreIntl.formatters),
54
- $t: formatMessage.bind(null, resolvedConfig, coreIntl.formatters)
55
- };
56
- };
@@ -1,9 +0,0 @@
1
- import { type FormatDateTimeRangeOptions } from "@formatjs/intl";
2
- import * as React from "react";
3
- interface Props extends FormatDateTimeRangeOptions {
4
- from: Parameters<Intl.DateTimeFormat["formatRange"]>[0];
5
- to: Parameters<Intl.DateTimeFormat["formatRange"]>[1];
6
- children?(value: React.ReactNode): React.ReactElement | null;
7
- }
8
- declare const FormattedDateTimeRange: React.FC<Props>;
9
- export default FormattedDateTimeRange;
@@ -1,16 +0,0 @@
1
- import "@formatjs/intl";
2
- import * as React from "react";
3
- import useIntl from "./useIntl.js";
4
- import { jsx as _jsx } from "react/jsx-runtime";
5
- const FormattedDateTimeRange = (props) => {
6
- const intl = useIntl();
7
- const { from, to, children, ...formatProps } = props;
8
- const formattedValue = intl.formatDateTimeRange(from, to, formatProps);
9
- if (typeof children === "function") {
10
- return children(formattedValue);
11
- }
12
- const Text = intl.textComponent || React.Fragment;
13
- return /* @__PURE__ */ _jsx(Text, { children: formattedValue });
14
- };
15
- FormattedDateTimeRange.displayName = "FormattedDateTimeRange";
16
- export default FormattedDateTimeRange;
@@ -1,11 +0,0 @@
1
- import type { FormatXMLElementFn, Options as IntlMessageFormatOptions, PrimitiveType } from "intl-messageformat";
2
- import * as React from "react";
3
- import { type MessageDescriptor } from "@formatjs/intl";
4
- export interface Props<V extends Record<string, any> = Record<string, React.ReactNode | PrimitiveType | FormatXMLElementFn<React.ReactNode>>> extends MessageDescriptor {
5
- values?: V;
6
- tagName?: React.ElementType<any>;
7
- children?(nodes: React.ReactNode[]): React.ReactNode | null;
8
- ignoreTag?: IntlMessageFormatOptions["ignoreTag"];
9
- }
10
- declare const MemoizedFormattedMessage: React.ComponentType<Props>;
11
- export default MemoizedFormattedMessage;
@@ -1,32 +0,0 @@
1
- import * as React from "react";
2
- import "@formatjs/intl";
3
- import { shallowEqual } from "../utils.js";
4
- import useIntl from "./useIntl.js";
5
- import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
6
- function areEqual(prevProps, nextProps) {
7
- const { values, ...otherProps } = prevProps;
8
- const { values: nextValues, ...nextOtherProps } = nextProps;
9
- return shallowEqual(nextValues, values) && shallowEqual(otherProps, nextOtherProps);
10
- }
11
- function FormattedMessage(props) {
12
- const intl = useIntl();
13
- const { formatMessage, textComponent: Text = React.Fragment } = intl;
14
- const { id, description, defaultMessage, values, children, tagName: Component = Text, ignoreTag } = props;
15
- const descriptor = {
16
- id,
17
- description,
18
- defaultMessage
19
- };
20
- const nodes = formatMessage(descriptor, values, { ignoreTag });
21
- if (typeof children === "function") {
22
- return children(Array.isArray(nodes) ? nodes : [nodes]);
23
- }
24
- if (Component) {
25
- return /* @__PURE__ */ _jsx(Component, { children: nodes });
26
- }
27
- return /* @__PURE__ */ _jsx(_Fragment, { children: nodes });
28
- }
29
- FormattedMessage.displayName = "FormattedMessage";
30
- const MemoizedFormattedMessage = React.memo(FormattedMessage, areEqual);
31
- MemoizedFormattedMessage.displayName = "MemoizedFormattedMessage";
32
- export default MemoizedFormattedMessage;
@@ -1,14 +0,0 @@
1
- import * as React from "react";
2
- import { type FormatPluralOptions } from "@formatjs/intl";
3
- interface Props extends FormatPluralOptions {
4
- value: number;
5
- other: React.ReactNode;
6
- zero?: React.ReactNode;
7
- one?: React.ReactNode;
8
- two?: React.ReactNode;
9
- few?: React.ReactNode;
10
- many?: React.ReactNode;
11
- children?(value: React.ReactNode): React.ReactElement | null;
12
- }
13
- declare const FormattedPlural: React.FC<Props>;
14
- export default FormattedPlural;
@@ -1,25 +0,0 @@
1
- /*
2
- * Copyright 2015, Yahoo Inc.
3
- * Copyrights licensed under the New BSD License.
4
- * See the accompanying LICENSE file for terms.
5
- */
6
- import * as React from "react";
7
- import "@formatjs/intl";
8
- import useIntl from "./useIntl.js";
9
- import { jsx as _jsx } from "react/jsx-runtime";
10
- const FormattedPlural = (props) => {
11
- const { formatPlural, textComponent: Text } = useIntl();
12
- const { value, other, children } = props;
13
- const pluralCategory = formatPlural(value, props);
14
- const formattedPlural = props[pluralCategory] || other;
15
- if (typeof children === "function") {
16
- return children(formattedPlural);
17
- }
18
- if (Text) {
19
- return /* @__PURE__ */ _jsx(Text, { children: formattedPlural });
20
- }
21
- // Work around @types/react where React.FC cannot return string
22
- return formattedPlural;
23
- };
24
- FormattedPlural.displayName = "FormattedPlural";
25
- export default FormattedPlural;
@@ -1,4 +0,0 @@
1
- import * as React from "react";
2
- import type { IntlConfig } from "../types.js";
3
- declare const IntlProvider: React.FC<React.PropsWithChildren<IntlConfig>>;
4
- export default IntlProvider;
@@ -1,57 +0,0 @@
1
- /*
2
- * Copyright 2015, Yahoo Inc.
3
- * Copyrights licensed under the New BSD License.
4
- * See the accompanying LICENSE file for terms.
5
- */
6
- import { createIntlCache } from "@formatjs/intl";
7
- import * as React from "react";
8
- import { DEFAULT_INTL_CONFIG, invariantIntlContext, shallowEqual } from "../utils.js";
9
- import { createIntl } from "./createIntl.js";
10
- import { Provider } from "./context.js";
11
- import { jsx as _jsx } from "react/jsx-runtime";
12
- function processIntlConfig(config) {
13
- return {
14
- locale: config.locale,
15
- timeZone: config.timeZone,
16
- fallbackOnEmptyString: config.fallbackOnEmptyString,
17
- formats: config.formats,
18
- textComponent: config.textComponent,
19
- messages: config.messages,
20
- defaultLocale: config.defaultLocale,
21
- defaultFormats: config.defaultFormats,
22
- onError: config.onError,
23
- onWarn: config.onWarn,
24
- wrapRichTextChunksInFragment: config.wrapRichTextChunksInFragment,
25
- defaultRichTextElements: config.defaultRichTextElements
26
- };
27
- }
28
- function IntlProviderImpl(props) {
29
- const cacheRef = React.useRef(createIntlCache());
30
- const prevConfigRef = React.useRef(undefined);
31
- const intlRef = React.useRef(undefined);
32
- // Filter out undefined values from props so they don't override defaults.
33
- // React's defaultProps treated `prop={undefined}` as "not provided"; we
34
- // replicate that behaviour here after converting to a function component.
35
- const filteredProps = {};
36
- for (const key in props) {
37
- if (props[key] !== undefined) {
38
- filteredProps[key] = props[key];
39
- }
40
- }
41
- const config = processIntlConfig({
42
- ...DEFAULT_INTL_CONFIG,
43
- ...filteredProps
44
- });
45
- if (!prevConfigRef.current || !shallowEqual(prevConfigRef.current, config)) {
46
- prevConfigRef.current = config;
47
- intlRef.current = createIntl(config, cacheRef.current);
48
- }
49
- invariantIntlContext(intlRef.current);
50
- return /* @__PURE__ */ _jsx(Provider, {
51
- value: intlRef.current,
52
- children: props.children
53
- });
54
- }
55
- IntlProviderImpl.displayName = "IntlProvider";
56
- const IntlProvider = IntlProviderImpl;
57
- export default IntlProvider;
@@ -1,10 +0,0 @@
1
- import * as React from "react";
2
- import { type FormatRelativeTimeOptions } from "@formatjs/intl";
3
- export interface Props extends FormatRelativeTimeOptions {
4
- value?: number;
5
- unit?: Intl.RelativeTimeFormatUnit;
6
- updateIntervalInSeconds?: number;
7
- children?(value: string): React.ReactElement | null;
8
- }
9
- declare const FormattedRelativeTime: React.FC<Props>;
10
- export default FormattedRelativeTime;
@@ -1,120 +0,0 @@
1
- /*
2
- * Copyright 2015, Yahoo Inc.
3
- * Copyrights licensed under the New BSD License.
4
- * See the accompanying LICENSE file for terms.
5
- */
6
- import * as React from "react";
7
- import "@formatjs/intl";
8
- import { invariant } from "../utils.js";
9
- import useIntl from "./useIntl.js";
10
- import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
11
- const MINUTE = 60;
12
- const HOUR = 60 * 60;
13
- const DAY = 60 * 60 * 24;
14
- function selectUnit(seconds) {
15
- const absValue = Math.abs(seconds);
16
- if (absValue < MINUTE) {
17
- return "second";
18
- }
19
- if (absValue < HOUR) {
20
- return "minute";
21
- }
22
- if (absValue < DAY) {
23
- return "hour";
24
- }
25
- return "day";
26
- }
27
- function getDurationInSeconds(unit) {
28
- switch (unit) {
29
- case "second": return 1;
30
- case "minute": return MINUTE;
31
- case "hour": return HOUR;
32
- default: return DAY;
33
- }
34
- }
35
- function valueToSeconds(value, unit) {
36
- if (!value) {
37
- return 0;
38
- }
39
- switch (unit) {
40
- case "second": return value;
41
- case "minute": return value * MINUTE;
42
- default: return value * HOUR;
43
- }
44
- }
45
- const INCREMENTABLE_UNITS = [
46
- "second",
47
- "minute",
48
- "hour"
49
- ];
50
- function canIncrement(unit = "second") {
51
- return INCREMENTABLE_UNITS.indexOf(unit) > -1;
52
- }
53
- const SimpleFormattedRelativeTime = (props) => {
54
- const { formatRelativeTime, textComponent: Text } = useIntl();
55
- const { children, value, unit, ...otherProps } = props;
56
- const formattedRelativeTime = formatRelativeTime(value || 0, unit, otherProps);
57
- if (typeof children === "function") {
58
- return children(formattedRelativeTime);
59
- }
60
- if (Text) {
61
- return /* @__PURE__ */ _jsx(Text, { children: formattedRelativeTime });
62
- }
63
- return /* @__PURE__ */ _jsx(_Fragment, { children: formattedRelativeTime });
64
- };
65
- const FormattedRelativeTime = ({ value = 0, unit = "second", updateIntervalInSeconds, ...otherProps }) => {
66
- invariant(!updateIntervalInSeconds || !!(updateIntervalInSeconds && canIncrement(unit)), "Cannot schedule update with unit longer than hour");
67
- const [prevUnit, setPrevUnit] = React.useState();
68
- const [prevValue, setPrevValue] = React.useState(0);
69
- const [currentValueInSeconds, setCurrentValueInSeconds] = React.useState(0);
70
- const updateTimer = React.useRef(undefined);
71
- if (unit !== prevUnit || value !== prevValue) {
72
- setPrevValue(value || 0);
73
- setPrevUnit(unit);
74
- setCurrentValueInSeconds(canIncrement(unit) ? valueToSeconds(value, unit) : 0);
75
- }
76
- React.useEffect(() => {
77
- function clearUpdateTimer() {
78
- clearTimeout(updateTimer.current);
79
- }
80
- clearUpdateTimer();
81
- // If there's no interval and we cannot increment this unit, do nothing
82
- if (!updateIntervalInSeconds || !canIncrement(unit)) {
83
- return clearUpdateTimer;
84
- }
85
- // Figure out the next interesting time
86
- const nextValueInSeconds = currentValueInSeconds - updateIntervalInSeconds;
87
- const nextUnit = selectUnit(nextValueInSeconds);
88
- // We've reached the max auto incrementable unit, don't schedule another update
89
- if (nextUnit === "day") {
90
- return clearUpdateTimer;
91
- }
92
- const unitDuration = getDurationInSeconds(nextUnit);
93
- const remainder = nextValueInSeconds % unitDuration;
94
- const prevInterestingValueInSeconds = nextValueInSeconds - remainder;
95
- const nextInterestingValueInSeconds = prevInterestingValueInSeconds >= currentValueInSeconds ? prevInterestingValueInSeconds - unitDuration : prevInterestingValueInSeconds;
96
- const delayInSeconds = Math.abs(nextInterestingValueInSeconds - currentValueInSeconds);
97
- if (currentValueInSeconds !== nextInterestingValueInSeconds) {
98
- updateTimer.current = setTimeout(() => setCurrentValueInSeconds(nextInterestingValueInSeconds), delayInSeconds * 1e3);
99
- }
100
- return clearUpdateTimer;
101
- }, [
102
- currentValueInSeconds,
103
- updateIntervalInSeconds,
104
- unit
105
- ]);
106
- let currentValue = value || 0;
107
- let currentUnit = unit;
108
- if (canIncrement(unit) && typeof currentValueInSeconds === "number" && updateIntervalInSeconds) {
109
- currentUnit = selectUnit(currentValueInSeconds);
110
- const unitDuration = getDurationInSeconds(currentUnit);
111
- currentValue = Math.round(currentValueInSeconds / unitDuration);
112
- }
113
- return /* @__PURE__ */ _jsx(SimpleFormattedRelativeTime, {
114
- value: currentValue,
115
- unit: currentUnit,
116
- ...otherProps
117
- });
118
- };
119
- FormattedRelativeTime.displayName = "FormattedRelativeTime";
120
- export default FormattedRelativeTime;
@@ -1,2 +0,0 @@
1
- import { type IntlShape } from "../types.js";
2
- export default function useIntl(this: void): IntlShape;