11import 'react-day-picker/lib/style.css' ;
2- import { CalendarMultiselectIcon , RecentIcon } from 'plotly-icons' ;
2+ import { CalendarMultiselectIcon } from 'plotly-icons' ;
33import { ms2DateTime , dateTime2ms } from 'plotly.js/src/lib/dates' ;
44import DayPicker from 'react-day-picker' ;
55import PropTypes from 'prop-types' ;
@@ -25,27 +25,13 @@ export default class DateTimePicker extends Component {
2525 super ( ) ;
2626 this . state = {
2727 calendarOpen : false ,
28- timepickerOpen : false ,
2928 } ;
3029
31- this . onToggle = this . onToggle . bind ( this ) ;
3230 this . toPlotlyJSDate = this . toPlotlyJSDate . bind ( this ) ;
3331 this . onMonthChange = this . onMonthChange . bind ( this ) ;
3432 this . onYearChange = this . onYearChange . bind ( this ) ;
3533 this . updateTime = this . updateTime . bind ( this ) ;
36- }
37-
38- componentWillReceiveProps ( nextProps ) {
39- // Reset the value to the graph's actual value
40- if ( nextProps . value !== this . props . value ) {
41- this . setState ( {
42- value : nextProps . value ,
43- } ) ;
44- }
45- }
46-
47- onToggle ( ) {
48- this . setState ( { expanded : ! this . state . expanded } ) ;
34+ this . updateDate = this . updateDate . bind ( this ) ;
4935 }
5036
5137 toPlotlyJSDate ( value ) {
@@ -54,17 +40,20 @@ export default class DateTimePicker extends Component {
5440 }
5541
5642 getYearOptions ( current ) {
57- const yearAsNumber = parseInt ( current , 10 ) ;
5843 // eslint-disable-next-line
59- const lastFive = new Array ( 5 ) . fill ( 0 ) . map ( ( year , index ) => {
60- const newOption = yearAsNumber - ( 5 - index ) ; // eslint-disable-line
44+ const base = 5 ;
45+ const yearAsNumber = parseInt ( current , 10 ) ;
46+
47+ const lastFive = new Array ( base ) . fill ( 0 ) . map ( ( year , index ) => {
48+ const newOption = yearAsNumber - ( base - index ) ;
6149 return { label : newOption , value : newOption } ;
6250 } ) ;
63- // eslint-disable-next-line
64- const nextFive = new Array ( 5 ) . fill ( 0 ) . map ( ( year , index ) => {
51+
52+ const nextFive = new Array ( base ) . fill ( 0 ) . map ( ( year , index ) => {
6553 const newOption = yearAsNumber + ( index + 1 ) ;
6654 return { label : newOption , value : newOption } ;
6755 } ) ;
56+
6857 return lastFive . concat ( [ { label : current , value : current } ] ) . concat ( nextFive ) ;
6958 }
7059
@@ -99,51 +88,58 @@ export default class DateTimePicker extends Component {
9988 }
10089
10190 getTime ( JSDate ) {
102- return `${ formatDigits ( JSDate . getHours ( ) , 2 ) } :${ formatDigits (
103- JSDate . getMinutes ( ) ,
104- 2
105- ) } :${ formatDigits ( JSDate . getSeconds ( ) , 2 ) } .${ formatDigits ( JSDate . getMilliseconds ( ) , 3 ) } `;
91+ return (
92+ `${ formatDigits ( JSDate . getHours ( ) , 2 ) } :` +
93+ `${ formatDigits ( JSDate . getMinutes ( ) , 2 ) } :` +
94+ `${ formatDigits ( JSDate . getSeconds ( ) , 2 ) } .` +
95+ `${ formatDigits ( JSDate . getMilliseconds ( ) , 3 ) } `
96+ ) ;
97+ }
98+
99+ parseDateTime ( value ) {
100+ const parsed = value . split ( ' ' ) ;
101+ return { date : parsed [ 0 ] , time : parsed [ 1 ] } ;
106102 }
107103
108104 updateTime ( value ) {
109- const date = this . props . value . split ( ' ' ) [ 0 ] ;
110- this . props . onChange ( date + ' ' + value ) ;
105+ const { date : currentDate , time : currentTime } = this . parseDateTime ( this . props . value ) ;
106+
107+ if ( value !== currentTime ) {
108+ this . props . onChange ( currentDate + ' ' + value ) ;
109+ }
110+ }
111+
112+ updateDate ( value ) {
113+ const { date : currentDate , time : currentTime } = this . parseDateTime ( this . props . value ) ;
114+
115+ if ( value !== currentDate ) {
116+ this . props . onChange ( value + ' ' + currentTime ) ;
117+ }
111118 }
112119
113120 render ( ) {
121+ const { date : currentDate } = this . parseDateTime ( this . props . value ) ;
114122 const JSDate = new Date ( this . props . value ) ;
115123 const currentYear = JSDate . getFullYear ( ) ;
116124
117125 return (
118126 < div className = "datetimepicker-container" >
119127 < TextInput
120- value = { this . props . value }
128+ value = { currentDate }
121129 editableClassName = { 'datetimepicker-container-input' }
122- onUpdate = { this . props . onChange }
130+ onUpdate = { this . updateDate }
123131 />
124132 < div className = "datetimepicker-container-icons" >
125133 < CalendarMultiselectIcon
126- onClick = { ( ) =>
127- this . setState ( { calendarOpen : ! this . state . calendarOpen , timepickerOpen : false } )
128- }
134+ onClick = { ( ) => this . setState ( { calendarOpen : ! this . state . calendarOpen } ) }
129135 className = {
130136 this . state . calendarOpen
131137 ? 'datetimepicker-date-icon--selected'
132138 : 'datetimepicker-date-icon'
133139 }
134140 />
135- < RecentIcon
136- onClick = { ( ) =>
137- this . setState ( { timepickerOpen : ! this . state . timepickerOpen , calendarOpen : false } )
138- }
139- className = {
140- this . state . timepickerOpen
141- ? 'datetimepicker-time-icon--selected'
142- : 'datetimepicker-time-icon'
143- }
144- />
145141 </ div >
146- { this . state . calendarOpen || this . state . timepickerOpen ? (
142+ { this . state . calendarOpen ? (
147143 < div className = "datetimepicker-container__content" >
148144 { this . state . calendarOpen ? (
149145 < div className = "datetimepicker-datepicker-container" >
@@ -168,27 +164,24 @@ export default class DateTimePicker extends Component {
168164 modifiers = { { highlighted : JSDate } }
169165 month = { JSDate }
170166 onDayClick = { value => {
171- this . props . onChange ( this . toPlotlyJSDate ( value ) ) ;
167+ const plotlyDate = this . toPlotlyJSDate ( value ) . split ( ' ' ) [ 0 ] ;
168+ this . updateDate ( plotlyDate ) ;
172169 } }
173170 />
174171 </ div >
175172 ) : null }
176- { this . state . timepickerOpen ? (
177- < div >
178- < TextInput
179- value = { this . getTime ( JSDate ) }
180- onUpdate = { this . updateTime }
181- placeHolder = "hh:mm:ss.xxx"
182- />
183- < span className = "datetimepicker-date-units" >
184- { new Date ( this . props . value ) . toLocaleTimeString ( 'en-US' ) . split ( ' ' ) [ 1 ] === 'PM'
185- ? 'PM'
186- : 'AM' }
187- </ span >
188- </ div >
189- ) : null }
190173 </ div >
191174 ) : null }
175+ < div className = "datetimepicker-container-time" >
176+ < TextInput
177+ value = { this . getTime ( JSDate ) }
178+ onUpdate = { this . updateTime }
179+ placeHolder = "hh:mm:ss.xxx"
180+ />
181+ < span className = "datetimepicker-date-units" >
182+ { JSDate . toLocaleTimeString ( 'en-US' ) . split ( ' ' ) [ 1 ] === 'PM' ? 'PM' : 'AM' }
183+ </ span >
184+ </ div >
192185 </ div >
193186 ) ;
194187 }
0 commit comments