| 1 |
import { useState } from 'react'; |
| 2 |
import { |
| 3 |
Flex, |
| 4 |
FlexItem, |
| 5 |
InputGroup, |
| 6 |
InputGroupItem, |
| 7 |
DatePicker, |
| 8 |
isValidDate, |
| 9 |
TimePicker, |
| 10 |
yyyyMMddFormat |
| 11 |
} from '@patternfly/react-core'; |
| 12 |
|
| 13 |
export const DateTimeRangePicker: React.FunctionComponent = () => { |
| 14 |
const [from, setFrom] = useState<Date | undefined>(undefined); |
| 15 |
const [to, setTo] = useState<Date | undefined>(undefined); |
| 16 |
|
| 17 |
const toValidator = (date: Date) => |
| 18 |
isValidDate(from) && yyyyMMddFormat(date) >= yyyyMMddFormat(from) |
| 19 |
? '' |
| 20 |
: 'The "to" date must be after the "from" date'; |
| 21 |
|
| 22 |
const onFromDateChange = (_event: React.FormEvent<HTMLInputElement>, inputDate: string, newFromDate: Date) => { |
| 23 |
if (isValidDate(from) && isValidDate(newFromDate) && inputDate === yyyyMMddFormat(newFromDate)) { |
| 24 |
newFromDate.setHours(from.getHours()); |
| 25 |
newFromDate.setMinutes(from.getMinutes()); |
| 26 |
} |
| 27 |
if (isValidDate(newFromDate) && inputDate === yyyyMMddFormat(newFromDate)) { |
| 28 |
setFrom(new Date(newFromDate)); |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
const onFromTimeChange = (_event: React.FormEvent<HTMLInputElement>, _time: string, hour: number, minute: number) => { |
| 33 |
if (isValidDate(from)) { |
| 34 |
const updatedFromDate = new Date(from); |
| 35 |
updatedFromDate.setHours(hour); |
| 36 |
updatedFromDate.setMinutes(minute); |
| 37 |
setFrom(updatedFromDate); |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
const onToDateChange = (_event: React.FormEvent<HTMLInputElement>, inputDate: string, newToDate: Date) => { |
| 42 |
if (isValidDate(to) && isValidDate(newToDate) && inputDate === yyyyMMddFormat(newToDate)) { |
| 43 |
newToDate.setHours(to.getHours()); |
| 44 |
newToDate.setMinutes(to.getMinutes()); |
| 45 |
} |
| 46 |
if (isValidDate(newToDate) && inputDate === yyyyMMddFormat(newToDate)) { |
| 47 |
setTo(newToDate); |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
const onToTimeChange = (_event: React.FormEvent<HTMLInputElement>, _time: string, hour: number, minute: number) => { |
| 52 |
if (isValidDate(to)) { |
| 53 |
const updatedToDate = new Date(to); |
| 54 |
updatedToDate.setHours(hour); |
| 55 |
updatedToDate.setMinutes(minute); |
| 56 |
setTo(updatedToDate); |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
return ( |
| 61 |
<Flex direction={{ default: 'column', lg: 'row' }}> |
| 62 |
<FlexItem> |
| 63 |
<InputGroup> |
| 64 |
<InputGroupItem> |
| 65 |
<DatePicker onChange={onFromDateChange} aria-label="Start date" placeholder="YYYY-MM-DD" /> |
| 66 |
</InputGroupItem> |
| 67 |
<InputGroupItem> |
| 68 |
<TimePicker aria-label="Start time" style={{ width: '150px' }} onChange={onFromTimeChange} /> |
| 69 |
</InputGroupItem> |
| 70 |
</InputGroup> |
| 71 |
</FlexItem> |
| 72 |
<FlexItem>to</FlexItem> |
| 73 |
<FlexItem> |
| 74 |
<InputGroup> |
| 75 |
<InputGroupItem> |
| 76 |
<DatePicker |
| 77 |
value={isValidDate(to) ? yyyyMMddFormat(to) : to} |
| 78 |
onChange={onToDateChange} |
| 79 |
isDisabled={!isValidDate(from)} |
| 80 |
rangeStart={from} |
| 81 |
validators={[toValidator]} |
| 82 |
aria-label="End date" |
| 83 |
placeholder="YYYY-MM-DD" |
| 84 |
/> |
| 85 |
</InputGroupItem> |
| 86 |
<InputGroupItem> |
| 87 |
<TimePicker style={{ width: '150px' }} onChange={onToTimeChange} isDisabled={!isValidDate(from)} /> |
| 88 |
</InputGroupItem> |
| 89 |
</InputGroup> |
| 90 |
</FlexItem> |
| 91 |
</Flex> |
| 92 |
); |
| 93 |
}; |