Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PropType of Formatted{,HTML}Message to accept any component #1228

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/components/html-message.js
Expand Up @@ -23,7 +23,11 @@ export default class FormattedHTMLMessage extends Component {
static propTypes = {
...messageDescriptorPropTypes,
values: PropTypes.object,
tagName: PropTypes.string,
tagName: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.shape({render: PropTypes.func.isRequired}),
]),
children: PropTypes.func,
};

Expand Down
6 changes: 5 additions & 1 deletion src/components/message.js
Expand Up @@ -40,7 +40,11 @@ export default class FormattedMessage extends Component {
static propTypes = {
...messageDescriptorPropTypes,
values: PropTypes.object,
tagName: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
tagName: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
PropTypes.shape({render: PropTypes.func.isRequired}),
]),
children: PropTypes.func,
};

Expand Down
20 changes: 20 additions & 0 deletions test/unit/components/html-message.js
Expand Up @@ -190,6 +190,26 @@ describe('<FormattedHTMLMessage>', () => {
);
});

it('accepts a react component as `tagName` prop', () => {
const {intl} = intlProvider.getChildContext();
const descriptor = {
id: 'hello',
defaultMessage: 'Hello, World!',
};

const H1 = (children) => <h1>{children}</h1>
const el = <FormattedHTMLMessage {...descriptor} tagName={H1} />;

renderer.render(el, {intl});
expect(renderer.getRenderOutput()).toEqualJSX(
<H1
dangerouslySetInnerHTML={{
__html: intl.formatHTMLMessage(descriptor),
}}
/>
);
});

it('supports function-as-child pattern', () => {
const {intl} = intlProvider.getChildContext();
const descriptor = {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/components/message.js
Expand Up @@ -205,7 +205,7 @@ describe('<FormattedMessage>', () => {
);
});

it('accepts an react element as `tagName` prop', () => {
it('accepts a react component as `tagName` prop', () => {
const {intl} = intlProvider.getChildContext();
const descriptor = {
id: 'hello',
Expand Down