@depup/react-intl 10.1.0-depup.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/LICENSE.md +27 -0
- package/README.md +25 -0
- package/changes.json +5 -0
- package/index.d.ts +50 -0
- package/index.js +36 -0
- package/package.json +160 -0
- package/react-intl.iife.js +8132 -0
- package/server.d.ts +10 -0
- package/server.js +10 -0
- package/src/components/context.d.ts +5 -0
- package/src/components/context.js +5 -0
- package/src/components/createFormattedComponent.d.ts +27 -0
- package/src/components/createFormattedComponent.js +60 -0
- package/src/components/createIntl.d.ts +9 -0
- package/src/components/createIntl.js +56 -0
- package/src/components/dateTimeRange.d.ts +9 -0
- package/src/components/dateTimeRange.js +16 -0
- package/src/components/message.d.ts +11 -0
- package/src/components/message.js +32 -0
- package/src/components/plural.d.ts +14 -0
- package/src/components/plural.js +25 -0
- package/src/components/provider.d.ts +4 -0
- package/src/components/provider.js +57 -0
- package/src/components/relative.d.ts +10 -0
- package/src/components/relative.js +120 -0
- package/src/components/useIntl.d.ts +2 -0
- package/src/components/useIntl.js +9 -0
- package/src/types.d.ts +14 -0
- package/src/types.js +8 -0
- package/src/utils.d.ts +26 -0
- package/src/utils.js +71 -0
package/server.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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;
|
package/server.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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) {
|
|
6
|
+
return msgs;
|
|
7
|
+
}
|
|
8
|
+
export function defineMessage(msg) {
|
|
9
|
+
return msg;
|
|
10
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
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>;
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
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;
|
|
@@ -0,0 +1,16 @@
|
|
|
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;
|
|
@@ -0,0 +1,11 @@
|
|
|
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;
|
|
@@ -0,0 +1,32 @@
|
|
|
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;
|
|
@@ -0,0 +1,14 @@
|
|
|
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;
|
|
@@ -0,0 +1,25 @@
|
|
|
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;
|
|
@@ -0,0 +1,57 @@
|
|
|
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;
|
|
@@ -0,0 +1,10 @@
|
|
|
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;
|
|
@@ -0,0 +1,120 @@
|
|
|
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;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import "../types.js";
|
|
3
|
+
import { invariantIntlContext } from "../utils.js";
|
|
4
|
+
import { Context } from "./context.js";
|
|
5
|
+
export default function useIntl() {
|
|
6
|
+
const intl = React.useContext(Context);
|
|
7
|
+
invariantIntlContext(intl);
|
|
8
|
+
return intl;
|
|
9
|
+
}
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type ResolvedIntlConfig as CoreResolvedIntlConfig, type Formatters, type IntlFormatters, type MessageDescriptor } from "@formatjs/intl";
|
|
2
|
+
import { type FormatXMLElementFn, type Options as IntlMessageFormatOptions, type PrimitiveType } from "intl-messageformat";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import type { DEFAULT_INTL_CONFIG } from "./utils.js";
|
|
5
|
+
export type IntlConfig = Omit<ResolvedIntlConfig, keyof typeof DEFAULT_INTL_CONFIG> & Partial<typeof DEFAULT_INTL_CONFIG>;
|
|
6
|
+
export interface ResolvedIntlConfig extends CoreResolvedIntlConfig<React.ReactNode> {
|
|
7
|
+
textComponent?: React.ComponentType | keyof React.JSX.IntrinsicElements;
|
|
8
|
+
wrapRichTextChunksInFragment?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface IntlShape extends ResolvedIntlConfig, IntlFormatters<React.ReactNode> {
|
|
11
|
+
formatMessage(this: void, descriptor: MessageDescriptor, values?: Record<string, PrimitiveType | FormatXMLElementFn<string, string>>, opts?: IntlMessageFormatOptions): string;
|
|
12
|
+
formatMessage(this: void, descriptor: MessageDescriptor, values?: Record<string, React.ReactNode | PrimitiveType | FormatXMLElementFn<string, React.ReactNode>>, opts?: IntlMessageFormatOptions): Array<React.ReactNode>;
|
|
13
|
+
formatters: Formatters;
|
|
14
|
+
}
|
package/src/types.js
ADDED
package/src/utils.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type FormatXMLElementFn } from "intl-messageformat";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { type ResolvedIntlConfig } from "./types.js";
|
|
4
|
+
export declare function invariant(condition: boolean, message: string, Err?: any): asserts condition;
|
|
5
|
+
export declare function invariantIntlContext(intl?: any): asserts intl;
|
|
6
|
+
export type DefaultIntlConfig = Pick<ResolvedIntlConfig, "fallbackOnEmptyString" | "formats" | "messages" | "timeZone" | "textComponent" | "defaultLocale" | "defaultFormats" | "onError">;
|
|
7
|
+
export declare const DEFAULT_INTL_CONFIG: DefaultIntlConfig;
|
|
8
|
+
/**
|
|
9
|
+
* Builds an array of {@link React.ReactNode}s with index-based keys, similar to
|
|
10
|
+
* {@link React.Children.toArray}. However, this function tells React that it
|
|
11
|
+
* was intentional, so they won't produce a bunch of warnings about it.
|
|
12
|
+
*
|
|
13
|
+
* React doesn't recommend doing this because it makes reordering inefficient,
|
|
14
|
+
* but we mostly need this for message chunks, which don't tend to reorder to
|
|
15
|
+
* begin with.
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
export declare const toKeyedReactNodeArray: typeof React.Children.toArray;
|
|
19
|
+
/**
|
|
20
|
+
* Takes a `formatXMLElementFn`, and composes it in function, which passes
|
|
21
|
+
* argument `parts` through, assigning unique key to each part, to prevent
|
|
22
|
+
* "Each child in a list should have a unique "key"" React error.
|
|
23
|
+
* @param formatXMLElementFn
|
|
24
|
+
*/
|
|
25
|
+
export declare function assignUniqueKeysToParts(formatXMLElementFn: FormatXMLElementFn<React.ReactNode>): FormatXMLElementFn<React.ReactNode>;
|
|
26
|
+
export declare function shallowEqual<T extends Record<string, unknown> = Record<string, unknown>>(objA?: T, objB?: T): boolean;
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import "intl-messageformat";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import "./types.js";
|
|
4
|
+
import { DEFAULT_INTL_CONFIG as CORE_DEFAULT_INTL_CONFIG } from "@formatjs/intl";
|
|
5
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
6
|
+
export function invariant(condition, message, Err = Error) {
|
|
7
|
+
if (!condition) {
|
|
8
|
+
throw new Err(message);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function invariantIntlContext(intl) {
|
|
12
|
+
invariant(intl, "[React Intl] Could not find required `intl` object. " + "<IntlProvider> needs to exist in the component ancestry.");
|
|
13
|
+
}
|
|
14
|
+
export const DEFAULT_INTL_CONFIG = {
|
|
15
|
+
...CORE_DEFAULT_INTL_CONFIG,
|
|
16
|
+
textComponent: React.Fragment
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Builds an array of {@link React.ReactNode}s with index-based keys, similar to
|
|
20
|
+
* {@link React.Children.toArray}. However, this function tells React that it
|
|
21
|
+
* was intentional, so they won't produce a bunch of warnings about it.
|
|
22
|
+
*
|
|
23
|
+
* React doesn't recommend doing this because it makes reordering inefficient,
|
|
24
|
+
* but we mostly need this for message chunks, which don't tend to reorder to
|
|
25
|
+
* begin with.
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
export const toKeyedReactNodeArray = (children) => {
|
|
29
|
+
const childrenArray = React.Children.toArray(children);
|
|
30
|
+
return childrenArray.map((child, index) => {
|
|
31
|
+
// For React elements, wrap in a keyed Fragment
|
|
32
|
+
// This creates a new element with a key rather than trying to add one after creation
|
|
33
|
+
if (React.isValidElement(child)) {
|
|
34
|
+
return /* @__PURE__ */ _jsx(React.Fragment, { children: child }, index);
|
|
35
|
+
}
|
|
36
|
+
return child;
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Takes a `formatXMLElementFn`, and composes it in function, which passes
|
|
41
|
+
* argument `parts` through, assigning unique key to each part, to prevent
|
|
42
|
+
* "Each child in a list should have a unique "key"" React error.
|
|
43
|
+
* @param formatXMLElementFn
|
|
44
|
+
*/
|
|
45
|
+
export function assignUniqueKeysToParts(formatXMLElementFn) {
|
|
46
|
+
return function(parts) {
|
|
47
|
+
// eslint-disable-next-line prefer-rest-params
|
|
48
|
+
return formatXMLElementFn(toKeyedReactNodeArray(parts));
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export function shallowEqual(objA, objB) {
|
|
52
|
+
if (objA === objB) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
if (!objA || !objB) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
var aKeys = Object.keys(objA);
|
|
59
|
+
var bKeys = Object.keys(objB);
|
|
60
|
+
var len = aKeys.length;
|
|
61
|
+
if (bKeys.length !== len) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
for (var i = 0; i < len; i++) {
|
|
65
|
+
var key = aKeys[i];
|
|
66
|
+
if (objA[key] !== objB[key] || !Object.prototype.hasOwnProperty.call(objB, key)) {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|