@@ -2,100 +2,37 @@ import React from 'react';
22import PropTypes from 'prop-types' ;
33import invariant from 'invariant' ;
44import hoistNonReactStatics from 'hoist-non-react-statics' ;
5- import { getMessageWithNamedParams , getMessageWithParams } from './messages' ;
6- import { normalizeKeyPrefix } from './utils' ;
7-
8- /**
9- * A React Context which holds the translations map.
10- */
11- const MessageSourceContext = React . createContext ( null ) ;
12- MessageSourceContext . displayName = 'MessageSourceContext' ;
5+ import { useMessageSource } from './useMessageSource' ;
136
147/**
158 * Creates a HOC which passes the MessageSourceApi to the given Component.
169 */
1710function enhanceWithMessages ( keyPrefix , WrappedComponent ) {
18- const normalizedKeyPrefix = normalizeKeyPrefix ( keyPrefix || '' ) ;
1911 const wrappedComponentName = WrappedComponent . displayName || WrappedComponent . name || 'Component' ;
2012
2113 /**
2214 * The enhancer HOC.
2315 */
24- class Enhancer extends React . Component {
25- /**
26- * Retrieves a text message.
27- *
28- * Example usage:
29- * let name, lastName;
30- * ...
31- * const message = getMessage('message.key', name, lastName);
32- *
33- * @param key the key of the message.
34- * @param params an optional parameters (param0, param1 ...).
35- */
36- getMessage = ( key , ...params ) => {
37- const textKey = normalizedKeyPrefix + key ;
38- const message = getMessageWithParams ( this . context , textKey , ...params ) ;
39- if ( message === textKey ) {
40- return getMessageWithParams ( this . context , key , ...params ) ;
41- }
42-
43- return message ;
44- } ;
45-
46- /**
47- * Retrieves a text message with named parameters.
48- *
49- * Example usage:
50- * const parameters = {
51- * name: 'John Doe',
52- * }
53- *
54- * const message = getMessageWithNamedParams('message.key', parameters)
55- *
56- * @param key the key of the message.
57- * @param namedParams a map of named parameters.
58- */
59- getMessageWithNamedParams = ( key , namedParams ) => {
60- const textKey = normalizedKeyPrefix + key ;
61- const message = getMessageWithNamedParams ( this . context , textKey , namedParams ) ;
62- if ( message === textKey ) {
63- return getMessageWithNamedParams ( this . context , key , namedParams ) ;
64- }
65-
66- return message ;
67- } ;
68-
69- render ( ) {
70- if ( process . env . NODE_ENV !== 'production' ) {
71- /* eslint-disable react/prop-types */
72- invariant (
73- ! this . props . getMessage ,
74- `[react-message-source]: [%s] already has a prop named [getMessage]. It will be overwritten.` ,
75- wrappedComponentName ,
76- ) ;
77-
78- invariant (
79- ! this . props . getMessageWithNamedParams ,
80- `[react-message-source]: [%s] already has a prop named [getMessageWithNamedParams]. It will be overwritten.` ,
81- wrappedComponentName ,
82- ) ;
83- /* eslint-enable react/prop-types */
84- }
85-
86- return (
87- < WrappedComponent
88- { ...this . props }
89- getMessage = { this . getMessage }
90- getMessageWithNamedParams = { this . getMessageWithNamedParams }
91- />
16+ function Enhancer ( props ) {
17+ const messageSourceApi = useMessageSource ( keyPrefix ) ;
18+ if ( process . env . NODE_ENV !== 'production' ) {
19+ const hasOwn = Object . prototype . hasOwnProperty ;
20+ const propsToOverwrite = Object . keys ( messageSourceApi )
21+ . filter ( propToCheck => hasOwn . call ( props , propToCheck ) )
22+ . join ( ', ' ) ;
23+
24+ invariant (
25+ ! propsToOverwrite ,
26+ `[react-message-source]: [%s] already has props named [%s]. They will be overwritten.` ,
27+ wrappedComponentName ,
28+ propsToOverwrite ,
9229 ) ;
9330 }
31+
32+ return < WrappedComponent { ...props } { ...messageSourceApi } /> ;
9433 }
9534
96- Enhancer . contextType = MessageSourceContext ;
9735 Enhancer . displayName = `WithMessages(${ wrappedComponentName } )` ;
98-
9936 return hoistNonReactStatics ( Enhancer , WrappedComponent ) ;
10037}
10138
@@ -113,17 +50,6 @@ function internalWithMessages(keyPrefixOrComponent) {
11350 return enhanceWithMessages ( null , keyPrefixOrComponent ) ;
11451}
11552
116- /**
117- * Example usage:
118- *
119- * const translations = await fetch('/api/rest/texts?lang=en');
120- * <MessageSource.Provider value={translations}>
121- * <SomeOtherComponent />
122- * ...
123- * </MessageSource.Provider>
124- */
125- export const { Provider } = MessageSourceContext ;
126-
12753/**
12854 * Example usages:
12955 *
0 commit comments