react-intl 7.1.14 → 8.0.1

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.
Files changed (46) hide show
  1. package/index.d.ts +15 -15
  2. package/index.js +21 -46
  3. package/package.json +10 -8
  4. package/react-intl.iife.js +113 -111
  5. package/src/components/createFormattedComponent.d.ts +1 -1
  6. package/src/components/createFormattedComponent.js +17 -24
  7. package/src/components/createIntl.d.ts +1 -1
  8. package/src/components/createIntl.js +12 -16
  9. package/src/components/dateTimeRange.js +6 -8
  10. package/src/components/injectIntl.d.ts +1 -1
  11. package/src/components/injectIntl.js +14 -16
  12. package/src/components/message.js +10 -12
  13. package/src/components/plural.js +4 -7
  14. package/src/components/provider.d.ts +2 -2
  15. package/src/components/provider.js +15 -17
  16. package/src/components/relative.js +10 -12
  17. package/src/components/useIntl.d.ts +1 -1
  18. package/src/components/useIntl.js +6 -10
  19. package/src/types.d.ts +1 -1
  20. package/src/types.js +1 -2
  21. package/src/utils.d.ts +2 -1
  22. package/src/utils.js +21 -29
  23. package/lib/index.d.ts +0 -46
  24. package/lib/index.js +0 -26
  25. package/lib/src/components/createFormattedComponent.d.ts +0 -27
  26. package/lib/src/components/createFormattedComponent.js +0 -62
  27. package/lib/src/components/createIntl.d.ts +0 -9
  28. package/lib/src/components/createIntl.js +0 -58
  29. package/lib/src/components/dateTimeRange.d.ts +0 -9
  30. package/lib/src/components/dateTimeRange.js +0 -15
  31. package/lib/src/components/injectIntl.d.ts +0 -39
  32. package/lib/src/components/injectIntl.js +0 -35
  33. package/lib/src/components/message.d.ts +0 -11
  34. package/lib/src/components/message.js +0 -35
  35. package/lib/src/components/plural.d.ts +0 -14
  36. package/lib/src/components/plural.js +0 -23
  37. package/lib/src/components/provider.d.ts +0 -29
  38. package/lib/src/components/provider.js +0 -59
  39. package/lib/src/components/relative.d.ts +0 -10
  40. package/lib/src/components/relative.js +0 -125
  41. package/lib/src/components/useIntl.d.ts +0 -2
  42. package/lib/src/components/useIntl.js +0 -8
  43. package/lib/src/types.d.ts +0 -14
  44. package/lib/src/types.js +0 -1
  45. package/lib/src/utils.d.ts +0 -25
  46. package/lib/src/utils.js +0 -69
package/lib/index.js DELETED
@@ -1,26 +0,0 @@
1
- import { createFormattedComponent, createFormattedDateTimePartsComponent, } from './src/components/createFormattedComponent';
2
- import { createIntl } from './src/components/createIntl';
3
- import FormattedDateTimeRange from './src/components/dateTimeRange';
4
- import injectIntl, { Context as IntlContext, Provider as RawIntlProvider, } from './src/components/injectIntl';
5
- import FormattedMessage from './src/components/message';
6
- import FormattedPlural from './src/components/plural';
7
- import IntlProvider from './src/components/provider';
8
- import FormattedRelativeTime from './src/components/relative';
9
- import useIntl from './src/components/useIntl';
10
- export { createIntlCache, InvalidConfigError, MessageFormatError, MissingDataError, MissingTranslationError, IntlError as ReactIntlError, IntlErrorCode as ReactIntlErrorCode, UnsupportedFormatterError, } from '@formatjs/intl';
11
- export { createIntl, FormattedDateTimeRange, FormattedMessage, FormattedPlural, FormattedRelativeTime, injectIntl, IntlContext, IntlProvider, RawIntlProvider, useIntl, };
12
- export function defineMessages(msgs) {
13
- return msgs;
14
- }
15
- export function defineMessage(msg) {
16
- return msg;
17
- }
18
- // IMPORTANT: Explicit here to prevent api-extractor from outputing `import('./src/types').CustomFormatConfig`
19
- export var FormattedDate = createFormattedComponent('formatDate');
20
- export var FormattedTime = createFormattedComponent('formatTime');
21
- export var FormattedNumber = createFormattedComponent('formatNumber');
22
- export var FormattedList = createFormattedComponent('formatList');
23
- export var FormattedDisplayName = createFormattedComponent('formatDisplayName');
24
- export var FormattedDateParts = createFormattedDateTimePartsComponent('formatDate');
25
- export var FormattedTimeParts = createFormattedDateTimePartsComponent('formatTime');
26
- export { FormattedListParts, FormattedNumberParts, } from './src/components/createFormattedComponent';
@@ -1,27 +0,0 @@
1
- import { FormatDateOptions, FormatDisplayNameOptions, FormatListOptions, FormatNumberOptions } from '@formatjs/intl';
2
- import * as React from 'react';
3
- import { IntlShape } from '../types';
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,62 +0,0 @@
1
- import { __rest } from "tslib";
2
- import * as React from 'react';
3
- import useIntl from './useIntl';
4
- var DisplayName;
5
- (function (DisplayName) {
6
- DisplayName["formatDate"] = "FormattedDate";
7
- DisplayName["formatTime"] = "FormattedTime";
8
- DisplayName["formatNumber"] = "FormattedNumber";
9
- DisplayName["formatList"] = "FormattedList";
10
- // Note that this DisplayName is the locale display name, not to be confused with
11
- // the name of the enum, which is for React component display name in dev tools.
12
- DisplayName["formatDisplayName"] = "FormattedDisplayName";
13
- })(DisplayName || (DisplayName = {}));
14
- var DisplayNameParts;
15
- (function (DisplayNameParts) {
16
- DisplayNameParts["formatDate"] = "FormattedDateParts";
17
- DisplayNameParts["formatTime"] = "FormattedTimeParts";
18
- DisplayNameParts["formatNumber"] = "FormattedNumberParts";
19
- DisplayNameParts["formatList"] = "FormattedListParts";
20
- })(DisplayNameParts || (DisplayNameParts = {}));
21
- export var FormattedNumberParts = function (props) {
22
- var intl = useIntl();
23
- var value = props.value, children = props.children, formatProps = __rest(props, ["value", "children"]);
24
- return children(intl.formatNumberToParts(value, formatProps));
25
- };
26
- FormattedNumberParts.displayName = 'FormattedNumberParts';
27
- export var FormattedListParts = function (props) {
28
- var intl = useIntl();
29
- var value = props.value, children = props.children, formatProps = __rest(props, ["value", "children"]);
30
- return children(intl.formatListToParts(value, formatProps));
31
- };
32
- FormattedNumberParts.displayName = 'FormattedNumberParts';
33
- export function createFormattedDateTimePartsComponent(name) {
34
- var ComponentParts = function (props) {
35
- var intl = useIntl();
36
- var value = props.value, children = props.children, formatProps = __rest(props, ["value", "children"]);
37
- var date = typeof value === 'string' ? new Date(value || 0) : value;
38
- var formattedParts = name === 'formatDate'
39
- ? intl.formatDateToParts(date, formatProps)
40
- : intl.formatTimeToParts(date, formatProps);
41
- return children(formattedParts);
42
- };
43
- ComponentParts.displayName = DisplayNameParts[name];
44
- return ComponentParts;
45
- }
46
- export function createFormattedComponent(name) {
47
- var Component = function (props) {
48
- var intl = useIntl();
49
- var value = props.value, children = props.children, formatProps = __rest(props
50
- // TODO: fix TS type definition for localeMatcher upstream
51
- , ["value", "children"]);
52
- // TODO: fix TS type definition for localeMatcher upstream
53
- var formattedValue = intl[name](value, formatProps);
54
- if (typeof children === 'function') {
55
- return children(formattedValue);
56
- }
57
- var Text = intl.textComponent || React.Fragment;
58
- return React.createElement(Text, null, formattedValue);
59
- };
60
- Component.displayName = DisplayName[name];
61
- return Component;
62
- }
@@ -1,9 +0,0 @@
1
- import { CreateIntlFn } from '@formatjs/intl';
2
- import * as React from 'react';
3
- import type { IntlConfig, IntlShape } from '../types';
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,58 +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 { __assign, __rest, __spreadArray } from "tslib";
7
- import { createIntl as coreCreateIntl, formatMessage as coreFormatMessage, } from '@formatjs/intl';
8
- import { isFormatXMLElementFn, } from 'intl-messageformat';
9
- import { DEFAULT_INTL_CONFIG, assignUniqueKeysToParts, toKeyedReactNodeArray, } from '../utils';
10
- function assignUniqueKeysToFormatXMLElementFnArgument(values) {
11
- if (!values) {
12
- return values;
13
- }
14
- return Object.keys(values).reduce(function (acc, k) {
15
- var v = values[k];
16
- acc[k] = isFormatXMLElementFn(v)
17
- ? assignUniqueKeysToParts(v)
18
- : v;
19
- return acc;
20
- }, {});
21
- }
22
- var formatMessage = function (config, formatters, descriptor, rawValues) {
23
- var rest = [];
24
- for (var _i = 4; _i < arguments.length; _i++) {
25
- rest[_i - 4] = arguments[_i];
26
- }
27
- var values = assignUniqueKeysToFormatXMLElementFnArgument(rawValues);
28
- var chunks = coreFormatMessage.apply(void 0, __spreadArray([config,
29
- formatters,
30
- descriptor,
31
- values], rest, false));
32
- if (Array.isArray(chunks)) {
33
- return toKeyedReactNodeArray(chunks);
34
- }
35
- return chunks;
36
- };
37
- /**
38
- * Create intl object
39
- * @param config intl config
40
- * @param cache cache for formatter instances to prevent memory leak
41
- */
42
- export var createIntl = function (_a, cache) {
43
- var rawDefaultRichTextElements = _a.defaultRichTextElements, config = __rest(_a, ["defaultRichTextElements"]);
44
- var defaultRichTextElements = assignUniqueKeysToFormatXMLElementFnArgument(rawDefaultRichTextElements);
45
- var coreIntl = coreCreateIntl(__assign(__assign(__assign({}, DEFAULT_INTL_CONFIG), config), { defaultRichTextElements: defaultRichTextElements }), cache);
46
- var resolvedConfig = {
47
- locale: coreIntl.locale,
48
- timeZone: coreIntl.timeZone,
49
- fallbackOnEmptyString: coreIntl.fallbackOnEmptyString,
50
- formats: coreIntl.formats,
51
- defaultLocale: coreIntl.defaultLocale,
52
- defaultFormats: coreIntl.defaultFormats,
53
- messages: coreIntl.messages,
54
- onError: coreIntl.onError,
55
- defaultRichTextElements: defaultRichTextElements,
56
- };
57
- return __assign(__assign({}, coreIntl), { formatMessage: formatMessage.bind(null, resolvedConfig, coreIntl.formatters), $t: formatMessage.bind(null, resolvedConfig, coreIntl.formatters) });
58
- };
@@ -1,9 +0,0 @@
1
- import { 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,15 +0,0 @@
1
- import { __rest } from "tslib";
2
- import * as React from 'react';
3
- import useIntl from './useIntl';
4
- var FormattedDateTimeRange = function (props) {
5
- var intl = useIntl();
6
- var from = props.from, to = props.to, children = props.children, formatProps = __rest(props, ["from", "to", "children"]);
7
- var formattedValue = intl.formatDateTimeRange(from, to, formatProps);
8
- if (typeof children === 'function') {
9
- return children(formattedValue);
10
- }
11
- var Text = intl.textComponent || React.Fragment;
12
- return React.createElement(Text, null, formattedValue);
13
- };
14
- FormattedDateTimeRange.displayName = 'FormattedDateTimeRange';
15
- export default FormattedDateTimeRange;
@@ -1,39 +0,0 @@
1
- import * as React from 'react';
2
- import { IntlShape } from '../types';
3
- declare global {
4
- interface Window {
5
- /**
6
- * Set this to `true` prior to mounting to bypass using a globally-exposed context.
7
- */
8
- __REACT_INTL_BYPASS_GLOBAL_CONTEXT__: boolean | undefined;
9
- __REACT_INTL_CONTEXT__: React.Context<IntlShape> | undefined;
10
- }
11
- }
12
- export declare const Provider: React.Provider<IntlShape>;
13
- export declare const Context: React.Context<IntlShape>;
14
- export interface Opts<IntlPropName extends string = 'intl', ForwardRef extends boolean = false> {
15
- intlPropName?: IntlPropName;
16
- forwardRef?: ForwardRef;
17
- enforceContext?: boolean;
18
- }
19
- export type WrappedComponentProps<IntlPropName extends string = 'intl'> = {
20
- [k in IntlPropName]: IntlShape;
21
- };
22
- /**
23
- * Utility type to help deal with the fact that `Omit` doesn't play well with unions:
24
- * - https://github.com/microsoft/TypeScript/issues/31501
25
- * - https://github.com/microsoft/TypeScript/issues/28339
26
- *
27
- * @example
28
- * DistributedOmit<X | Y, K> --> Omit<X, K> | Omit<Y, K>
29
- */
30
- export type DistributedOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
31
- export type WithIntlProps<P> = DistributedOmit<P, keyof WrappedComponentProps> & {
32
- forwardedRef?: React.Ref<any>;
33
- };
34
- export default function injectIntl<IntlPropName extends string = 'intl', P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>>(WrappedComponent: React.ComponentType<P>, options?: Opts<IntlPropName, false>): React.FC<WithIntlProps<P>> & {
35
- WrappedComponent: React.ComponentType<P>;
36
- };
37
- export default function injectIntl<IntlPropName extends string = 'intl', P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>, T extends React.ComponentType<P> = any>(WrappedComponent: React.ComponentType<P>, options?: Opts<IntlPropName, true>): React.ForwardRefExoticComponent<React.PropsWithoutRef<WithIntlProps<React.PropsWithChildren<P>>> & React.RefAttributes<T>> & {
38
- WrappedComponent: React.ComponentType<P>;
39
- };
@@ -1,35 +0,0 @@
1
- import { __assign } from "tslib";
2
- import hoistNonReactStatics from 'hoist-non-react-statics';
3
- import * as React from 'react';
4
- import { invariantIntlContext } from '../utils';
5
- function getDisplayName(Component) {
6
- return Component.displayName || Component.name || 'Component';
7
- }
8
- // This is primarily dealing with packaging systems where multiple copies of react-intl
9
- // might exist
10
- var IntlContext = typeof window !== 'undefined' && !window.__REACT_INTL_BYPASS_GLOBAL_CONTEXT__
11
- ? window.__REACT_INTL_CONTEXT__ ||
12
- (window.__REACT_INTL_CONTEXT__ = React.createContext(null))
13
- : React.createContext(null);
14
- var IntlConsumer = IntlContext.Consumer, IntlProvider = IntlContext.Provider;
15
- export var Provider = IntlProvider;
16
- export var Context = IntlContext;
17
- export default function injectIntl(WrappedComponent, options) {
18
- var _a = options || {}, _b = _a.intlPropName, intlPropName = _b === void 0 ? 'intl' : _b, _c = _a.forwardRef, forwardRef = _c === void 0 ? false : _c, _d = _a.enforceContext, enforceContext = _d === void 0 ? true : _d;
19
- var WithIntl = function (props) { return (React.createElement(IntlConsumer, null, function (intl) {
20
- var _a;
21
- if (enforceContext) {
22
- invariantIntlContext(intl);
23
- }
24
- var intlProp = (_a = {}, _a[intlPropName] = intl, _a);
25
- return (React.createElement(WrappedComponent, __assign({}, props, intlProp, { ref: forwardRef ? props.forwardedRef : null })));
26
- })); };
27
- WithIntl.displayName = "injectIntl(".concat(getDisplayName(WrappedComponent), ")");
28
- WithIntl.WrappedComponent = WrappedComponent;
29
- if (forwardRef) {
30
- return hoistNonReactStatics(
31
- // @ts-expect-error
32
- React.forwardRef(function (props, ref) { return (React.createElement(WithIntl, __assign({}, props, { forwardedRef: ref }))); }), WrappedComponent);
33
- }
34
- return hoistNonReactStatics(WithIntl, WrappedComponent);
35
- }
@@ -1,11 +0,0 @@
1
- import type { FormatXMLElementFn, Options as IntlMessageFormatOptions, PrimitiveType } from 'intl-messageformat';
2
- import * as React from 'react';
3
- import { 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,35 +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 { __rest } from "tslib";
7
- import * as React from 'react';
8
- import { shallowEqual } from '../utils';
9
- import useIntl from './useIntl';
10
- function areEqual(prevProps, nextProps) {
11
- var values = prevProps.values, otherProps = __rest(prevProps, ["values"]);
12
- var nextValues = nextProps.values, nextOtherProps = __rest(nextProps, ["values"]);
13
- return (shallowEqual(nextValues, values) &&
14
- shallowEqual(otherProps, nextOtherProps));
15
- }
16
- function FormattedMessage(props) {
17
- var intl = useIntl();
18
- var formatMessage = intl.formatMessage, _a = intl.textComponent, Text = _a === void 0 ? React.Fragment : _a;
19
- var id = props.id, description = props.description, defaultMessage = props.defaultMessage, values = props.values, children = props.children, _b = props.tagName, Component = _b === void 0 ? Text : _b, ignoreTag = props.ignoreTag;
20
- var descriptor = { id: id, description: description, defaultMessage: defaultMessage };
21
- var nodes = formatMessage(descriptor, values, {
22
- ignoreTag: ignoreTag,
23
- });
24
- if (typeof children === 'function') {
25
- return children(Array.isArray(nodes) ? nodes : [nodes]);
26
- }
27
- if (Component) {
28
- return React.createElement(Component, null, nodes);
29
- }
30
- return React.createElement(React.Fragment, null, nodes);
31
- }
32
- FormattedMessage.displayName = 'FormattedMessage';
33
- var MemoizedFormattedMessage = React.memo(FormattedMessage, areEqual);
34
- MemoizedFormattedMessage.displayName = 'MemoizedFormattedMessage';
35
- export default MemoizedFormattedMessage;
@@ -1,14 +0,0 @@
1
- import * as React from 'react';
2
- import { 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,23 +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 useIntl from './useIntl';
8
- var FormattedPlural = function (props) {
9
- var _a = useIntl(), formatPlural = _a.formatPlural, Text = _a.textComponent;
10
- var value = props.value, other = props.other, children = props.children;
11
- var pluralCategory = formatPlural(value, props);
12
- var formattedPlural = props[pluralCategory] || other;
13
- if (typeof children === 'function') {
14
- return children(formattedPlural);
15
- }
16
- if (Text) {
17
- return React.createElement(Text, null, formattedPlural);
18
- }
19
- // Work around @types/react where React.FC cannot return string
20
- return formattedPlural;
21
- };
22
- FormattedPlural.displayName = 'FormattedPlural';
23
- export default FormattedPlural;
@@ -1,29 +0,0 @@
1
- import { IntlCache } from '@formatjs/intl';
2
- import * as React from 'react';
3
- import type { IntlConfig, IntlShape } from '../types';
4
- import { DefaultIntlConfig } from '../utils';
5
- interface State {
6
- /**
7
- * Explicit intl cache to prevent memory leaks
8
- */
9
- cache: IntlCache;
10
- /**
11
- * Intl object we created
12
- */
13
- intl?: IntlShape;
14
- /**
15
- * list of memoized config we care about.
16
- * This is important since creating intl is
17
- * very expensive
18
- */
19
- prevConfig: IntlConfig;
20
- }
21
- export default class IntlProvider extends React.PureComponent<React.PropsWithChildren<IntlConfig>, State> {
22
- static displayName: string;
23
- static defaultProps: DefaultIntlConfig;
24
- private cache;
25
- state: State;
26
- static getDerivedStateFromProps(props: Readonly<IntlConfig>, { prevConfig, cache }: State): Partial<State> | null;
27
- render(): React.JSX.Element;
28
- }
29
- export {};
@@ -1,59 +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 { __extends } from "tslib";
7
- import { createIntlCache } from '@formatjs/intl';
8
- import * as React from 'react';
9
- import { DEFAULT_INTL_CONFIG, invariantIntlContext, shallowEqual, } from '../utils';
10
- import { createIntl } from './createIntl';
11
- import { Provider } from './injectIntl';
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
- var IntlProvider = /** @class */ (function (_super) {
29
- __extends(IntlProvider, _super);
30
- function IntlProvider() {
31
- var _this = _super !== null && _super.apply(this, arguments) || this;
32
- _this.cache = createIntlCache();
33
- _this.state = {
34
- cache: _this.cache,
35
- intl: createIntl(processIntlConfig(_this.props), _this.cache),
36
- prevConfig: processIntlConfig(_this.props),
37
- };
38
- return _this;
39
- }
40
- IntlProvider.getDerivedStateFromProps = function (props, _a) {
41
- var prevConfig = _a.prevConfig, cache = _a.cache;
42
- var config = processIntlConfig(props);
43
- if (!shallowEqual(prevConfig, config)) {
44
- return {
45
- intl: createIntl(config, cache),
46
- prevConfig: config,
47
- };
48
- }
49
- return null;
50
- };
51
- IntlProvider.prototype.render = function () {
52
- invariantIntlContext(this.state.intl);
53
- return React.createElement(Provider, { value: this.state.intl }, this.props.children);
54
- };
55
- IntlProvider.displayName = 'IntlProvider';
56
- IntlProvider.defaultProps = DEFAULT_INTL_CONFIG;
57
- return IntlProvider;
58
- }(React.PureComponent));
59
- export default IntlProvider;
@@ -1,10 +0,0 @@
1
- import * as React from 'react';
2
- import { 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,125 +0,0 @@
1
- import { __assign, __rest } from "tslib";
2
- /*
3
- * Copyright 2015, Yahoo Inc.
4
- * Copyrights licensed under the New BSD License.
5
- * See the accompanying LICENSE file for terms.
6
- */
7
- import * as React from 'react';
8
- import { invariant } from '../utils';
9
- import useIntl from './useIntl';
10
- var MINUTE = 60;
11
- var HOUR = 60 * 60;
12
- var DAY = 60 * 60 * 24;
13
- function selectUnit(seconds) {
14
- var absValue = Math.abs(seconds);
15
- if (absValue < MINUTE) {
16
- return 'second';
17
- }
18
- if (absValue < HOUR) {
19
- return 'minute';
20
- }
21
- if (absValue < DAY) {
22
- return 'hour';
23
- }
24
- return 'day';
25
- }
26
- function getDurationInSeconds(unit) {
27
- switch (unit) {
28
- case 'second':
29
- return 1;
30
- case 'minute':
31
- return MINUTE;
32
- case 'hour':
33
- return HOUR;
34
- default:
35
- return DAY;
36
- }
37
- }
38
- function valueToSeconds(value, unit) {
39
- if (!value) {
40
- return 0;
41
- }
42
- switch (unit) {
43
- case 'second':
44
- return value;
45
- case 'minute':
46
- return value * MINUTE;
47
- default:
48
- return value * HOUR;
49
- }
50
- }
51
- var INCREMENTABLE_UNITS = [
52
- 'second',
53
- 'minute',
54
- 'hour',
55
- ];
56
- function canIncrement(unit) {
57
- if (unit === void 0) { unit = 'second'; }
58
- return INCREMENTABLE_UNITS.indexOf(unit) > -1;
59
- }
60
- var SimpleFormattedRelativeTime = function (props) {
61
- var _a = useIntl(), formatRelativeTime = _a.formatRelativeTime, Text = _a.textComponent;
62
- var children = props.children, value = props.value, unit = props.unit, otherProps = __rest(props, ["children", "value", "unit"]);
63
- var formattedRelativeTime = formatRelativeTime(value || 0, unit, otherProps);
64
- if (typeof children === 'function') {
65
- return children(formattedRelativeTime);
66
- }
67
- if (Text) {
68
- return React.createElement(Text, null, formattedRelativeTime);
69
- }
70
- return React.createElement(React.Fragment, null, formattedRelativeTime);
71
- };
72
- var FormattedRelativeTime = function (_a) {
73
- var _b = _a.value, value = _b === void 0 ? 0 : _b, _c = _a.unit, unit = _c === void 0 ? 'second' : _c, updateIntervalInSeconds = _a.updateIntervalInSeconds, otherProps = __rest(_a, ["value", "unit", "updateIntervalInSeconds"]);
74
- invariant(!updateIntervalInSeconds ||
75
- !!(updateIntervalInSeconds && canIncrement(unit)), 'Cannot schedule update with unit longer than hour');
76
- var _d = React.useState(), prevUnit = _d[0], setPrevUnit = _d[1];
77
- var _e = React.useState(0), prevValue = _e[0], setPrevValue = _e[1];
78
- var _f = React.useState(0), currentValueInSeconds = _f[0], setCurrentValueInSeconds = _f[1];
79
- var updateTimer;
80
- if (unit !== prevUnit || value !== prevValue) {
81
- setPrevValue(value || 0);
82
- setPrevUnit(unit);
83
- setCurrentValueInSeconds(canIncrement(unit) ? valueToSeconds(value, unit) : 0);
84
- }
85
- React.useEffect(function () {
86
- function clearUpdateTimer() {
87
- clearTimeout(updateTimer);
88
- }
89
- clearUpdateTimer();
90
- // If there's no interval and we cannot increment this unit, do nothing
91
- if (!updateIntervalInSeconds || !canIncrement(unit)) {
92
- return clearUpdateTimer;
93
- }
94
- // Figure out the next interesting time
95
- var nextValueInSeconds = currentValueInSeconds - updateIntervalInSeconds;
96
- var nextUnit = selectUnit(nextValueInSeconds);
97
- // We've reached the max auto incrementable unit, don't schedule another update
98
- if (nextUnit === 'day') {
99
- return clearUpdateTimer;
100
- }
101
- var unitDuration = getDurationInSeconds(nextUnit);
102
- var remainder = nextValueInSeconds % unitDuration;
103
- var prevInterestingValueInSeconds = nextValueInSeconds - remainder;
104
- var nextInterestingValueInSeconds = prevInterestingValueInSeconds >= currentValueInSeconds
105
- ? prevInterestingValueInSeconds - unitDuration
106
- : prevInterestingValueInSeconds;
107
- var delayInSeconds = Math.abs(nextInterestingValueInSeconds - currentValueInSeconds);
108
- if (currentValueInSeconds !== nextInterestingValueInSeconds) {
109
- updateTimer = setTimeout(function () { return setCurrentValueInSeconds(nextInterestingValueInSeconds); }, delayInSeconds * 1e3);
110
- }
111
- return clearUpdateTimer;
112
- }, [currentValueInSeconds, updateIntervalInSeconds, unit]);
113
- var currentValue = value || 0;
114
- var currentUnit = unit;
115
- if (canIncrement(unit) &&
116
- typeof currentValueInSeconds === 'number' &&
117
- updateIntervalInSeconds) {
118
- currentUnit = selectUnit(currentValueInSeconds);
119
- var unitDuration = getDurationInSeconds(currentUnit);
120
- currentValue = Math.round(currentValueInSeconds / unitDuration);
121
- }
122
- return (React.createElement(SimpleFormattedRelativeTime, __assign({ value: currentValue, unit: currentUnit }, otherProps)));
123
- };
124
- FormattedRelativeTime.displayName = 'FormattedRelativeTime';
125
- export default FormattedRelativeTime;
@@ -1,2 +0,0 @@
1
- import { IntlShape } from '../types';
2
- export default function useIntl(this: void): IntlShape;
@@ -1,8 +0,0 @@
1
- import * as React from 'react';
2
- import { invariantIntlContext } from '../utils';
3
- import { Context } from './injectIntl';
4
- export default function useIntl() {
5
- var intl = React.useContext(Context);
6
- invariantIntlContext(intl);
7
- return intl;
8
- }
@@ -1,14 +0,0 @@
1
- import { ResolvedIntlConfig as CoreResolvedIntlConfig, Formatters, IntlFormatters, MessageDescriptor } from '@formatjs/intl';
2
- import { FormatXMLElementFn, Options as IntlMessageFormatOptions, PrimitiveType } from 'intl-messageformat';
3
- import * as React from 'react';
4
- import { DEFAULT_INTL_CONFIG } from './utils';
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/lib/src/types.js DELETED
@@ -1 +0,0 @@
1
- export {};