Skip to content

Commit d2baa7a

Browse files
committed
Improve type accuracy for value prop and all callbacks
1 parent 9d814f4 commit d2baa7a

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/DateTimePicker.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type DateTimePickerProps = {
8686
nativeInputAriaLabel?: string;
8787
onCalendarClose?: () => void;
8888
onCalendarOpen?: () => void;
89-
onChange?: (value: Date | null) => void;
89+
onChange?: (value: Value) => void;
9090
onClockClose?: () => void;
9191
onClockOpen?: () => void;
9292
onFocus?: (event: React.FocusEvent<HTMLDivElement>) => void;
@@ -212,7 +212,7 @@ export default function DateTimePicker(props: DateTimePickerProps) {
212212
closeClock();
213213
}, [closeCalendar, closeClock]);
214214

215-
function onChange(value: Date | null, shouldCloseWidgets: boolean = shouldCloseWidgetsProps) {
215+
function onChange(value: Value, shouldCloseWidgets: boolean = shouldCloseWidgetsProps) {
216216
if (shouldCloseWidgets) {
217217
closeWidgets();
218218
}
@@ -222,7 +222,13 @@ export default function DateTimePicker(props: DateTimePickerProps) {
222222
}
223223
}
224224

225-
function onDateChange(nextValue: Value, shouldCloseWidgets?: boolean) {
225+
type DatePiece = Date | null;
226+
227+
function onDateChange(
228+
nextValue: DatePiece | [DatePiece, DatePiece],
229+
shouldCloseWidgets?: boolean,
230+
) {
231+
// React-Calendar passes an array of values when selectRange is enabled
226232
const [nextValueFrom] = Array.isArray(nextValue) ? nextValue : [nextValue];
227233
const [valueFrom] = Array.isArray(value) ? value : [value];
228234

src/shared/types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,4 @@ export type LooseValuePiece = string | Date | null;
88

99
export type LooseValue = LooseValuePiece | [LooseValuePiece, LooseValuePiece];
1010

11-
type ValuePiece = Date | null;
12-
13-
export type Value = ValuePiece | [ValuePiece, ValuePiece];
11+
export type Value = Date | null;

0 commit comments

Comments
 (0)