Skip to content

Commit e93ce95

Browse files
fix: datepicker time format on input should stay the same when calendar selection is made (#4336)
1 parent ec13fb0 commit e93ce95

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

src/shared/components/dateRangePicker/DatePicker.tsx

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class DatePicker extends PureComponent<Props, State> {
7777
private inCurrentMonth: boolean = false
7878
state = {
7979
inputValue: null,
80-
inputFormat: null,
80+
inputFormat: DEFAULT_TIME_FORMAT,
8181
}
8282

8383
public componentDidUpdate() {
@@ -198,19 +198,6 @@ class DatePicker extends PureComponent<Props, State> {
198198
return 'range-picker--day'
199199
}
200200

201-
private overrideInputState = (): void => {
202-
const {dateTime, timeZone} = this.props
203-
const {inputFormat} = this.state
204-
205-
let value = new Date(dateTime).toISOString()
206-
if (inputFormat) {
207-
const formatter = createDateTimeFormatter(inputFormat, timeZone)
208-
value = formatter.format(dateTime)
209-
}
210-
211-
this.setState({inputValue: value, inputFormat: getFormat(value)})
212-
}
213-
214201
private handleSelectDate = (date: Date): void => {
215202
const {onSelectDate, timeZone} = this.props
216203

@@ -223,7 +210,6 @@ class DatePicker extends PureComponent<Props, State> {
223210
}
224211

225212
onSelectDate(date.toISOString())
226-
this.overrideInputState()
227213
}
228214

229215
private handleChangeInput = (e: ChangeEvent<HTMLInputElement>): void => {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react'
2+
3+
import DateRangePicker from './DateRangePicker'
4+
import {convertTimeRangeToCustom} from 'src/shared/utils/duration'
5+
import {pastHourTimeRange} from 'src/shared/constants/timeRanges'
6+
import {renderWithReduxAndRouter} from 'src/mockState'
7+
8+
import {screen} from '@testing-library/dom'
9+
import {fireEvent} from '@testing-library/react'
10+
11+
jest.useFakeTimers().setSystemTime(new Date('2022-03-10 20:00:00').getTime())
12+
13+
describe('Date Range Picker', function() {
14+
it('should have input values in the correct format after user changes the date selection', () => {
15+
const {getAllByText} = renderWithReduxAndRouter(
16+
<DateRangePicker
17+
timeRange={convertTimeRangeToCustom(pastHourTimeRange)}
18+
onSetTimeRange={() => {}}
19+
onClose={() => {}}
20+
position={{position: 'relative'}}
21+
/>
22+
)
23+
24+
// YYYY-MM-DD HH:mm format by default
25+
expect(screen.getByTitle('Start')).toHaveValue('2022-03-10 19:00:00')
26+
expect(screen.getByTitle('Stop')).toHaveValue('2022-03-10 20:00:00')
27+
28+
// change the time and the format should stay the same
29+
30+
const lowerTime = getAllByText('18:00')[0]
31+
const upperTime = getAllByText('21:00')[1]
32+
33+
fireEvent.click(lowerTime)
34+
fireEvent.click(upperTime)
35+
36+
expect(screen.getByTitle('Start')).toHaveValue('2022-03-10 18:00:00')
37+
expect(screen.getByTitle('Stop')).toHaveValue('2022-03-10 21:00:00')
38+
})
39+
40+
it('should have the `Apply Time Range` Button disabled on invalid input', () => {
41+
const {getAllByTestId, getByTestId} = renderWithReduxAndRouter(
42+
<DateRangePicker
43+
timeRange={convertTimeRangeToCustom(pastHourTimeRange)}
44+
onSetTimeRange={() => {}}
45+
onClose={() => {}}
46+
position={{position: 'relative'}}
47+
/>
48+
)
49+
const lowerInput = getAllByTestId('timerange--input')[0]
50+
51+
fireEvent.change(lowerInput, {target: {value: 'iampumpkinthecat;)'}})
52+
53+
const applyButton = getByTestId('daterange--apply-btn')
54+
55+
expect(applyButton).toBeDisabled()
56+
})
57+
})

0 commit comments

Comments
 (0)