react-intl: change the FormattedMessage tagName prop to accept any ReactType (#31906)

In the react-intl source the prop is defined as follows:

    tagName: PropTypes.oneOfType([PropTypes.string, PropTypes.element])

But it's actually passed straight to React.createElement(tagName, …),
so any type that is accepted by createElement should be allowed here.

It is useful to allow passing custom components to FormattedMessage,
for example when you use styled components, eg.

    const FancyHeading = styled('h1')`font-size: 32px`;
    <FormattedMessage … tagName={FancyHeading} />
This commit is contained in:
Tomas Carnecky 2019-01-06 20:04:11 +01:00 committed by Sheetal Nandi
parent 1ff633d518
commit 08c13378e4
2 changed files with 8 additions and 1 deletions

View File

@ -146,7 +146,7 @@ declare namespace ReactIntl {
interface Props extends MessageDescriptor {
values?: {[key: string]: MessageValue | JSX.Element};
tagName?: string;
tagName?: React.ReactType;
children?: (...formattedMessage: Array<string | JSX.Element>) => React.ReactNode;
}
}

View File

@ -156,6 +156,13 @@ class SomeComponent extends React.Component<SomeComponentProps & InjectedIntlPro
values={{ blank: null, empty: undefined }}
tagName="div" />
<FormattedMessage
id="test"
description="Test"
defaultMessage="Hi {blank} and {empty}!"
values={{ blank: null, empty: undefined }}
tagName={({ children }) => <div>{children}</div>} />
<FormattedMessage
id="test"
description="Test"