react-intl 3.12.0 → 4.1.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 (66) hide show
  1. package/CHANGELOG.md +47 -0
  2. package/dist/components/message.d.ts +1 -1
  3. package/dist/components/message.js +2 -2
  4. package/dist/components/plural.d.ts +1 -1
  5. package/dist/components/provider.js +13 -8
  6. package/dist/error.d.ts +11 -0
  7. package/dist/error.js +28 -0
  8. package/dist/formatters/dateTime.js +5 -4
  9. package/dist/formatters/displayName.js +4 -2
  10. package/dist/formatters/list.js +4 -2
  11. package/dist/formatters/message.d.ts +1 -2
  12. package/dist/formatters/message.js +17 -34
  13. package/dist/formatters/number.js +3 -2
  14. package/dist/formatters/plural.js +4 -2
  15. package/dist/formatters/relativeTime.js +4 -2
  16. package/dist/index.d.ts +2 -2
  17. package/dist/index.js +6 -4
  18. package/dist/react-intl.api.md +29 -22
  19. package/dist/react-intl.d.ts +77 -36
  20. package/dist/react-intl.js +6267 -5860
  21. package/dist/react-intl.js.map +1 -1
  22. package/dist/react-intl.min.js +1 -1
  23. package/dist/react-intl.min.js.map +1 -1
  24. package/dist/types.d.ts +5 -5
  25. package/dist/utils.d.ts +3 -4
  26. package/dist/utils.js +2 -18
  27. package/lib/components/message.d.ts +1 -1
  28. package/lib/components/message.js +2 -2
  29. package/lib/components/plural.d.ts +1 -1
  30. package/lib/components/provider.js +15 -10
  31. package/lib/error.d.ts +11 -0
  32. package/lib/error.js +9 -0
  33. package/lib/formatters/dateTime.js +6 -5
  34. package/lib/formatters/displayName.js +6 -4
  35. package/lib/formatters/list.js +6 -4
  36. package/lib/formatters/message.d.ts +1 -2
  37. package/lib/formatters/message.js +17 -31
  38. package/lib/formatters/number.js +4 -3
  39. package/lib/formatters/plural.js +6 -4
  40. package/lib/formatters/relativeTime.js +6 -4
  41. package/lib/index.d.ts +2 -2
  42. package/lib/index.js +4 -2
  43. package/lib/react-intl.d.ts +21 -17
  44. package/lib/tsdoc-metadata.json +1 -1
  45. package/lib/types.d.ts +5 -5
  46. package/lib/utils.d.ts +3 -4
  47. package/lib/utils.js +2 -16
  48. package/package.json +45 -47
  49. package/src/components/message.tsx +6 -5
  50. package/src/components/provider.tsx +25 -14
  51. package/src/error.ts +18 -0
  52. package/src/formatters/dateTime.ts +30 -5
  53. package/src/formatters/displayName.ts +15 -4
  54. package/src/formatters/list.ts +15 -4
  55. package/src/formatters/message.ts +49 -66
  56. package/src/formatters/number.ts +16 -3
  57. package/src/formatters/plural.ts +15 -4
  58. package/src/formatters/relativeTime.ts +15 -4
  59. package/src/index.ts +4 -2
  60. package/src/types.ts +5 -7
  61. package/src/utils.ts +4 -25
  62. package/dist/components/html-message.d.ts +0 -11
  63. package/dist/components/html-message.js +0 -75
  64. package/lib/components/html-message.d.ts +0 -11
  65. package/lib/components/html-message.js +0 -43
  66. package/src/components/html-message.tsx +0 -68
@@ -14,11 +14,11 @@ import {
14
14
  CustomFormats,
15
15
  } from '../types';
16
16
 
17
- import {createError, escape} from '../utils';
18
17
  import IntlMessageFormat, {
19
18
  FormatXMLElementFn,
20
19
  PrimitiveType,
21
20
  } from 'intl-messageformat';
21
+ import {ReactIntlError, ReactIntlErrorCode} from '../error';
22
22
 
23
23
  function setTimeZoneInOptions(
24
24
  opts: Record<string, Intl.DateTimeFormatOptions>,
@@ -72,8 +72,8 @@ function deepMergeFormatsAndSetTimeZone(
72
72
  };
73
73
  }
74
74
 
75
- export const prepareIntlMessageFormatHtmlOutput = (
76
- chunks: (string | object)[]
75
+ export const prepareIntlMessageFormatHtmlOutput = <T>(
76
+ chunks: (string | T)[]
77
77
  ): React.ReactElement => React.createElement(React.Fragment, null, ...chunks);
78
78
 
79
79
  export function formatMessage(
@@ -97,7 +97,7 @@ export function formatMessage(
97
97
  messageDescriptor?: MessageDescriptor,
98
98
  values?: Record<string, PrimitiveType>
99
99
  ): string;
100
- export function formatMessage(
100
+ export function formatMessage<T>(
101
101
  {
102
102
  locale,
103
103
  formats,
@@ -118,11 +118,20 @@ export function formatMessage(
118
118
  >,
119
119
  state: Formatters,
120
120
  messageDescriptor: MessageDescriptor = {id: ''},
121
- values: Record<
122
- string,
123
- PrimitiveType | React.ReactElement | FormatXMLElementFn
124
- > = {}
125
- ): string | React.ReactNodeArray | React.ReactElement {
121
+ values:
122
+ | Record<
123
+ string,
124
+ | string
125
+ | number
126
+ | boolean
127
+ | Date
128
+ | T
129
+ | FormatXMLElementFn<T>
130
+ | null
131
+ | undefined
132
+ >
133
+ | undefined = {}
134
+ ): React.ReactNode {
126
135
  const {id, defaultMessage} = messageDescriptor;
127
136
 
128
137
  // `id` is a required field of a Message Descriptor.
@@ -131,7 +140,7 @@ export function formatMessage(
131
140
  formats = deepMergeFormatsAndSetTimeZone(formats, timeZone);
132
141
  defaultFormats = deepMergeFormatsAndSetTimeZone(defaultFormats, timeZone);
133
142
 
134
- let formattedMessageParts: Array<string | object> = [];
143
+ let formattedMessageParts: string | T | (string | T)[] = '';
135
144
 
136
145
  if (message) {
137
146
  try {
@@ -139,34 +148,34 @@ export function formatMessage(
139
148
  formatters: state,
140
149
  });
141
150
 
142
- formattedMessageParts = formatter.formatHTMLMessage(values);
151
+ formattedMessageParts = formatter.format(values);
143
152
  } catch (e) {
144
153
  onError(
145
- createError(
154
+ new ReactIntlError(
155
+ ReactIntlErrorCode.FORMAT_ERROR,
146
156
  `Error formatting message: "${id}" for locale: "${locale}"` +
147
157
  (defaultMessage ? ', using default message as fallback.' : ''),
148
158
  e
149
159
  )
150
160
  );
151
161
  }
152
- } else {
162
+ } else if (
163
+ !defaultMessage ||
164
+ (locale && locale.toLowerCase() !== defaultLocale.toLowerCase())
165
+ ) {
153
166
  // This prevents warnings from littering the console in development
154
167
  // when no `messages` are passed into the <IntlProvider> for the
155
- // default locale, and a default message is in the source.
156
- if (
157
- !defaultMessage ||
158
- (locale && locale.toLowerCase() !== defaultLocale.toLowerCase())
159
- ) {
160
- onError(
161
- createError(
162
- `Missing message: "${id}" for locale: "${locale}"` +
163
- (defaultMessage ? ', using default message as fallback.' : '')
164
- )
165
- );
166
- }
168
+ // default locale.
169
+ onError(
170
+ new ReactIntlError(
171
+ ReactIntlErrorCode.MISSING_TRANSLATION,
172
+ `Missing message: "${id}" for locale: "${locale}"` +
173
+ (defaultMessage ? ', using default message as fallback.' : '')
174
+ )
175
+ );
167
176
  }
168
177
 
169
- if (!formattedMessageParts.length && defaultMessage) {
178
+ if (!formattedMessageParts && defaultMessage) {
170
179
  try {
171
180
  const formatter = state.getMessageFormat(
172
181
  defaultMessage,
@@ -174,17 +183,22 @@ export function formatMessage(
174
183
  defaultFormats
175
184
  );
176
185
 
177
- formattedMessageParts = formatter.formatHTMLMessage(values);
186
+ formattedMessageParts = formatter.format(values);
178
187
  } catch (e) {
179
188
  onError(
180
- createError(`Error formatting the default message for: "${id}"`, e)
189
+ new ReactIntlError(
190
+ ReactIntlErrorCode.FORMAT_ERROR,
191
+ `Error formatting the default message for: "${id}"`,
192
+ e
193
+ )
181
194
  );
182
195
  }
183
196
  }
184
197
 
185
- if (!formattedMessageParts.length) {
198
+ if (!formattedMessageParts) {
186
199
  onError(
187
- createError(
200
+ new ReactIntlError(
201
+ ReactIntlErrorCode.FORMAT_ERROR,
188
202
  `Cannot format message: "${id}", ` +
189
203
  `using message ${
190
204
  message || defaultMessage ? 'source' : 'id'
@@ -196,41 +210,10 @@ export function formatMessage(
196
210
  }
197
211
  return defaultMessage || String(id);
198
212
  }
199
- if (
200
- formattedMessageParts.length === 1 &&
201
- typeof formattedMessageParts[0] === 'string'
202
- ) {
203
- return (formattedMessageParts[0] as string) || defaultMessage || String(id);
213
+ if (Array.isArray(formattedMessageParts)) {
214
+ return prepareIntlMessageFormatHtmlOutput<T>(
215
+ formattedMessageParts as Array<string | T>
216
+ );
204
217
  }
205
-
206
- return prepareIntlMessageFormatHtmlOutput(formattedMessageParts);
207
- }
208
-
209
- export function formatHTMLMessage(
210
- config: Pick<
211
- IntlConfig,
212
- | 'locale'
213
- | 'formats'
214
- | 'messages'
215
- | 'defaultLocale'
216
- | 'defaultFormats'
217
- | 'onError'
218
- >,
219
- state: Formatters,
220
- messageDescriptor: MessageDescriptor = {id: ''},
221
- rawValues: Record<string, PrimitiveType> = {}
222
- ): React.ReactNode {
223
- // Process all the values before they are used when formatting the ICU
224
- // Message string. Since the formatted message might be injected via
225
- // `innerHTML`, all String-based values need to be HTML-escaped.
226
- const escapedValues = Object.keys(rawValues).reduce(
227
- (escaped: Record<string, any>, name) => {
228
- const value = rawValues[name];
229
- escaped[name] = typeof value === 'string' ? escape(value) : value;
230
- return escaped;
231
- },
232
- {}
233
- );
234
-
235
- return formatMessage(config, state, messageDescriptor, escapedValues);
218
+ return formattedMessageParts as string | T;
236
219
  }
@@ -1,6 +1,7 @@
1
1
  import {IntlConfig, Formatters, IntlFormatters} from '../types';
2
- import {getNamedFormat, filterProps, createError} from '../utils';
2
+ import {getNamedFormat, filterProps} from '../utils';
3
3
  import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat';
4
+ import {ReactIntlError, ReactIntlErrorCode} from '../error';
4
5
 
5
6
  const NUMBER_FORMAT_OPTIONS: Array<keyof UnifiedNumberFormatOptions> = [
6
7
  'localeMatcher',
@@ -55,7 +56,13 @@ export function formatNumber(
55
56
  try {
56
57
  return getFormatter(config, getNumberFormat, options).format(value);
57
58
  } catch (e) {
58
- config.onError(createError('Error formatting number.', e));
59
+ config.onError(
60
+ new ReactIntlError(
61
+ ReactIntlErrorCode.FORMAT_ERROR,
62
+ 'Error formatting number.',
63
+ e
64
+ )
65
+ );
59
66
  }
60
67
 
61
68
  return String(value);
@@ -70,7 +77,13 @@ export function formatNumberToParts(
70
77
  try {
71
78
  return getFormatter(config, getNumberFormat, options).formatToParts(value);
72
79
  } catch (e) {
73
- config.onError(createError('Error formatting number.', e));
80
+ config.onError(
81
+ new ReactIntlError(
82
+ ReactIntlErrorCode.FORMAT_ERROR,
83
+ 'Error formatting number.',
84
+ e
85
+ )
86
+ );
74
87
  }
75
88
 
76
89
  return [];
@@ -1,5 +1,7 @@
1
1
  import {IntlConfig, Formatters, IntlFormatters} from '../types';
2
- import {filterProps, createError} from '../utils';
2
+ import {filterProps} from '../utils';
3
+ import {ReactIntlErrorCode, ReactIntlError} from '../error';
4
+ import {FormatError, ErrorCode} from 'intl-messageformat';
3
5
 
4
6
  const PLURAL_FORMAT_OPTIONS: Array<keyof Intl.PluralRulesOptions> = [
5
7
  'localeMatcher',
@@ -14,9 +16,12 @@ export function formatPlural(
14
16
  ): string {
15
17
  if (!Intl.PluralRules) {
16
18
  onError(
17
- createError(`Intl.PluralRules is not available in this environment.
19
+ new FormatError(
20
+ `Intl.PluralRules is not available in this environment.
18
21
  Try polyfilling it using "@formatjs/intl-pluralrules"
19
- `)
22
+ `,
23
+ ErrorCode.MISSING_INTL_API
24
+ )
20
25
  );
21
26
  }
22
27
  const filteredOptions = filterProps(options, PLURAL_FORMAT_OPTIONS);
@@ -24,7 +29,13 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
24
29
  try {
25
30
  return getPluralRules(locale, filteredOptions).select(value);
26
31
  } catch (e) {
27
- onError(createError('Error formatting plural.', e));
32
+ onError(
33
+ new ReactIntlError(
34
+ ReactIntlErrorCode.FORMAT_ERROR,
35
+ 'Error formatting plural.',
36
+ e
37
+ )
38
+ );
28
39
  }
29
40
 
30
41
  return 'other';
@@ -1,9 +1,11 @@
1
1
  import {IntlConfig, IntlFormatters, Formatters} from '../types';
2
2
 
3
- import {getNamedFormat, filterProps, createError} from '../utils';
3
+ import {getNamedFormat, filterProps} from '../utils';
4
4
  import RelativeTimeFormat, {
5
5
  IntlRelativeTimeFormatOptions,
6
6
  } from '@formatjs/intl-relativetimeformat';
7
+ import {FormatError, ErrorCode} from 'intl-messageformat';
8
+ import {ReactIntlError, ReactIntlErrorCode} from '../error';
7
9
 
8
10
  const RELATIVE_TIME_FORMAT_OPTIONS: Array<keyof IntlRelativeTimeFormatOptions> = [
9
11
  'numeric',
@@ -45,9 +47,12 @@ export function formatRelativeTime(
45
47
  const RelativeTimeFormat = (Intl as any).RelativeTimeFormat;
46
48
  if (!RelativeTimeFormat) {
47
49
  config.onError(
48
- createError(`Intl.RelativeTimeFormat is not available in this environment.
50
+ new FormatError(
51
+ `Intl.RelativeTimeFormat is not available in this environment.
49
52
  Try polyfilling it using "@formatjs/intl-relativetimeformat"
50
- `)
53
+ `,
54
+ ErrorCode.MISSING_INTL_API
55
+ )
51
56
  );
52
57
  }
53
58
  try {
@@ -56,7 +61,13 @@ Try polyfilling it using "@formatjs/intl-relativetimeformat"
56
61
  unit
57
62
  );
58
63
  } catch (e) {
59
- config.onError(createError('Error formatting relative time.', e));
64
+ config.onError(
65
+ new ReactIntlError(
66
+ ReactIntlErrorCode.FORMAT_ERROR,
67
+ 'Error formatting relative time.',
68
+ e
69
+ )
70
+ );
60
71
  }
61
72
 
62
73
  return String(value);
package/src/index.ts CHANGED
@@ -5,7 +5,9 @@
5
5
  */
6
6
  import * as React from 'react';
7
7
  export * from './types';
8
- export {defineMessages} from '@formatjs/macro';
8
+ export function defineMessages<T, U extends Record<string, T>>(msgs: U): U {
9
+ return msgs;
10
+ }
9
11
  import {
10
12
  createFormattedComponent,
11
13
  createFormattedDateTimePartsComponent,
@@ -52,5 +54,5 @@ export {FormattedNumberParts} from './components/createFormattedComponent';
52
54
  export {default as FormattedRelativeTime} from './components/relative';
53
55
  export {default as FormattedPlural} from './components/plural';
54
56
  export {default as FormattedMessage} from './components/message';
55
- export {default as FormattedHTMLMessage} from './components/html-message';
56
57
  export {createIntlCache} from './utils';
58
+ export {ReactIntlError, ReactIntlErrorCode} from './error'
package/src/types.ts CHANGED
@@ -8,6 +8,7 @@ import IntlMessageFormat, {
8
8
  Formats,
9
9
  PrimitiveType,
10
10
  FormatXMLElementFn,
11
+ FormatError,
11
12
  } from 'intl-messageformat';
12
13
  import IntlRelativeTimeFormat, {
13
14
  IntlRelativeTimeFormatOptions,
@@ -16,6 +17,7 @@ import {MessageFormatElement} from 'intl-messageformat-parser';
16
17
  import {UnifiedNumberFormatOptions} from '@formatjs/intl-unified-numberformat';
17
18
  import IntlListFormat, {IntlListFormatOptions} from '@formatjs/intl-listformat';
18
19
  import {DisplayNames, DisplayNamesOptions} from '@formatjs/intl-displaynames';
20
+ import { ReactIntlError } from './error';
19
21
 
20
22
  export interface IntlConfig {
21
23
  locale: string;
@@ -25,7 +27,7 @@ export interface IntlConfig {
25
27
  messages: Record<string, string> | Record<string, MessageFormatElement[]>;
26
28
  defaultLocale: string;
27
29
  defaultFormats: CustomFormats;
28
- onError(err: string): void;
30
+ onError(err: ReactIntlError | FormatError): void;
29
31
  }
30
32
 
31
33
  export interface CustomFormats extends Partial<Formats> {
@@ -64,7 +66,7 @@ export type FormatDisplayNameOptions = Exclude<
64
66
  'localeMatcher'
65
67
  >;
66
68
 
67
- export interface IntlFormatters {
69
+ export interface IntlFormatters<T = React.ReactNode> {
68
70
  formatDate(
69
71
  value: Parameters<Intl.DateTimeFormat['format']>[0] | string,
70
72
  opts?: FormatDateOptions
@@ -106,13 +108,9 @@ export interface IntlFormatters {
106
108
  descriptor: MessageDescriptor,
107
109
  values?: Record<
108
110
  string,
109
- PrimitiveType | React.ReactElement | FormatXMLElementFn
111
+ PrimitiveType | React.ReactElement | FormatXMLElementFn<T>
110
112
  >
111
113
  ): string | React.ReactNodeArray;
112
- formatHTMLMessage(
113
- descriptor: MessageDescriptor,
114
- values?: Record<string, PrimitiveType>
115
- ): React.ReactNode;
116
114
  formatList(values: Array<string>, opts?: FormatListOptions): string;
117
115
  formatList(
118
116
  values: Array<string | React.ReactNode>,
package/src/utils.ts CHANGED
@@ -15,23 +15,7 @@ import IntlMessageFormat from 'intl-messageformat';
15
15
  import memoizeIntlConstructor from 'intl-format-cache';
16
16
  import {invariant} from '@formatjs/intl-utils';
17
17
  import {IntlRelativeTimeFormatOptions} from '@formatjs/intl-relativetimeformat';
18
-
19
- const ESCAPED_CHARS: Record<number, string> = {
20
- 38: '&amp;',
21
- 62: '&gt;',
22
- 60: '&lt;',
23
- 34: '&quot;',
24
- 39: '&#x27;',
25
- };
26
-
27
- const UNSAFE_CHARS_REGEX = /[&><"']/g;
28
-
29
- export function escape(str: string): string {
30
- return ('' + str).replace(
31
- UNSAFE_CHARS_REGEX,
32
- match => ESCAPED_CHARS[match.charCodeAt(0)]
33
- );
34
- }
18
+ import { ReactIntlError, ReactIntlErrorCode } from './error';
35
19
 
36
20
  export function filterProps<T extends Record<string, any>, K extends string>(
37
21
  props: T,
@@ -57,12 +41,7 @@ export function invariantIntlContext(intl?: any): asserts intl {
57
41
  );
58
42
  }
59
43
 
60
- export function createError(message: string, exception?: Error): string {
61
- const eMsg = exception ? `\n${exception.stack}` : '';
62
- return `[React Intl] ${message}${eMsg}`;
63
- }
64
-
65
- export function defaultErrorHandler(error: string): void {
44
+ export function defaultErrorHandler(error: ReactIntlError): void {
66
45
  if (process.env.NODE_ENV !== 'production') {
67
46
  console.error(error);
68
47
  }
@@ -132,7 +111,7 @@ export function getNamedFormat<T extends keyof CustomFormats>(
132
111
  formats: CustomFormats,
133
112
  type: T,
134
113
  name: string,
135
- onError: (err: string) => void
114
+ onError: (err: ReactIntlError) => void
136
115
  ):
137
116
  | Intl.NumberFormatOptions
138
117
  | Intl.DateTimeFormatOptions
@@ -147,5 +126,5 @@ export function getNamedFormat<T extends keyof CustomFormats>(
147
126
  return format;
148
127
  }
149
128
 
150
- onError(createError(`No ${type} format named: ${name}`));
129
+ onError(new ReactIntlError(ReactIntlErrorCode.UNSUPPORTED_FORMATTER, `No ${type} format named: ${name}`));
151
130
  }
@@ -1,11 +0,0 @@
1
- import { PrimitiveType } from 'intl-messageformat';
2
- import FormattedMessage from './message';
3
- declare class FormattedHTMLMessage extends FormattedMessage<Record<string, PrimitiveType>> {
4
- static displayName: string;
5
- static defaultProps: {
6
- tagName: "span";
7
- values: {};
8
- };
9
- render(): JSX.Element;
10
- }
11
- export default FormattedHTMLMessage;
@@ -1,75 +0,0 @@
1
- "use strict";
2
- /*
3
- * Copyright 2015, Yahoo Inc.
4
- * Copyrights licensed under the New BSD License.
5
- * See the accompanying LICENSE file for terms.
6
- */
7
- var __extends = (this && this.__extends) || (function () {
8
- var extendStatics = function (d, b) {
9
- extendStatics = Object.setPrototypeOf ||
10
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
12
- return extendStatics(d, b);
13
- };
14
- return function (d, b) {
15
- extendStatics(d, b);
16
- function __() { this.constructor = d; }
17
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
18
- };
19
- })();
20
- var __assign = (this && this.__assign) || function () {
21
- __assign = Object.assign || function(t) {
22
- for (var s, i = 1, n = arguments.length; i < n; i++) {
23
- s = arguments[i];
24
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
25
- t[p] = s[p];
26
- }
27
- return t;
28
- };
29
- return __assign.apply(this, arguments);
30
- };
31
- Object.defineProperty(exports, "__esModule", { value: true });
32
- var React = require("react");
33
- var message_1 = require("./message");
34
- var injectIntl_1 = require("./injectIntl");
35
- var utils_1 = require("../utils");
36
- var FormattedHTMLMessage = /** @class */ (function (_super) {
37
- __extends(FormattedHTMLMessage, _super);
38
- function FormattedHTMLMessage() {
39
- return _super !== null && _super.apply(this, arguments) || this;
40
- }
41
- FormattedHTMLMessage.prototype.render = function () {
42
- var _this = this;
43
- return (React.createElement(injectIntl_1.Context.Consumer, null, function (intl) {
44
- if (!_this.props.defaultMessage) {
45
- utils_1.invariantIntlContext(intl);
46
- }
47
- var formatHTMLMessage = intl.formatHTMLMessage, textComponent = intl.textComponent;
48
- var _a = _this.props, id = _a.id, description = _a.description, defaultMessage = _a.defaultMessage, rawValues = _a.values, children = _a.children;
49
- var Component = _this.props.tagName;
50
- // This is bc of TS3.3 doesn't recognize `defaultProps`
51
- if (!Component) {
52
- Component = textComponent || 'span';
53
- }
54
- var descriptor = { id: id, description: description, defaultMessage: defaultMessage };
55
- var formattedHTMLMessage = formatHTMLMessage(descriptor, rawValues);
56
- if (typeof children === 'function') {
57
- return children(formattedHTMLMessage);
58
- }
59
- // Since the message presumably has HTML in it, we need to set
60
- // `innerHTML` in order for it to be rendered and not escaped by React.
61
- // To be safe, all string prop values were escaped when formatting the
62
- // message. It is assumed that the message is not UGC, and came from the
63
- // developer making it more like a template.
64
- //
65
- // Note: There's a perf impact of using this component since there's no
66
- // way for React to do its virtual DOM diffing.
67
- var html = { __html: formattedHTMLMessage };
68
- return React.createElement(Component, { dangerouslySetInnerHTML: html });
69
- }));
70
- };
71
- FormattedHTMLMessage.displayName = 'FormattedHTMLMessage';
72
- FormattedHTMLMessage.defaultProps = __assign(__assign({}, message_1.default.defaultProps), { tagName: 'span' });
73
- return FormattedHTMLMessage;
74
- }(message_1.default));
75
- exports.default = FormattedHTMLMessage;
@@ -1,11 +0,0 @@
1
- import { PrimitiveType } from 'intl-messageformat';
2
- import FormattedMessage from './message';
3
- declare class FormattedHTMLMessage extends FormattedMessage<Record<string, PrimitiveType>> {
4
- static displayName: string;
5
- static defaultProps: {
6
- tagName: "span";
7
- values: {};
8
- };
9
- render(): JSX.Element;
10
- }
11
- export default FormattedHTMLMessage;
@@ -1,43 +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 FormattedMessage from './message';
8
- import { Context } from './injectIntl';
9
- import { invariantIntlContext } from '../utils';
10
- class FormattedHTMLMessage extends FormattedMessage {
11
- render() {
12
- return (React.createElement(Context.Consumer, null, (intl) => {
13
- if (!this.props.defaultMessage) {
14
- invariantIntlContext(intl);
15
- }
16
- const { formatHTMLMessage, textComponent } = intl;
17
- const { id, description, defaultMessage, values: rawValues, children, } = this.props;
18
- let { tagName: Component } = this.props;
19
- // This is bc of TS3.3 doesn't recognize `defaultProps`
20
- if (!Component) {
21
- Component = textComponent || 'span';
22
- }
23
- const descriptor = { id, description, defaultMessage };
24
- const formattedHTMLMessage = formatHTMLMessage(descriptor, rawValues);
25
- if (typeof children === 'function') {
26
- return children(formattedHTMLMessage);
27
- }
28
- // Since the message presumably has HTML in it, we need to set
29
- // `innerHTML` in order for it to be rendered and not escaped by React.
30
- // To be safe, all string prop values were escaped when formatting the
31
- // message. It is assumed that the message is not UGC, and came from the
32
- // developer making it more like a template.
33
- //
34
- // Note: There's a perf impact of using this component since there's no
35
- // way for React to do its virtual DOM diffing.
36
- const html = { __html: formattedHTMLMessage };
37
- return React.createElement(Component, { dangerouslySetInnerHTML: html });
38
- }));
39
- }
40
- }
41
- FormattedHTMLMessage.displayName = 'FormattedHTMLMessage';
42
- FormattedHTMLMessage.defaultProps = Object.assign(Object.assign({}, FormattedMessage.defaultProps), { tagName: 'span' });
43
- export default FormattedHTMLMessage;
@@ -1,68 +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
-
7
- import * as React from 'react';
8
- import {PrimitiveType} from 'intl-messageformat';
9
- import FormattedMessage from './message';
10
- import {Context} from './injectIntl';
11
- import {invariantIntlContext} from '../utils';
12
-
13
- class FormattedHTMLMessage extends FormattedMessage<
14
- Record<string, PrimitiveType>
15
- > {
16
- static displayName = 'FormattedHTMLMessage';
17
- static defaultProps = {
18
- ...FormattedMessage.defaultProps,
19
- tagName: 'span' as 'span',
20
- };
21
- render(): JSX.Element {
22
- return (
23
- <Context.Consumer>
24
- {(intl): React.ReactNode => {
25
- if (!this.props.defaultMessage) {
26
- invariantIntlContext(intl);
27
- }
28
-
29
- const {formatHTMLMessage, textComponent} = intl;
30
- const {
31
- id,
32
- description,
33
- defaultMessage,
34
- values: rawValues,
35
- children,
36
- } = this.props;
37
-
38
- let {tagName: Component} = this.props;
39
-
40
- // This is bc of TS3.3 doesn't recognize `defaultProps`
41
- if (!Component) {
42
- Component = textComponent || 'span';
43
- }
44
-
45
- const descriptor = {id, description, defaultMessage};
46
- const formattedHTMLMessage = formatHTMLMessage(descriptor, rawValues);
47
-
48
- if (typeof children === 'function') {
49
- return children(formattedHTMLMessage);
50
- }
51
-
52
- // Since the message presumably has HTML in it, we need to set
53
- // `innerHTML` in order for it to be rendered and not escaped by React.
54
- // To be safe, all string prop values were escaped when formatting the
55
- // message. It is assumed that the message is not UGC, and came from the
56
- // developer making it more like a template.
57
- //
58
- // Note: There's a perf impact of using this component since there's no
59
- // way for React to do its virtual DOM diffing.
60
- const html = {__html: formattedHTMLMessage};
61
- return <Component dangerouslySetInnerHTML={html} />;
62
- }}
63
- </Context.Consumer>
64
- );
65
- }
66
- }
67
-
68
- export default FormattedHTMLMessage;