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/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: PropTypes.any,
26
- date: PropTypes.oneOfType(dateTypes),
27
- parse: PropTypes.oneOfType(parseTypes),
28
- format: PropTypes.string,
29
- add: PropTypes.object,
30
- subtract: PropTypes.object,
31
- ago: PropTypes.bool,
32
- fromNow: PropTypes.bool,
33
- fromNowDuring: PropTypes.number,
34
- from: PropTypes.oneOfType(dateTypes),
35
- toNow: PropTypes.bool,
36
- to: PropTypes.oneOfType(dateTypes),
37
- calendar: PropTypes.oneOfType(calendarTypes),
38
- unix: PropTypes.bool,
39
- utc: PropTypes.bool,
40
- tz: PropTypes.string,
41
- withTitle: PropTypes.bool,
42
- titleFormat: PropTypes.string,
43
- locale: PropTypes.string,
44
- interval: PropTypes.number,
45
- diff: PropTypes.oneOfType(dateTypes),
46
- unit: PropTypes.string,
47
- decimal: PropTypes.bool,
48
- filter: PropTypes.func,
49
- onChange: PropTypes.func
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: 'time',
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 = null;
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(Moment.globalElement || this.props.element, {
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
  });