Repository navigation
Expand file tree
/
Copy pathDurationMessage.js
More file actions
61 lines (50 loc) · 1.79 KB
/
Copy pathDurationMessage.js
File metadata and controls
61 lines (50 loc) · 1.79 KB
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
import React from 'react';
import PropTypes from 'prop-types';
import { intlShape, FormattedNumber, injectIntl } from 'react-intl';
import { formatDurationToParts } from './formatDuration';
class DurationMessage extends React.Component {
render() {
const intl = this.props.intl;
const {
seconds,
textComponent: Text = intl.textComponent,
valueComponent: Value = Text,
} = this.props;
const durationParts = formatDurationToParts(intl, seconds, this.props);
return (
<Text>
{durationParts.map(({ type, value }, i) => {
if (value.trim().length === 0) {
return value;
}
switch (type) {
case 'unit':
case 'literal':
return <Text key={i}>{value}</Text>;
default:
return <Value key={i}>{value}</Value>;
}
})}
</Text>
);
}
}
/*
* This is a hack, react-intl has an internal method "shouldIntlComponentUpdate".
* FormattedNumber#shouldComponentUpdate does nothing but delegate to that method.
* We need that method to check if we should update the component.
*
* To remove this hack, we should either:
* - Ask react-intl to expose shouldIntlComponentUpdate and support it officially
* - Reimplement it, which would have a big decently big impact on the bundle size (https://github.com/yahoo/react-intl/blob/master/src/utils.js)
*/
DurationMessage.prototype.shouldComponentUpdate = FormattedNumber.prototype.shouldComponentUpdate;
DurationMessage.contextTypes = { intl: intlShape };
DurationMessage.propTypes = {
intl: intlShape.isRequired,
format: PropTypes.string,
seconds: PropTypes.number.isRequired,
textComponent: PropTypes.func.isRequired,
valueComponent: PropTypes.func,
};
export default injectIntl(DurationMessage);