@@ -13,9 +13,9 @@ const datePlaceholder = 'yyyy-mm-dd';
13
13
const timePlaceholder = 'hh:mm:ss.xxx' ;
14
14
15
15
export 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 ) ;
19
19
const isValidTime = isDateTime ( testDate + ' ' + time ) || [ '' , timePlaceholder ] . includes ( time ) ;
20
20
const isValidDate = isDateTime ( date + ' ' + testTime ) || [ '' , datePlaceholder ] . includes ( date ) ;
21
21
@@ -29,6 +29,7 @@ export default class DateTimePicker extends Component {
29
29
: 'datetimepicker-container-time-input +error' ,
30
30
timeValue : time ,
31
31
dateValue : date ,
32
+ AMPM : this . getAMPM ( date , time , context . localize ) ,
32
33
} ;
33
34
34
35
this . toPlotlyJSDate = this . toPlotlyJSDate . bind ( this ) ;
@@ -82,50 +83,75 @@ export default class DateTimePicker extends Component {
82
83
}
83
84
84
85
onMonthChange ( value ) {
85
- const currentDateInJS = new Date ( this . props . value ) ;
86
+ const currentDateInJS = new Date ( this . getAdjustedPlotlyJSDateTime ( this . props . value ) ) ;
86
87
currentDateInJS . setMonth ( value ) ;
87
88
const plotlyJSDate = this . toPlotlyJSDate ( currentDateInJS ) ;
88
89
89
90
if ( isDateTime ( plotlyJSDate ) ) {
90
91
this . props . onChange ( plotlyJSDate ) ;
91
92
}
92
93
93
- const { time, date} = this . parseDateTime ( plotlyJSDate ) ;
94
+ const { time, date} = this . parsePlotlyJSDateTime ( plotlyJSDate ) ;
94
95
this . setState ( {
95
96
timeValue : time ,
96
97
dateValue : date ,
97
98
} ) ;
98
99
}
99
100
100
101
onYearChange ( value ) {
101
- const currentDateInJS = new Date ( this . props . value ) ;
102
+ const currentDateInJS = new Date ( this . getAdjustedPlotlyJSDateTime ( this . props . value ) ) ;
102
103
currentDateInJS . setFullYear ( value ) ;
103
104
const plotlyJSDate = this . toPlotlyJSDate ( currentDateInJS ) ;
104
105
105
106
if ( isDateTime ( plotlyJSDate ) ) {
106
107
this . props . onChange ( plotlyJSDate ) ;
107
108
}
108
109
109
- const { time, date} = this . parseDateTime ( plotlyJSDate ) ;
110
+ const { time, date} = this . parsePlotlyJSDateTime ( plotlyJSDate ) ;
110
111
this . setState ( {
111
112
timeValue : time ,
112
113
dateValue : date ,
113
114
} ) ;
114
115
}
115
116
116
- parseDateTime ( value ) {
117
+ parsePlotlyJSDateTime ( value ) {
117
118
const parsed = value . split ( ' ' ) ;
118
119
return { date : parsed [ 0 ] , time : parsed [ 1 ] ? parsed [ 1 ] : '' } ;
119
120
}
120
121
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
+
121
144
onTimeChange ( value ) {
145
+ const { date : currentDate } = this . parsePlotlyJSDateTime ( this . props . value ) ;
122
146
const isValidTime = isDateTime ( testDate + ' ' + value ) ;
147
+
123
148
this . setState ( {
124
149
timeInputClassName :
125
150
isValidTime || value === ''
126
151
? 'datetimepicker-container-time-input'
127
152
: 'datetimepicker-container-time-input +error' ,
128
153
timeValue : value ,
154
+ AMPM : this . getAMPM ( currentDate , value , this . context . localize ) ,
129
155
} ) ;
130
156
}
131
157
@@ -141,13 +167,14 @@ export default class DateTimePicker extends Component {
141
167
}
142
168
143
169
onTimeUpdate ( value ) {
144
- const { time : currentTime , date : currentDate } = this . parseDateTime ( this . props . value ) ;
170
+ const { time : currentTime , date : currentDate } = this . parsePlotlyJSDateTime ( this . props . value ) ;
145
171
const isValidTime = isDateTime ( testDate + ' ' + value ) ;
146
172
147
173
if ( value === '' ) {
148
174
this . setState ( {
149
175
timeInputClassName : 'datetimepicker-container-time-input' ,
150
176
timeValue : currentTime ,
177
+ AMPM : this . getAMPM ( currentDate , currentTime , this . context . localize ) ,
151
178
} ) ;
152
179
return ;
153
180
}
@@ -158,7 +185,7 @@ export default class DateTimePicker extends Component {
158
185
}
159
186
160
187
onDateUpdate ( value ) {
161
- const { date : currentDate , time : currentTime } = this . parseDateTime ( this . props . value ) ;
188
+ const { date : currentDate , time : currentTime } = this . parsePlotlyJSDateTime ( this . props . value ) ;
162
189
const isValidDate = isDateTime ( value + ' ' + testTime ) ;
163
190
164
191
if ( isValidDate ) {
@@ -175,9 +202,13 @@ export default class DateTimePicker extends Component {
175
202
}
176
203
}
177
204
205
+ getAdjustedPlotlyJSDateTime ( dateTimeString ) {
206
+ const { date, time} = this . parsePlotlyJSDateTime ( dateTimeString ) ;
207
+ return date + ' ' + this . adjustedTime ( time ) ;
208
+ }
209
+
178
210
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 ) ) ;
181
212
const currentYear = JSDate . getFullYear ( ) ;
182
213
183
214
return (
@@ -241,9 +272,7 @@ export default class DateTimePicker extends Component {
241
272
placeHolder = { timePlaceholder }
242
273
editableClassName = { this . state . timeInputClassName }
243
274
/>
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 >
247
276
</ div >
248
277
</ div >
249
278
) ;
0 commit comments