forked from joshswan/react-native-globalize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormattedWrapper.js
81 lines (69 loc) · 1.93 KB
/
FormattedWrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*!
* React Native Globalize
*
* Copyright 2015-2017 Josh Swan
* Released under the MIT license
* https://github.com/joshswan/react-native-globalize/blob/master/LICENSE
*/
import { Component } from 'react';
import PropTypes from 'prop-types';
import { globalizeShape, globalizePropTypes } from '../types';
import Globalize from '../globalize';
export default class FormattedWrapper extends Component {
constructor(props) {
super(props);
if (props.cldr) {
Globalize.load(props.cldr);
}
if (props.messages) {
Globalize.loadMessages(props.messages);
}
const instance = new Globalize(props.locale, props.currency, {
fallback: props.localeFallback,
warnOnMissingMessage: props.warnOnMissingMessage,
});
this.state = {
globalize: instance,
};
}
getChildContext() {
return {
globalize: this.state.globalize,
};
}
componentWillReceiveProps(nextProps) {
if (this.props.locale !== nextProps.locale || this.props.currency !== nextProps.currency) {
const instance = new Globalize(nextProps.locale, nextProps.currency, {
fallback: nextProps.localeFallback,
warnOnMissingMessage: nextProps.warnOnMissingMessage,
});
this.setState({
globalize: instance,
});
}
}
render() {
return this.props.children;
}
}
FormattedWrapper.propTypes = {
...globalizePropTypes,
cldr: PropTypes.array, // eslint-disable-line react/forbid-prop-types
children: PropTypes.node.isRequired,
currency: PropTypes.string,
locale: PropTypes.string,
localeFallback: PropTypes.bool,
messages: PropTypes.object, // eslint-disable-line react/forbid-prop-types
warnOnMissingMessage: PropTypes.bool,
};
FormattedWrapper.childContextTypes = {
globalize: globalizeShape,
};
FormattedWrapper.defaultProps = {
cldr: null,
currency: 'USD',
locale: 'en',
localeFallback: false,
messages: null,
warnOnMissingMessage: true,
};