react-intl 4.2.2 → 4.5.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +1 -1
  3. package/dist/components/createFormattedComponent.js +3 -1
  4. package/dist/components/injectIntl.js +2 -3
  5. package/dist/components/message.d.ts +1 -1
  6. package/dist/error.d.ts +4 -2
  7. package/dist/error.js +2 -1
  8. package/dist/formatters/message.js +4 -4
  9. package/dist/formatters/relativeTime.js +1 -4
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.js +4 -0
  12. package/dist/react-intl.api.md +42 -35
  13. package/dist/react-intl.d.ts +72 -59
  14. package/dist/react-intl.js +74 -31
  15. package/dist/react-intl.js.map +1 -1
  16. package/dist/react-intl.min.js +1 -1
  17. package/dist/react-intl.min.js.map +1 -1
  18. package/dist/types.d.ts +2 -2
  19. package/lib/components/createFormattedComponent.js +3 -1
  20. package/lib/components/injectIntl.js +2 -3
  21. package/lib/components/message.d.ts +1 -1
  22. package/lib/error.d.ts +4 -2
  23. package/lib/error.js +2 -1
  24. package/lib/formatters/message.js +4 -4
  25. package/lib/formatters/relativeTime.js +1 -4
  26. package/lib/index.d.ts +1 -0
  27. package/lib/index.js +3 -0
  28. package/lib/react-intl.d.ts +51 -47
  29. package/lib/tsdoc-metadata.json +1 -1
  30. package/lib/types.d.ts +2 -2
  31. package/package.json +32 -39
  32. package/src/components/createFormattedComponent.tsx +50 -48
  33. package/src/components/injectIntl.tsx +33 -34
  34. package/src/components/message.tsx +33 -32
  35. package/src/components/plural.tsx +22 -22
  36. package/src/components/provider.tsx +36 -36
  37. package/src/components/relative.tsx +70 -70
  38. package/src/components/useIntl.ts +7 -7
  39. package/src/error.ts +26 -13
  40. package/src/formatters/dateTime.ts +25 -33
  41. package/src/formatters/displayName.ts +11 -11
  42. package/src/formatters/list.ts +29 -29
  43. package/src/formatters/message.ts +47 -50
  44. package/src/formatters/number.ts +15 -15
  45. package/src/formatters/plural.ts +10 -10
  46. package/src/formatters/relativeTime.ts +18 -19
  47. package/src/index.ts +49 -36
  48. package/src/tsconfig.cjs.json +8 -8
  49. package/src/tsconfig.json +7 -7
  50. package/src/types.ts +59 -59
  51. package/src/utils.ts +29 -24
  52. package/src/vendor.d.ts +1 -1
@@ -1,13 +1,13 @@
1
- import * as React from 'react';
2
- import {invariantIntlContext} from '../utils';
1
+ import * as React from 'react'
2
+ import {invariantIntlContext} from '../utils'
3
3
  import {
4
4
  IntlShape,
5
5
  FormatDateOptions,
6
6
  FormatNumberOptions,
7
7
  FormatListOptions,
8
8
  FormatDisplayNameOptions,
9
- } from '../types';
10
- import {Context} from './injectIntl';
9
+ } from '../types'
10
+ import {Context} from './injectIntl'
11
11
 
12
12
  enum DisplayName {
13
13
  formatDate = 'FormattedDate',
@@ -27,27 +27,29 @@ enum DisplayNameParts {
27
27
  }
28
28
 
29
29
  type Formatter = {
30
- formatDate: FormatDateOptions;
31
- formatTime: FormatDateOptions;
32
- formatNumber: FormatNumberOptions;
33
- formatList: FormatListOptions;
34
- formatDisplayName: FormatDisplayNameOptions;
35
- };
30
+ formatDate: FormatDateOptions
31
+ formatTime: FormatDateOptions
32
+ formatNumber: FormatNumberOptions
33
+ formatList: FormatListOptions
34
+ formatDisplayName: FormatDisplayNameOptions
35
+ }
36
36
 
37
- export const FormattedNumberParts: React.FC<Formatter['formatNumber'] & {
38
- value: Parameters<IntlShape['formatNumber']>[0];
37
+ export const FormattedNumberParts: React.FC<
38
+ Formatter['formatNumber'] & {
39
+ value: Parameters<IntlShape['formatNumber']>[0]
39
40
 
40
- children(val: Intl.NumberFormatPart[]): React.ReactElement | null;
41
- }> = props => (
41
+ children(val: Intl.NumberFormatPart[]): React.ReactElement | null
42
+ }
43
+ > = props => (
42
44
  <Context.Consumer>
43
45
  {(intl): React.ReactElement | null => {
44
- invariantIntlContext(intl);
45
- const {value, children, ...formatProps} = props;
46
- return children(intl.formatNumberToParts(value, formatProps));
46
+ invariantIntlContext(intl)
47
+ const {value, children, ...formatProps} = props
48
+ return children(intl.formatNumberToParts(value, formatProps))
47
49
  }}
48
50
  </Context.Consumer>
49
- );
50
- FormattedNumberParts.displayName = 'FormattedNumberParts';
51
+ )
52
+ FormattedNumberParts.displayName = 'FormattedNumberParts'
51
53
 
52
54
  export function createFormattedDateTimePartsComponent<
53
55
  Name extends 'formatDate' | 'formatTime'
@@ -55,65 +57,65 @@ export function createFormattedDateTimePartsComponent<
55
57
  name: Name
56
58
  ): React.FC<
57
59
  Formatter[Name] & {
58
- value: Parameters<IntlShape[Name]>[0];
59
- children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null;
60
+ value: Parameters<IntlShape[Name]>[0]
61
+ children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
60
62
  }
61
63
  > {
62
- type FormatFn = IntlShape[Name];
64
+ type FormatFn = IntlShape[Name]
63
65
  type Props = Formatter[Name] & {
64
- value: Parameters<FormatFn>[0];
65
- children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null;
66
- };
66
+ value: Parameters<FormatFn>[0]
67
+ children(val: Intl.DateTimeFormatPart[]): React.ReactElement | null
68
+ }
67
69
 
68
70
  const ComponentParts: React.FC<Props> = props => (
69
71
  <Context.Consumer>
70
72
  {(intl): React.ReactElement | null => {
71
- invariantIntlContext(intl);
72
- const {value, children, ...formatProps} = props;
73
- const date = typeof value === 'string' ? new Date(value || 0) : value;
73
+ invariantIntlContext(intl)
74
+ const {value, children, ...formatProps} = props
75
+ const date = typeof value === 'string' ? new Date(value || 0) : value
74
76
  const formattedParts: Intl.DateTimeFormatPart[] =
75
77
  name === 'formatDate'
76
78
  ? intl.formatDateToParts(date, formatProps)
77
- : intl.formatTimeToParts(date, formatProps);
79
+ : intl.formatTimeToParts(date, formatProps)
78
80
 
79
- return children(formattedParts);
81
+ return children(formattedParts)
80
82
  }}
81
83
  </Context.Consumer>
82
- );
83
- ComponentParts.displayName = DisplayNameParts[name];
84
- return ComponentParts;
84
+ )
85
+ ComponentParts.displayName = DisplayNameParts[name]
86
+ return ComponentParts
85
87
  }
86
88
 
87
89
  export function createFormattedComponent<Name extends keyof Formatter>(
88
90
  name: Name
89
91
  ): React.FC<
90
92
  Formatter[Name] & {
91
- value: Parameters<IntlShape[Name]>[0];
92
- children?(val: string): React.ReactElement | null;
93
+ value: Parameters<IntlShape[Name]>[0]
94
+ children?(val: string): React.ReactElement | null
93
95
  }
94
96
  > {
95
- type FormatFn = IntlShape[Name];
97
+ type FormatFn = IntlShape[Name]
96
98
  type Props = Formatter[Name] & {
97
- value: Parameters<FormatFn>[0];
98
- children?(val: string): React.ReactElement | null;
99
- };
99
+ value: Parameters<FormatFn>[0]
100
+ children?(val: string): React.ReactElement | null
101
+ }
100
102
 
101
103
  const Component: React.FC<Props> = props => (
102
104
  <Context.Consumer>
103
105
  {(intl): JSX.Element | null => {
104
- invariantIntlContext(intl);
105
- const {value, children, ...formatProps} = props;
106
+ invariantIntlContext(intl)
107
+ const {value, children, ...formatProps} = props
106
108
  // TODO: fix TS type definition for localeMatcher upstream
107
- const formattedValue = intl[name](value as any, formatProps as any);
109
+ const formattedValue = intl[name](value as any, formatProps as any)
108
110
 
109
111
  if (typeof children === 'function') {
110
- return children(formattedValue as any);
112
+ return children(formattedValue as any)
111
113
  }
112
- const Text = intl.textComponent || React.Fragment;
113
- return <Text>{formattedValue}</Text>;
114
+ const Text = intl.textComponent || React.Fragment
115
+ return <Text>{formattedValue}</Text>
114
116
  }}
115
117
  </Context.Consumer>
116
- );
117
- Component.displayName = DisplayName[name];
118
- return Component;
118
+ )
119
+ Component.displayName = DisplayName[name]
120
+ return Component
119
121
  }
@@ -1,41 +1,41 @@
1
- import * as React from 'react';
2
- import * as hoistNonReactStatics_ from 'hoist-non-react-statics';
1
+ import * as React from 'react'
2
+ import * as hoistNonReactStatics_ from 'hoist-non-react-statics'
3
3
  // Since rollup cannot deal with namespace being a function,
4
4
  // this is to interop with TypeScript since `invariant`
5
5
  // does not export a default
6
6
  // https://github.com/rollup/rollup/issues/1267
7
7
  const hoistNonReactStatics: typeof hoistNonReactStatics_ =
8
- (hoistNonReactStatics_ as any).default || hoistNonReactStatics_;
9
- import {invariantIntlContext} from '../utils';
10
- import {IntlShape, Omit} from '../types';
8
+ (hoistNonReactStatics_ as any).default || hoistNonReactStatics_
9
+ import {invariantIntlContext} from '../utils'
10
+ import {IntlShape, Omit} from '../types'
11
11
 
12
12
  function getDisplayName(Component: React.ComponentType<any>): string {
13
- return Component.displayName || Component.name || 'Component';
13
+ return Component.displayName || Component.name || 'Component'
14
14
  }
15
15
 
16
16
  // TODO: We should provide initial value here
17
- const IntlContext = React.createContext<IntlShape>(null as any);
18
- const {Consumer: IntlConsumer, Provider: IntlProvider} = IntlContext;
17
+ const IntlContext = React.createContext<IntlShape>(null as any)
18
+ const {Consumer: IntlConsumer, Provider: IntlProvider} = IntlContext
19
19
 
20
- export const Provider = IntlProvider;
21
- export const Context = IntlContext;
20
+ export const Provider = IntlProvider
21
+ export const Context = IntlContext
22
22
 
23
23
  export interface Opts<
24
24
  IntlPropName extends string = 'intl',
25
25
  ForwardRef extends boolean = false
26
26
  > {
27
- intlPropName?: IntlPropName;
28
- forwardRef?: ForwardRef;
29
- enforceContext?: boolean;
27
+ intlPropName?: IntlPropName
28
+ forwardRef?: ForwardRef
29
+ enforceContext?: boolean
30
30
  }
31
31
 
32
32
  export type WrappedComponentProps<IntlPropName extends string = 'intl'> = {
33
- [k in IntlPropName]: IntlShape;
34
- };
33
+ [k in IntlPropName]: IntlShape
34
+ }
35
35
 
36
36
  export type WithIntlProps<P> = Omit<P, keyof WrappedComponentProps> & {
37
- forwardedRef?: React.Ref<any>;
38
- };
37
+ forwardedRef?: React.Ref<any>
38
+ }
39
39
 
40
40
  export default function injectIntl<
41
41
  IntlPropName extends string,
@@ -44,8 +44,8 @@ export default function injectIntl<
44
44
  WrappedComponent: React.ComponentType<P>,
45
45
  options?: Opts<IntlPropName, false>
46
46
  ): React.FC<WithIntlProps<P>> & {
47
- WrappedComponent: React.ComponentType<P>;
48
- };
47
+ WrappedComponent: React.ComponentType<P>
48
+ }
49
49
  export default function injectIntl<
50
50
  IntlPropName extends string = 'intl',
51
51
  P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>,
@@ -56,8 +56,8 @@ export default function injectIntl<
56
56
  ): React.ForwardRefExoticComponent<
57
57
  React.PropsWithoutRef<WithIntlProps<P>> & React.RefAttributes<T>
58
58
  > & {
59
- WrappedComponent: React.ComponentType<P>;
60
- };
59
+ WrappedComponent: React.ComponentType<P>
60
+ }
61
61
  export default function injectIntl<
62
62
  IntlPropName extends string = 'intl',
63
63
  P extends WrappedComponentProps<IntlPropName> = WrappedComponentProps<any>,
@@ -69,34 +69,33 @@ export default function injectIntl<
69
69
  ): React.ForwardRefExoticComponent<
70
70
  React.PropsWithoutRef<WithIntlProps<P>> & React.RefAttributes<T>
71
71
  > & {
72
- WrappedComponent: React.ComponentType<P>;
72
+ WrappedComponent: React.ComponentType<P>
73
73
  } {
74
74
  const {intlPropName = 'intl', forwardRef = false, enforceContext = true} =
75
- options || {};
75
+ options || {}
76
76
 
77
77
  const WithIntl: React.FC<P & {forwardedRef?: React.Ref<any>}> & {
78
- WrappedComponent: React.ComponentType<P>;
78
+ WrappedComponent: React.ComponentType<P>
79
79
  } = props => (
80
80
  <IntlConsumer>
81
81
  {(intl): React.ReactNode => {
82
82
  if (enforceContext) {
83
- invariantIntlContext(intl);
83
+ invariantIntlContext(intl)
84
84
  }
85
+ const intlProp = {[intlPropName]: intl}
85
86
 
86
87
  return (
87
88
  <WrappedComponent
88
89
  {...props}
89
- {...{
90
- [intlPropName]: intl,
91
- }}
90
+ {...intlProp}
92
91
  ref={forwardRef ? props.forwardedRef : null}
93
92
  />
94
- );
93
+ )
95
94
  }}
96
95
  </IntlConsumer>
97
- );
98
- WithIntl.displayName = `injectIntl(${getDisplayName(WrappedComponent)})`;
99
- WithIntl.WrappedComponent = WrappedComponent;
96
+ )
97
+ WithIntl.displayName = `injectIntl(${getDisplayName(WrappedComponent)})`
98
+ WithIntl.WrappedComponent = WrappedComponent
100
99
 
101
100
  if (forwardRef) {
102
101
  return hoistNonReactStatics(
@@ -104,8 +103,8 @@ export default function injectIntl<
104
103
  <WithIntl {...props} forwardedRef={ref} />
105
104
  )),
106
105
  WrappedComponent
107
- ) as any;
106
+ ) as any
108
107
  }
109
108
 
110
- return hoistNonReactStatics(WithIntl, WrappedComponent) as any;
109
+ return hoistNonReactStatics(WithIntl, WrappedComponent) as any
111
110
  }
@@ -4,31 +4,31 @@
4
4
  * See the accompanying LICENSE file for terms.
5
5
  */
6
6
 
7
- import * as React from 'react';
8
- import {PrimitiveType, FormatXMLElementFn} from 'intl-messageformat';
9
- import {Context} from './injectIntl';
10
- import {MessageDescriptor} from '../types';
11
- import {formatMessage} from '../formatters/message';
7
+ import * as React from 'react'
8
+ import {PrimitiveType, FormatXMLElementFn} from 'intl-messageformat'
9
+ import {Context} from './injectIntl'
10
+ import {MessageDescriptor} from '../types'
11
+ import {formatMessage} from '../formatters/message'
12
12
  import {
13
13
  invariantIntlContext,
14
14
  DEFAULT_INTL_CONFIG,
15
15
  createFormatters,
16
- } from '../utils';
17
- import * as shallowEquals_ from 'shallow-equal/objects';
16
+ } from '../utils'
17
+ import * as shallowEquals_ from 'shallow-equal/objects'
18
18
  const shallowEquals: typeof shallowEquals_ =
19
- (shallowEquals_ as any).default || shallowEquals_;
19
+ (shallowEquals_ as any).default || shallowEquals_
20
20
 
21
21
  function defaultFormatMessage<T = React.ReactNode>(
22
22
  descriptor: MessageDescriptor,
23
23
  values?: Record<
24
24
  string,
25
- PrimitiveType | React.ReactElement | FormatXMLElementFn<T>
25
+ PrimitiveType | React.ReactElement | FormatXMLElementFn<T, T>
26
26
  >
27
27
  ): string {
28
28
  if (process.env.NODE_ENV !== 'production') {
29
29
  console.error(
30
30
  '[React Intl] Could not find required `intl` object. <IntlProvider> needs to exist in the component ancestry. Using default message as fallback.'
31
- );
31
+ )
32
32
  }
33
33
 
34
34
  return formatMessage(
@@ -39,36 +39,37 @@ function defaultFormatMessage<T = React.ReactNode>(
39
39
  createFormatters(),
40
40
  descriptor,
41
41
  values as any
42
- );
42
+ )
43
43
  }
44
44
 
45
45
  export interface Props<
46
46
  V extends Record<string, any> = Record<string, React.ReactNode>
47
47
  > extends MessageDescriptor {
48
- values?: V;
49
- tagName?: React.ElementType<any>;
50
- children?(...nodes: React.ReactNodeArray): React.ReactNode;
48
+ values?: V
49
+ tagName?: React.ElementType<any>
50
+ children?(...nodes: React.ReactNodeArray): React.ReactNode
51
51
  }
52
52
 
53
53
  class FormattedMessage<
54
- T = React.ReactNode,
55
54
  V extends Record<string, any> = Record<
56
55
  string,
57
- PrimitiveType | React.ReactElement | FormatXMLElementFn<T>
56
+ | PrimitiveType
57
+ | React.ReactElement
58
+ | FormatXMLElementFn<React.ReactNode, React.ReactNode>
58
59
  >
59
60
  > extends React.Component<Props<V>> {
60
- static displayName = 'FormattedMessage';
61
+ static displayName = 'FormattedMessage'
61
62
  static defaultProps = {
62
63
  values: {},
63
- };
64
+ }
64
65
 
65
66
  shouldComponentUpdate(nextProps: Props<V>): boolean {
66
- const {values, ...otherProps} = this.props;
67
- const {values: nextValues, ...nextOtherProps} = nextProps;
67
+ const {values, ...otherProps} = this.props
68
+ const {values: nextValues, ...nextOtherProps} = nextProps
68
69
  return (
69
70
  !shallowEquals(nextValues, values) ||
70
71
  !shallowEquals(otherProps, nextOtherProps)
71
- );
72
+ )
72
73
  }
73
74
 
74
75
  render(): JSX.Element {
@@ -76,13 +77,13 @@ class FormattedMessage<
76
77
  <Context.Consumer>
77
78
  {(intl): React.ReactNode => {
78
79
  if (!this.props.defaultMessage) {
79
- invariantIntlContext(intl);
80
+ invariantIntlContext(intl)
80
81
  }
81
82
 
82
83
  const {
83
84
  formatMessage = defaultFormatMessage,
84
85
  textComponent: Text = React.Fragment,
85
- } = intl || {};
86
+ } = intl || {}
86
87
  const {
87
88
  id,
88
89
  description,
@@ -90,32 +91,32 @@ class FormattedMessage<
90
91
  values,
91
92
  children,
92
93
  tagName: Component = Text,
93
- } = this.props;
94
+ } = this.props
94
95
 
95
- const descriptor = {id, description, defaultMessage};
96
+ const descriptor = {id, description, defaultMessage}
96
97
  let nodes: string | React.ReactNodeArray = formatMessage(
97
98
  descriptor,
98
99
  values
99
- );
100
+ )
100
101
 
101
102
  if (!Array.isArray(nodes)) {
102
- nodes = [nodes];
103
+ nodes = [nodes]
103
104
  }
104
105
 
105
106
  if (typeof children === 'function') {
106
- return children(...nodes);
107
+ return children(...nodes)
107
108
  }
108
109
 
109
110
  if (Component) {
110
111
  // Needs to use `createElement()` instead of JSX, otherwise React will
111
112
  // warn about a missing `key` prop with rich-text message formatting.
112
- return React.createElement(Component, null, ...nodes);
113
+ return React.createElement(Component, null, ...nodes)
113
114
  }
114
- return nodes;
115
+ return nodes
115
116
  }}
116
117
  </Context.Consumer>
117
- );
118
+ )
118
119
  }
119
120
  }
120
121
 
121
- export default FormattedMessage;
122
+ export default FormattedMessage
@@ -4,20 +4,20 @@
4
4
  * See the accompanying LICENSE file for terms.
5
5
  */
6
6
 
7
- import * as React from 'react';
8
- import withIntl from './injectIntl';
9
- import {IntlShape, FormatPluralOptions} from '../types';
7
+ import * as React from 'react'
8
+ import withIntl from './injectIntl'
9
+ import {IntlShape, FormatPluralOptions} from '../types'
10
10
 
11
11
  interface Props extends FormatPluralOptions {
12
- value: number;
13
- intl: IntlShape;
14
- other: React.ReactNode;
15
- zero?: React.ReactNode;
16
- one?: React.ReactNode;
17
- two?: React.ReactNode;
18
- few?: React.ReactNode;
19
- many?: React.ReactNode;
20
- children?(value: React.ReactNode): React.ReactElement | null;
12
+ value: number
13
+ intl: IntlShape
14
+ other: React.ReactNode
15
+ zero?: React.ReactNode
16
+ one?: React.ReactNode
17
+ two?: React.ReactNode
18
+ few?: React.ReactNode
19
+ many?: React.ReactNode
20
+ children?(value: React.ReactNode): React.ReactElement | null
21
21
  }
22
22
 
23
23
  const FormattedPlural: React.FC<Props> = props => {
@@ -26,25 +26,25 @@ const FormattedPlural: React.FC<Props> = props => {
26
26
  other,
27
27
  children,
28
28
  intl: {formatPlural, textComponent: Text},
29
- } = props;
29
+ } = props
30
30
 
31
- const pluralCategory = formatPlural(value, props);
32
- const formattedPlural = props[pluralCategory as 'one'] || other;
31
+ const pluralCategory = formatPlural(value, props)
32
+ const formattedPlural = props[pluralCategory as 'one'] || other
33
33
 
34
34
  if (typeof children === 'function') {
35
- return children(formattedPlural);
35
+ return children(formattedPlural)
36
36
  }
37
37
  if (Text) {
38
- return <Text>{formattedPlural}</Text>;
38
+ return <Text>{formattedPlural}</Text>
39
39
  }
40
40
  // Work around @types/react where React.FC cannot return string
41
- return formattedPlural as any;
42
- };
41
+ return formattedPlural as any
42
+ }
43
43
 
44
44
  FormattedPlural.defaultProps = {
45
45
  type: 'cardinal',
46
- };
46
+ }
47
47
 
48
- FormattedPlural.displayName = 'FormattedPlural';
48
+ FormattedPlural.displayName = 'FormattedPlural'
49
49
 
50
- export default withIntl(FormattedPlural);
50
+ export default withIntl(FormattedPlural)