@@ -2,7 +2,6 @@ import React from 'react';
2
2
import PropTypes from 'prop-types' ;
3
3
import { withOnyx } from 'react-native-onyx' ;
4
4
import getComponentDisplayName from '../libs/getComponentDisplayName' ;
5
- import compose from '../libs/compose' ;
6
5
import ONYXKEYS from '../ONYXKEYS' ;
7
6
import { translate } from '../libs/translate' ;
8
7
import DateUtils from '../libs/DateUtils' ;
@@ -30,59 +29,123 @@ const withLocalizePropTypes = {
30
29
fromLocalPhone : PropTypes . func . isRequired ,
31
30
} ;
32
31
33
- function withLocalizeHOC ( WrappedComponent ) {
34
- const WithLocalize = ( props ) => {
35
- const translations = {
36
- translate : ( phrase , variables ) => translate ( props . preferredLocale , phrase , variables ) ,
37
- numberFormat : ( number , options ) => numberFormat ( props . preferredLocale , number , options ) ,
38
- timestampToRelative : timestamp => DateUtils . timestampToRelative ( props . preferredLocale , timestamp ) ,
39
- timestampToDateTime : ( timestamp , includeTimezone ) => DateUtils . timestampToDateTime (
40
- props . preferredLocale ,
41
- timestamp ,
42
- includeTimezone ,
43
- ) ,
44
- toLocalPhone : number => toLocalPhone ( props . preferredLocale , number ) ,
45
- fromLocalPhone : number => fromLocalPhone ( props . preferredLocale , number ) ,
46
- } ;
47
- return (
48
- < WrappedComponent
49
- // eslint-disable-next-line react/jsx-props-no-spreading
50
- { ...props }
51
- ref = { props . forwardedRef }
52
- translate = { translations . translate }
53
- numberFormat = { translations . numberFormat }
54
- timestampToRelative = { translations . timestampToRelative }
55
- timestampToDateTime = { translations . timestampToDateTime }
56
- toLocalPhone = { translations . toLocalPhone }
57
- fromLocalPhone = { translations . fromLocalPhone }
58
- />
59
- ) ;
60
- } ;
61
- WithLocalize . displayName = `WithLocalize(${ getComponentDisplayName ( WrappedComponent ) } )` ;
62
- WithLocalize . propTypes = {
32
+ export default ( WrappedComponent ) => {
33
+ const propTypes = {
34
+ /** The user's preferred locale e.g. 'en', 'es-ES' */
63
35
preferredLocale : PropTypes . string ,
36
+
37
+ /** Passed ref from whatever component is wrapped in the HOC */
64
38
forwardedRef : PropTypes . oneOfType ( [
65
39
PropTypes . func ,
66
40
PropTypes . shape ( { current : PropTypes . instanceOf ( React . Component ) } ) ,
67
41
] ) ,
68
42
} ;
69
- WithLocalize . defaultProps = {
43
+
44
+ const defaultProps = {
70
45
preferredLocale : CONST . DEFAULT_LOCALE ,
71
46
forwardedRef : undefined ,
72
47
} ;
73
- return React . forwardRef ( ( props , ref ) => (
48
+
49
+ class WithLocalize extends React . Component {
50
+ constructor ( props ) {
51
+ super ( props ) ;
52
+
53
+ this . translate = this . translate . bind ( this ) ;
54
+ this . numberFormat = this . numberFormat . bind ( this ) ;
55
+ this . timestampToRelative = this . timestampToRelative . bind ( this ) ;
56
+ this . timestampToDateTime = this . timestampToDateTime . bind ( this ) ;
57
+ this . fromLocalPhone = this . fromLocalPhone . bind ( this ) ;
58
+ this . toLocalPhone = this . toLocalPhone . bind ( this ) ;
59
+ }
60
+
61
+ /**
62
+ * @param {String } phrase
63
+ * @param {Object } [variables]
64
+ * @returns {String }
65
+ */
66
+ translate ( phrase , variables ) {
67
+ return translate ( this . props . preferredLocale , phrase , variables ) ;
68
+ }
69
+
70
+ /**
71
+ * @param {Number } number
72
+ * @param {Intl.NumberFormatOptions } options
73
+ * @returns {String }
74
+ */
75
+ numberFormat ( number , options ) {
76
+ return numberFormat ( this . props . preferredLocale , number , options ) ;
77
+ }
78
+
79
+ /**
80
+ * @param {Number } timestamp
81
+ * @returns {String }
82
+ */
83
+ timestampToRelative ( timestamp ) {
84
+ return DateUtils . timestampToRelative ( this . props . preferredLocale , timestamp ) ;
85
+ }
86
+
87
+ /**
88
+ * @param {Number } timestamp
89
+ * @param {Boolean } [includeTimezone]
90
+ * @returns {String }
91
+ */
92
+ timestampToDateTime ( timestamp , includeTimezone ) {
93
+ return DateUtils . timestampToDateTime (
94
+ this . props . preferredLocale ,
95
+ timestamp ,
96
+ includeTimezone ,
97
+ ) ;
98
+ }
99
+
100
+ /**
101
+ * @param {Number } number
102
+ * @returns {String }
103
+ */
104
+ toLocalPhone ( number ) {
105
+ return toLocalPhone ( this . props . preferredLocale , number ) ;
106
+ }
107
+
108
+ /**
109
+ * @param {Number } number
110
+ * @returns {String }
111
+ */
112
+ fromLocalPhone ( number ) {
113
+ return fromLocalPhone ( this . props . preferredLocale , number ) ;
114
+ }
115
+
116
+ render ( ) {
117
+ return (
118
+ < WrappedComponent
119
+ // eslint-disable-next-line react/jsx-props-no-spreading
120
+ { ...this . props }
121
+ ref = { this . props . forwardedRef }
122
+ translate = { this . translate }
123
+ numberFormat = { this . numberFormat }
124
+ timestampToRelative = { this . timestampToRelative }
125
+ timestampToDateTime = { this . timestampToDateTime }
126
+ toLocalPhone = { this . toLocalPhone }
127
+ fromLocalPhone = { this . fromLocalPhone }
128
+ />
129
+ ) ;
130
+ }
131
+ }
132
+
133
+ WithLocalize . propTypes = propTypes ;
134
+ WithLocalize . defaultProps = defaultProps ;
135
+
136
+ const withForwardedRef = React . forwardRef ( ( props , ref ) => (
74
137
// eslint-disable-next-line react/jsx-props-no-spreading
75
138
< WithLocalize { ...props } forwardedRef = { ref } />
76
139
) ) ;
77
- }
78
- export default compose (
79
- withOnyx ( {
140
+
141
+ withForwardedRef . displayName = `withLocalize(${ getComponentDisplayName ( WrappedComponent ) } )` ;
142
+
143
+ return withOnyx ( {
80
144
preferredLocale : {
81
145
key : ONYXKEYS . NVP_PREFERRED_LOCALE ,
82
146
} ,
83
- } ) ,
84
- withLocalizeHOC ,
85
- ) ;
147
+ } ) ( withForwardedRef ) ;
148
+ } ;
86
149
87
150
export {
88
151
withLocalizePropTypes ,
0 commit comments