react-moment 0.8.0 → 0.8.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/workspace.xml +234 -177
- package/README.md +76 -29
- package/coverage/clover.xml +105 -98
- package/coverage/coverage-final.json +1 -1
- package/coverage/lcov-report/index.html +14 -14
- package/coverage/lcov-report/index.jsx.html +70 -34
- package/coverage/lcov-report/objects.js.html +1 -1
- package/coverage/lcov.info +210 -195
- package/dist/index.d.ts +3 -0
- package/dist/index.js +12 -2
- package/package.json +3 -3
- package/src/index.jsx +41 -29
- package/tests/index.jsx +16 -0
package/src/index.jsx
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import PropTypes from 'prop-types';
|
|
3
3
|
import moment from 'moment';
|
|
4
|
+
import 'moment-duration-format';
|
|
4
5
|
import { objectKeyFilter } from './objects';
|
|
5
6
|
|
|
6
7
|
const dateTypes = [
|
|
@@ -22,35 +23,37 @@ const calendarTypes = [
|
|
|
22
23
|
|
|
23
24
|
export default class Moment extends React.Component {
|
|
24
25
|
static propTypes = {
|
|
25
|
-
element:
|
|
26
|
-
date:
|
|
27
|
-
parse:
|
|
28
|
-
format:
|
|
29
|
-
add:
|
|
30
|
-
subtract:
|
|
31
|
-
ago:
|
|
32
|
-
fromNow:
|
|
33
|
-
fromNowDuring:
|
|
34
|
-
from:
|
|
35
|
-
toNow:
|
|
36
|
-
to:
|
|
37
|
-
calendar:
|
|
38
|
-
unix:
|
|
39
|
-
utc:
|
|
40
|
-
tz:
|
|
41
|
-
withTitle:
|
|
42
|
-
titleFormat:
|
|
43
|
-
locale:
|
|
44
|
-
interval:
|
|
45
|
-
diff:
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
26
|
+
element: PropTypes.any,
|
|
27
|
+
date: PropTypes.oneOfType(dateTypes),
|
|
28
|
+
parse: PropTypes.oneOfType(parseTypes),
|
|
29
|
+
format: PropTypes.string,
|
|
30
|
+
add: PropTypes.object,
|
|
31
|
+
subtract: PropTypes.object,
|
|
32
|
+
ago: PropTypes.bool,
|
|
33
|
+
fromNow: PropTypes.bool,
|
|
34
|
+
fromNowDuring: PropTypes.number,
|
|
35
|
+
from: PropTypes.oneOfType(dateTypes),
|
|
36
|
+
toNow: PropTypes.bool,
|
|
37
|
+
to: PropTypes.oneOfType(dateTypes),
|
|
38
|
+
calendar: PropTypes.oneOfType(calendarTypes),
|
|
39
|
+
unix: PropTypes.bool,
|
|
40
|
+
utc: PropTypes.bool,
|
|
41
|
+
tz: PropTypes.string,
|
|
42
|
+
withTitle: PropTypes.bool,
|
|
43
|
+
titleFormat: PropTypes.string,
|
|
44
|
+
locale: PropTypes.string,
|
|
45
|
+
interval: PropTypes.number,
|
|
46
|
+
diff: PropTypes.oneOfType(dateTypes),
|
|
47
|
+
duration: PropTypes.oneOfType(dateTypes),
|
|
48
|
+
durationFromNow: PropTypes.bool,
|
|
49
|
+
unit: PropTypes.string,
|
|
50
|
+
decimal: PropTypes.bool,
|
|
51
|
+
filter: PropTypes.func,
|
|
52
|
+
onChange: PropTypes.func
|
|
50
53
|
};
|
|
51
54
|
|
|
52
55
|
static defaultProps = {
|
|
53
|
-
element:
|
|
56
|
+
element: null,
|
|
54
57
|
fromNow: false,
|
|
55
58
|
toNow: false,
|
|
56
59
|
calendar: false,
|
|
@@ -71,7 +74,7 @@ export default class Moment extends React.Component {
|
|
|
71
74
|
static globalFormat = null;
|
|
72
75
|
static globalParse = null;
|
|
73
76
|
static globalFilter = null;
|
|
74
|
-
static globalElement =
|
|
77
|
+
static globalElement = 'time';
|
|
75
78
|
static globalTimezone = null;
|
|
76
79
|
static pooledElements = [];
|
|
77
80
|
static pooledTimer = null;
|
|
@@ -272,7 +275,7 @@ export default class Moment extends React.Component {
|
|
|
272
275
|
props = props || this.props;
|
|
273
276
|
const {
|
|
274
277
|
fromNow, fromNowDuring, from, add, subtract, toNow, to, ago,
|
|
275
|
-
calendar, diff, unit, decimal
|
|
278
|
+
calendar, diff, duration, durationFromNow, unit, decimal
|
|
276
279
|
} = props;
|
|
277
280
|
|
|
278
281
|
let { format } = props;
|
|
@@ -302,10 +305,19 @@ export default class Moment extends React.Component {
|
|
|
302
305
|
content = datetime.calendar(null, calendar);
|
|
303
306
|
} else if (diff) {
|
|
304
307
|
content = datetime.diff(diff, unit, decimal);
|
|
308
|
+
} else if (duration) {
|
|
309
|
+
content = datetime.diff(duration);
|
|
310
|
+
} else if (durationFromNow) {
|
|
311
|
+
content = moment().diff(datetime);
|
|
305
312
|
} else {
|
|
306
313
|
content = datetime.toString();
|
|
307
314
|
}
|
|
308
315
|
|
|
316
|
+
if (duration || durationFromNow) {
|
|
317
|
+
content = moment.duration(content);
|
|
318
|
+
content = content.format(format);
|
|
319
|
+
}
|
|
320
|
+
|
|
309
321
|
const filter = Moment.globalFilter || this.props.filter;
|
|
310
322
|
content = filter(content);
|
|
311
323
|
|
|
@@ -325,7 +337,7 @@ export default class Moment extends React.Component {
|
|
|
325
337
|
props.title = this.getTitle();
|
|
326
338
|
}
|
|
327
339
|
|
|
328
|
-
return React.createElement(
|
|
340
|
+
return React.createElement(this.props.element || Moment.globalElement, {
|
|
329
341
|
dateTime: Moment.getDatetime(this.props),
|
|
330
342
|
...props
|
|
331
343
|
},
|
package/tests/index.jsx
CHANGED
|
@@ -297,4 +297,20 @@ describe('react-moment', () => {
|
|
|
297
297
|
);
|
|
298
298
|
expect(ReactDOM.findDOMNode(date).innerHTML).toEqual('1976-04-19 09');
|
|
299
299
|
});
|
|
300
|
+
|
|
301
|
+
it('globalElement', () => {
|
|
302
|
+
Moment.globalElement = 'span';
|
|
303
|
+
const date = TestUtils.renderIntoDocument(
|
|
304
|
+
<Moment parse="YYYY-MM-DD HH:mm">1976-04-19 12:59</Moment>
|
|
305
|
+
);
|
|
306
|
+
expect(ReactDOM.findDOMNode(date).tagName).toEqual('SPAN');
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('globalElement overwrite', () => {
|
|
310
|
+
Moment.globalElement = 'span';
|
|
311
|
+
const date = TestUtils.renderIntoDocument(
|
|
312
|
+
<Moment element="div" parse="YYYY-MM-DD HH:mm">1976-04-19 12:59</Moment>
|
|
313
|
+
);
|
|
314
|
+
expect(ReactDOM.findDOMNode(date).tagName).toEqual('DIV');
|
|
315
|
+
});
|
|
300
316
|
});
|