-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The date
parameter in onChange
from DatePickerProps
is differently typed in v7.0.0
#4924
Comments
I think there is no bug or problem. It is not The type depends on It is (
| {
selectsRange?: never;
selectsMultiple?: never;
onChange: (
date: Date | null,
event?:
| React.MouseEvent<HTMLElement>
| React.KeyboardEvent<HTMLElement>,
) => void;
}
| {
selectsRange: true;
selectsMultiple?: never;
onChange: (
date: [Date | null, Date | null],
event?:
| React.MouseEvent<HTMLElement>
| React.KeyboardEvent<HTMLElement>,
) => void;
}
| {
selectsRange?: never;
selectsMultiple: true;
onChange: (
date: Date[] | null,
event?:
| React.MouseEvent<HTMLElement>
| React.KeyboardEvent<HTMLElement>,
) => void;
}
); |
@talatkuyuk That was my expectation as well. However, you can see in the sandbox link I provided, |
@arendora, don't pass directly type Props = Omit<DatePickerProps, "onChange"> & {
label: string;
onChange: (date: Date | null) => void;
};
export const DatePickerComponent = ({
label,
onChange,
...datePickerProps
}: Props) => {
const onChangeHandler = (
date: Date | null,
event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>
) => {
onChange(date);
};
return (
<label>
<span>{label}</span>
<DatePicker {...datePickerProps} onChange={onChangeHandler} />
</label>
);
}; |
I appreciate the suggestion @talatkuyuk and it is a temporary workaround, however that just overrides the issue and I think the underlying type change should still be addressed. |
Thank you @arendora, but, I don't think it is a temporary workaround, it should be as I explained. You pass a If you want to pass strictly an appropriate event handler as a prop of export default function App() {
const [startDate, setStartDate] = useState<Date | null>(new Date());
const onChange = (
date: Date | null,
event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLElement>
) => {
setStartDate(date);
};
return (
<div className="App">
<DatePickerComponent
label="Date"
onChange={onChange}
// ...
/>
</div>
);
} In that case, in the component you don't need to destruct type Props = DatePickerProps & {
label: string;
};
export const DatePickerComponent = ({
label,
...datePickerProps
}: Props) => {
return (
<label>
<span>{label}</span>
<DatePicker {...datePickerProps} />
</label>
);
}; That is my conclusion. |
@arendora The issue you're facing is because TypeScript doesn't know for certain the type should be <DatePickerComponent
selectsRange={true}
onChange={(d) => {
/* d: [Date | null, Date | null] */
}}
// …
/> If you'd like to constrain your custom component to only allow single dates you'll need to constrain your props type. One way of doing this is: type Props = DatePickerProps & {
label: string;
} & {
selectsRange?: never;
selectsMultiple?: never;
}; With this change |
Describe the bug
The
date
parameter inonChange
when taken fromDatePickerProps
is typeDate & [Date | null, Date | null] & Date[]
.Before v7.0.0 , the
date
parameter inonChange
fromReactDatePickerProps
is typed depending onWithRange
andWithMultiple
so that typesDate | null
,Date[] | null
or[Date | null, Date | null]
were accepted.However, in v7.0.0 the
date
parameter is now typeDate & [Date | null, Date | null] & Date[]
.To Reproduce
Sandbox link
Expected behavior
onChange
fromDatePickerProps
accepts parameterdate
of typeDate
.Screenshots
The text was updated successfully, but these errors were encountered: