Skip to content

Commit

Permalink
feat(react-intl): allow FormattedMessage to pass in ignoreTag, fix #2538
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Jan 29, 2021
1 parent 96f1c48 commit a615cf9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/react-intl/src/components/message.tsx
Expand Up @@ -5,7 +5,11 @@
*/

import * as React from 'react';
import type {PrimitiveType, FormatXMLElementFn} from 'intl-messageformat';
import type {
PrimitiveType,
FormatXMLElementFn,
Options as IntlMessageFormatOptions,
} from 'intl-messageformat';
import {Context} from './injectIntl';
import {invariantIntlContext} from '../utils';
import * as shallowEquals_ from 'shallow-equal/objects';
Expand All @@ -19,6 +23,7 @@ export interface Props<
values?: V;
tagName?: React.ElementType<any>;
children?(...nodes: React.ReactNodeArray): React.ReactNode;
ignoreTag?: IntlMessageFormatOptions['ignoreTag'];
}

class FormattedMessage<
Expand Down Expand Up @@ -54,10 +59,13 @@ class FormattedMessage<
values,
children,
tagName: Component = Text,
ignoreTag,
} = this.props;

const descriptor = {id, description, defaultMessage};
let nodes: React.ReactNode = formatMessage(descriptor, values);
let nodes: React.ReactNode = formatMessage(descriptor, values, {
ignoreTag,
});

if (!Array.isArray(nodes)) {
nodes = [nodes];
Expand Down
18 changes: 18 additions & 0 deletions packages/react-intl/tests/unit/components/message.tsx
Expand Up @@ -229,6 +229,24 @@ describe('<FormattedMessage>', () => {
expect(nameNode).toHaveTextContent('Jest');
});

it('supports rich-text message formatting with defaultRichTextElements', () => {
const {getByTestId} = mountWithProvider(
{
id: 'hello',
defaultMessage: 'Hello, <b>{name}</b>!',
values: {
name: 'Jest',
},
ignoreTag: true,
},
{
...providerProps,
}
);

expect(getByTestId('comp')).toHaveTextContent('Hello, <b>{name}</b>!');
});

it('supports rich-text message formatting w/ nested tag', () => {
const {getByTestId} = mountWithProvider(
{
Expand Down

0 comments on commit a615cf9

Please sign in to comment.