-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
441 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { ReactElement } from "react"; | ||
import React, { useRef, useState } from "react"; | ||
import { HStack } from "@chakra-ui/react"; | ||
|
||
import { combineDateAndTime } from "../../../utils/DateUtils"; | ||
|
||
import DatePicker from "./DatePicker"; | ||
import TimePicker from "./TimePicker"; | ||
|
||
type DateTimePickerProps = { | ||
name?: string; | ||
onChange: (date: Date | null) => void; | ||
value: Date | null | undefined; | ||
isDisabled?: boolean; | ||
}; | ||
|
||
const DateTimePicker = ({ | ||
isDisabled, | ||
onChange, | ||
value, | ||
name, | ||
}: DateTimePickerProps): ReactElement => { | ||
const timePickerRef = useRef<HTMLDivElement>(null); | ||
|
||
const [internalDateValue, setInternalDateValue] = useState<Date | null>(null); | ||
const [internalTimeValue, setInternalTimeValue] = useState<Date | null>(null); | ||
const dateValue = value ?? internalDateValue; | ||
const timeValue = value ?? internalTimeValue; | ||
|
||
const handleDateChange = (newDate: Date | null) => { | ||
setInternalDateValue(newDate); | ||
if (timeValue && newDate) { | ||
onChange(combineDateAndTime(newDate, timeValue)); | ||
} | ||
|
||
// For some reason, the time picker doesn't focus properly if we click it | ||
// while the date picker is open. This is a workaround to make sure the | ||
// click happens after the date picker has started to close. | ||
setTimeout(() => { | ||
timePickerRef.current?.click(); | ||
}, 0); | ||
}; | ||
|
||
const handleTimeChange = (newTime: Date | null) => { | ||
setInternalTimeValue(newTime); | ||
if (dateValue && newTime) { | ||
onChange(combineDateAndTime(dateValue, newTime)); | ||
} | ||
}; | ||
|
||
return ( | ||
<HStack gap={4}> | ||
<DatePicker | ||
isDisabled={isDisabled} | ||
name={name} | ||
onChange={handleDateChange} | ||
value={dateValue ?? undefined} | ||
/> | ||
<TimePicker | ||
ref={timePickerRef} | ||
isDisabled={isDisabled} | ||
name={name} | ||
onChange={handleTimeChange} | ||
value={timeValue ?? undefined} | ||
/> | ||
</HStack> | ||
); | ||
}; | ||
|
||
export default DateTimePicker; |
Oops, something went wrong.