@@ -21,54 +21,50 @@ export type DateTimeRangeProps = {
2121} ;
2222
2323/** Display a range between 2 timestamps. */
24- export default class DateTimeRange extends React . PureComponent < DateTimeRangeProps > {
25- static defaultProps = {
26- from : null ,
27- separator : ' – ' ,
28- to : null ,
29- } ;
24+ export default function DateTimeRange ( {
25+ from,
26+ locale,
27+ separator = ' – ' ,
28+ timezone,
29+ to,
30+ } : DateTimeRangeProps ) {
31+ if ( ! from || ! to ) {
32+ return < Empty /> ;
33+ }
3034
31- render ( ) {
32- const { from , locale, separator , timezone, to } = this . props ;
35+ const fromTimeStamp = createDateTime ( from , { locale , timezone } ) ;
36+ const toTimeStamp = createDateTime ( to , { locale, timezone } ) ;
3337
34- if ( ! from || ! to ) {
35- return < Empty /> ;
38+ if ( __DEV__ ) {
39+ if ( ! fromTimeStamp . isValid || ! toTimeStamp . isValid ) {
40+ throw new Error ( 'Invalid timestamps passed to `DateTimeRange`.' ) ;
3641 }
3742
38- const fromTimeStamp = createDateTime ( from , { locale, timezone } ) ;
39- const toTimeStamp = createDateTime ( to , { locale, timezone } ) ;
40-
41- if ( __DEV__ ) {
42- if ( ! fromTimeStamp . isValid || ! toTimeStamp . isValid ) {
43- throw new Error ( 'Invalid timestamps passed to `DateTimeRange`.' ) ;
44- }
45-
46- if ( toTimeStamp < fromTimeStamp ) {
47- throw new Error ( 'Invalid chronological order of timestamps passed to `DateTimeRange`.' ) ;
48- }
43+ if ( toTimeStamp < fromTimeStamp ) {
44+ throw new Error ( 'Invalid chronological order of timestamps passed to `DateTimeRange`.' ) ;
4945 }
46+ }
5047
51- const props = { locale, timezone } ;
52- let fromFormat = rangeFromDayBundle . get ( locale ) ;
53- let toFormat ;
54-
55- if ( fromTimeStamp . year !== toTimeStamp . year ) {
56- fromFormat = dateMediumBundle . get ( locale ) ;
57- toFormat = dateMediumBundle . get ( locale ) ;
58- } else if ( fromTimeStamp . month !== toTimeStamp . month ) {
59- toFormat = dateMediumBundle . get ( locale ) ;
60- } else if ( fromTimeStamp . day !== toTimeStamp . day ) {
61- toFormat = rangeToDayBundle . get ( locale ) ;
62- } else {
63- return < DateTime { ...props } medium noTime noTimezone at = { toTimeStamp } /> ;
64- }
48+ const props = { locale, timezone } ;
49+ let fromFormat = rangeFromDayBundle . get ( locale ) ;
50+ let toFormat ;
6551
66- return (
67- < span >
68- < DateTime { ...props } at = { fromTimeStamp } format = { fromFormat } />
69- { separator }
70- < DateTime { ...props } at = { toTimeStamp } format = { toFormat } />
71- </ span >
72- ) ;
52+ if ( fromTimeStamp . year !== toTimeStamp . year ) {
53+ fromFormat = dateMediumBundle . get ( locale ) ;
54+ toFormat = dateMediumBundle . get ( locale ) ;
55+ } else if ( fromTimeStamp . month !== toTimeStamp . month ) {
56+ toFormat = dateMediumBundle . get ( locale ) ;
57+ } else if ( fromTimeStamp . day !== toTimeStamp . day ) {
58+ toFormat = rangeToDayBundle . get ( locale ) ;
59+ } else {
60+ return < DateTime { ...props } medium noTime noTimezone at = { toTimeStamp } /> ;
7361 }
62+
63+ return (
64+ < span >
65+ < DateTime { ...props } at = { fromTimeStamp } format = { fromFormat } />
66+ { separator }
67+ < DateTime { ...props } at = { toTimeStamp } format = { toFormat } />
68+ </ span >
69+ ) ;
7470}
0 commit comments