@@ -13,9 +13,9 @@ const datePlaceholder = 'yyyy-mm-dd';
1313const timePlaceholder = 'hh:mm:ss.xxx' ;
1414
1515export default class DateTimePicker extends Component {
16- constructor ( props ) {
17- super ( props ) ;
18- const { time, date} = this . parseDateTime ( props . value ) ;
16+ constructor ( props , context ) {
17+ super ( props , context ) ;
18+ const { time, date} = this . parsePlotlyJSDateTime ( props . value ) ;
1919 const isValidTime = isDateTime ( testDate + ' ' + time ) || [ '' , timePlaceholder ] . includes ( time ) ;
2020 const isValidDate = isDateTime ( date + ' ' + testTime ) || [ '' , datePlaceholder ] . includes ( date ) ;
2121
@@ -29,6 +29,7 @@ export default class DateTimePicker extends Component {
2929 : 'datetimepicker-container-time-input +error' ,
3030 timeValue : time ,
3131 dateValue : date ,
32+ AMPM : this . getAMPM ( date , time , context . localize ) ,
3233 } ;
3334
3435 this . toPlotlyJSDate = this . toPlotlyJSDate . bind ( this ) ;
@@ -82,50 +83,75 @@ export default class DateTimePicker extends Component {
8283 }
8384
8485 onMonthChange ( value ) {
85- const currentDateInJS = new Date ( this . props . value ) ;
86+ const currentDateInJS = new Date ( this . getAdjustedPlotlyJSDateTime ( this . props . value ) ) ;
8687 currentDateInJS . setMonth ( value ) ;
8788 const plotlyJSDate = this . toPlotlyJSDate ( currentDateInJS ) ;
8889
8990 if ( isDateTime ( plotlyJSDate ) ) {
9091 this . props . onChange ( plotlyJSDate ) ;
9192 }
9293
93- const { time, date} = this . parseDateTime ( plotlyJSDate ) ;
94+ const { time, date} = this . parsePlotlyJSDateTime ( plotlyJSDate ) ;
9495 this . setState ( {
9596 timeValue : time ,
9697 dateValue : date ,
9798 } ) ;
9899 }
99100
100101 onYearChange ( value ) {
101- const currentDateInJS = new Date ( this . props . value ) ;
102+ const currentDateInJS = new Date ( this . getAdjustedPlotlyJSDateTime ( this . props . value ) ) ;
102103 currentDateInJS . setFullYear ( value ) ;
103104 const plotlyJSDate = this . toPlotlyJSDate ( currentDateInJS ) ;
104105
105106 if ( isDateTime ( plotlyJSDate ) ) {
106107 this . props . onChange ( plotlyJSDate ) ;
107108 }
108109
109- const { time, date} = this . parseDateTime ( plotlyJSDate ) ;
110+ const { time, date} = this . parsePlotlyJSDateTime ( plotlyJSDate ) ;
110111 this . setState ( {
111112 timeValue : time ,
112113 dateValue : date ,
113114 } ) ;
114115 }
115116
116- parseDateTime ( value ) {
117+ parsePlotlyJSDateTime ( value ) {
117118 const parsed = value . split ( ' ' ) ;
118119 return { date : parsed [ 0 ] , time : parsed [ 1 ] ? parsed [ 1 ] : '' } ;
119120 }
120121
122+ getAMPM ( date , time , _ ) {
123+ const plotlyJSDateTime = date + ' ' + time ;
124+ const isValidDateTime = isDateTime ( plotlyJSDateTime ) ;
125+ const JSDate = new Date ( this . getAdjustedPlotlyJSDateTime ( plotlyJSDateTime ) ) ;
126+ const localeTime = JSDate . toLocaleTimeString ( 'en-US' ) . split ( ' ' ) ;
127+
128+ return ! isValidDateTime || time === '' || JSDate . toDateString ( ) === 'Invalid Date'
129+ ? ''
130+ : localeTime [ 1 ] === 'PM'
131+ ? localeTime [ 0 ] . startsWith ( '12' )
132+ ? _ ( 'noon' )
133+ : 'PM'
134+ : 'AM' ;
135+ }
136+
137+ adjustedTime ( time ) {
138+ if ( time . toString ( ) . length <= 2 ) {
139+ return time + ':00' ;
140+ }
141+ return time ;
142+ }
143+
121144 onTimeChange ( value ) {
145+ const { date : currentDate } = this . parsePlotlyJSDateTime ( this . props . value ) ;
122146 const isValidTime = isDateTime ( testDate + ' ' + value ) ;
147+
123148 this . setState ( {
124149 timeInputClassName :
125150 isValidTime || value === ''
126151 ? 'datetimepicker-container-time-input'
127152 : 'datetimepicker-container-time-input +error' ,
128153 timeValue : value ,
154+ AMPM : this . getAMPM ( currentDate , value , this . context . localize ) ,
129155 } ) ;
130156 }
131157
@@ -141,13 +167,14 @@ export default class DateTimePicker extends Component {
141167 }
142168
143169 onTimeUpdate ( value ) {
144- const { time : currentTime , date : currentDate } = this . parseDateTime ( this . props . value ) ;
170+ const { time : currentTime , date : currentDate } = this . parsePlotlyJSDateTime ( this . props . value ) ;
145171 const isValidTime = isDateTime ( testDate + ' ' + value ) ;
146172
147173 if ( value === '' ) {
148174 this . setState ( {
149175 timeInputClassName : 'datetimepicker-container-time-input' ,
150176 timeValue : currentTime ,
177+ AMPM : this . getAMPM ( currentDate , currentTime , this . context . localize ) ,
151178 } ) ;
152179 return ;
153180 }
@@ -158,7 +185,7 @@ export default class DateTimePicker extends Component {
158185 }
159186
160187 onDateUpdate ( value ) {
161- const { date : currentDate , time : currentTime } = this . parseDateTime ( this . props . value ) ;
188+ const { date : currentDate , time : currentTime } = this . parsePlotlyJSDateTime ( this . props . value ) ;
162189 const isValidDate = isDateTime ( value + ' ' + testTime ) ;
163190
164191 if ( isValidDate ) {
@@ -175,9 +202,13 @@ export default class DateTimePicker extends Component {
175202 }
176203 }
177204
205+ getAdjustedPlotlyJSDateTime ( dateTimeString ) {
206+ const { date, time} = this . parsePlotlyJSDateTime ( dateTimeString ) ;
207+ return date + ' ' + this . adjustedTime ( time ) ;
208+ }
209+
178210 render ( ) {
179- const JSDate = new Date ( this . props . value ) ;
180- const localeTime = JSDate . toLocaleTimeString ( 'en-US' ) . split ( ' ' ) ;
211+ const JSDate = new Date ( this . getAdjustedPlotlyJSDateTime ( this . props . value ) ) ;
181212 const currentYear = JSDate . getFullYear ( ) ;
182213
183214 return (
@@ -241,9 +272,7 @@ export default class DateTimePicker extends Component {
241272 placeHolder = { timePlaceholder }
242273 editableClassName = { this . state . timeInputClassName }
243274 />
244- < span className = "datetimepicker-date-units" >
245- { localeTime [ 1 ] === 'PM' ? ( localeTime [ 0 ] . startsWith ( '12:' ) ? 'noon' : 'PM' ) : 'AM' }
246- </ span >
275+ < span className = "datetimepicker-date-units" > { this . state . AMPM } </ span >
247276 </ div >
248277 </ div >
249278 ) ;
0 commit comments