Skip to content

Commit ca901fd

Browse files
Avoid casting date input change event
1 parent ade8461 commit ca901fd

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

src/components/form-elements/date-input/DateInput.tsx

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import React, { HTMLProps, ChangeEvent, useEffect, useState } from 'react';
3+
import React, { ChangeEvent, EventHandler, HTMLProps, useEffect, useState } from 'react';
44
import classNames from 'classnames';
55
import { DayInput, MonthInput, YearInput } from './components/IndividualDateInputs';
66
import SingleInputFormGroup from '@components/utils/SingleInputFormGroup';
@@ -13,18 +13,24 @@ type DateInputValue = {
1313
year: string;
1414
};
1515

16-
export type DateInputChangeEvent = ChangeEvent<HTMLInputElement> & {
17-
target: HTMLInputElement & { value: DateInputValue };
18-
currentTarget: HTMLInputElement & { value: DateInputValue };
19-
};
16+
export interface DateInputChangeEvent
17+
extends Omit<ChangeEvent<DateInputElement>, 'target' | 'currentTarget'> {
18+
target: DateInputElement;
19+
currentTarget: DateInputElement;
20+
}
21+
22+
interface DateInputElement extends Omit<HTMLInputElement, 'value' | 'onChange'> {
23+
value?: Partial<DateInputValue>;
24+
onChange?: EventHandler<DateInputChangeEvent>;
25+
}
2026

2127
interface DateInputProps
22-
extends Omit<HTMLProps<HTMLDivElement>, 'value' | 'defaultValue'>,
28+
extends Omit<HTMLProps<HTMLDivElement>, 'value' | 'defaultValue' | 'label' | 'onChange'>,
2329
FormElementProps {
2430
autoSelectNext?: boolean;
2531
value?: Partial<DateInputValue>;
2632
defaultValue?: Partial<DateInputValue>;
27-
onChange?: (e: DateInputChangeEvent) => void;
33+
onChange?: EventHandler<DateInputChangeEvent>;
2834
}
2935

3036
type InputType = 'day' | 'month' | 'year';
@@ -39,7 +45,7 @@ const DateInputComponent = ({
3945
}: DateInputProps) => {
4046
let monthRef: HTMLInputElement | null = null;
4147
let yearRef: HTMLInputElement | null = null;
42-
const [internalDate, setInternalDate] = useState<Record<InputType, string>>({
48+
const [internalDate, setInternalDate] = useState<DateInputValue>({
4349
day: value?.day ?? '',
4450
month: value?.month ?? '',
4551
year: value?.year ?? '',
@@ -69,15 +75,16 @@ const DateInputComponent = ({
6975
event.stopPropagation();
7076

7177
if (onChange) {
72-
const newEventValue = {
78+
const newEventValue: DateInputValue = {
7379
...internalDate,
7480
[inputType]: event.target.value,
7581
};
76-
const newEvent = {
82+
83+
const newEvent: ChangeEvent<DateInputElement> = {
7784
...event,
7885
target: { ...event.target, value: newEventValue },
7986
currentTarget: { ...event.currentTarget, value: newEventValue },
80-
} as DateInputChangeEvent;
87+
};
8188

8289
onChange(newEvent);
8390
setInternalDate(newEventValue);

0 commit comments

Comments
 (0)