@@ -3,10 +3,23 @@ import {CalendarMultiselectIcon, RecentIcon} from 'plotly-icons';
33import { ms2DateTime , dateTime2ms } from 'plotly.js/src/lib/dates' ;
44import DayPicker from 'react-day-picker' ;
55import PropTypes from 'prop-types' ;
6- import React , { Component , Fragment } from 'react' ;
6+ import React , { Component } from 'react' ;
77import TextInput from './TextInput' ;
88import Dropdown from './Dropdown' ;
99
10+ function formatDigits ( number , nbDigits ) {
11+ const string = number . toString ( ) ;
12+ if ( string . length !== nbDigits ) {
13+ return (
14+ new Array ( nbDigits - string . length )
15+ . fill ( 0 )
16+ . join ( )
17+ . replace ( ',' , '' ) + number
18+ ) ;
19+ }
20+ return number ;
21+ }
22+
1023export default class DateTimePicker extends Component {
1124 constructor ( ) {
1225 super ( ) ;
@@ -19,6 +32,7 @@ export default class DateTimePicker extends Component {
1932 this . toPlotlyJSDate = this . toPlotlyJSDate . bind ( this ) ;
2033 this . onMonthChange = this . onMonthChange . bind ( this ) ;
2134 this . onYearChange = this . onYearChange . bind ( this ) ;
35+ this . updateTime = this . updateTime . bind ( this ) ;
2236 }
2337
2438 componentWillReceiveProps ( nextProps ) {
@@ -41,10 +55,12 @@ export default class DateTimePicker extends Component {
4155
4256 getYearOptions ( current ) {
4357 const yearAsNumber = parseInt ( current , 10 ) ;
58+ // eslint-disable-next-line
4459 const lastFive = new Array ( 5 ) . fill ( 0 ) . map ( ( year , index ) => {
45- const newOption = yearAsNumber - ( 5 - index ) ;
60+ const newOption = yearAsNumber - ( 5 - index ) ; // eslint-disable-line
4661 return { label : newOption , value : newOption } ;
4762 } ) ;
63+ // eslint-disable-next-line
4864 const nextFive = new Array ( 5 ) . fill ( 0 ) . map ( ( year , index ) => {
4965 const newOption = yearAsNumber + ( index + 1 ) ;
5066 return { label : newOption , value : newOption } ;
@@ -82,12 +98,21 @@ export default class DateTimePicker extends Component {
8298 this . props . onChange ( this . toPlotlyJSDate ( currentDateInJS ) ) ;
8399 }
84100
101+ getTime ( JSDate ) {
102+ return `${ formatDigits ( JSDate . getHours ( ) , 2 ) } :${ formatDigits (
103+ JSDate . getMinutes ( ) ,
104+ 2
105+ ) } :${ formatDigits ( JSDate . getSeconds ( ) , 2 ) } .${ formatDigits ( JSDate . getMilliseconds ( ) , 3 ) } `;
106+ }
107+
108+ updateTime ( value ) {
109+ const date = this . props . value . split ( ' ' ) [ 0 ] ;
110+ this . props . onChange ( date + ' ' + value ) ;
111+ }
112+
85113 render ( ) {
86114 const JSDate = new Date ( this . props . value ) ;
87- const currentMonth = JSDate . getMonth ( ) ;
88115 const currentYear = JSDate . getFullYear ( ) ;
89- const monthOptions = this . getMonthOptions ( ) ;
90- const yearOptions = this . getYearOptions ( currentYear ) ;
91116
92117 return (
93118 < div className = "datetimepicker-container" >
@@ -124,14 +149,14 @@ export default class DateTimePicker extends Component {
124149 < div className = "datetimepicker-datepicker-container" >
125150 < div className = "datetimepicker-datepicker-navbar" >
126151 < Dropdown
127- options = { monthOptions }
128- value = { currentMonth }
152+ options = { this . getMonthOptions ( ) }
153+ value = { JSDate . getMonth ( ) }
129154 className = "datimepicker-monthpicker"
130155 clearable = { false }
131156 onChange = { this . onMonthChange }
132157 />
133158 < Dropdown
134- options = { yearOptions }
159+ options = { this . getYearOptions ( currentYear ) }
135160 value = { currentYear }
136161 className = "datimepicker-yearpicker"
137162 clearable = { false }
@@ -151,10 +176,9 @@ export default class DateTimePicker extends Component {
151176 { this . state . timepickerOpen ? (
152177 < div >
153178 < TextInput
154- value = { JSDate }
155- onUpdate = { value => {
156- this . props . onChange ( this . toPlotlyJSDate ( value ) ) ;
157- } }
179+ value = { this . getTime ( JSDate ) }
180+ onUpdate = { this . updateTime }
181+ placeHolder = "hh:mm:ss.xxx"
158182 />
159183 < span className = "datetimepicker-date-units" >
160184 { new Date ( this . props . value ) . toLocaleTimeString ( 'en-US' ) . split ( ' ' ) [ 1 ] === 'PM'
0 commit comments