Open w-gluza opened 1 year ago
Any updates on this issue? It seems that the current "solution" for this is to revert to v3.
For others looking for an interim solution, you can use onChangeRaw
prop with a function like the following:
function onChangeRaw(raw: null | undefined | string) {
if (selectsRange) {
if (raw != null) {
let rawStart = raw;
let rawEnd = '';
if (raw.includes(' - ')) {
rawStart = raw.split(' - ')[0];
rawEnd = raw.split(' - ')[1];
}
const referenceDate = new Date(new Date().getFullYear(), 0, 1);
let startDate: null | Date = null;
try {
startDate = parse(rawStart, standardizedDateFormat, referenceDate);
} catch (e) {
console.error('Failed to parse start date', e);
}
let endDate: null | Date = null;
try {
endDate = parse(rawEnd, standardizedDateFormat, referenceDate);
} catch (e) {
console.error('Failed to parse end date', e);
}
onChange([
startDate && isValid(startDate) ? startDate : null,
endDate && isValid(endDate) ? endDate : null,
]);
}
}
}
Note: parse
and isValid
are imported from date-fns
.
Maybe not the most robust solutions; but seems to be working fine for me.
MichaelMakesGames, yes I tried what you did and it works for me. I thought I would provide a little more code context to help others:
import { parse, isValid } from "date-fns";
const handleIncidentDateChange = (dateRange: DateRange) => {
const [startDate, endDate] = dateRange;
const updatedClaim = {
...claim,
incidentDateBegin: startDate,
incidentDateEnd: endDate
};
setClaim(updatedClaim);
};
const standardizedDateFormat = "MM/dd/yyyy";
const handleIncidentDateChangeRaw = (event: React.FocusEvent<HTMLInputElement>) => {
const raw:string = event.target.value;
if (raw != null) {
if (typeof raw === "string" && raw.length >= 13) {
let rawStart = raw;
let rawEnd = '';
if (raw.includes(' - ')) {
rawStart = raw.split(' - ')[0];
rawEnd = raw.split(' - ')[1];
}
const referenceDate = new Date(new Date().getFullYear(), 0, 1);
let startDate: null | Date = null;
try {
startDate = parse(rawStart, standardizedDateFormat, referenceDate);
} catch (e) {
console.error('Failed to parse start date', e);
}
let endDate: null | Date = null;
try {
endDate = parse(rawEnd, standardizedDateFormat, referenceDate);
} catch (e) {
console.error('Failed to parse end date', e);
}
handleIncidentDateChange([
startDate && isValid(startDate) ? startDate : null,
endDate && isValid(endDate) ? endDate : null,
]);
}
}
}
<DatePicker
selectsRange={true}
startDate={claim?.incidentDateBegin}
endDate={claim?.incidentDateEnd}
onChange={(update) => {
handleIncidentDateChange(update);
}}
isClearable={true}
dateFormat="MM/dd/yyyy"
className="mb-1 custom-datepicker"
onChangeRaw={handleIncidentDateChangeRaw}
/>
This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 10 days.
Any updates? 👀
See also #3596 and #4002 which report the same issue.
Describe the bug Changing the range value inside the input does not update the value in the calendar. Please note input works when you set up a range for the first time, when updating the date, issues arise.
To Reproduce
PS You can also see this bug in official docs under the: Date Range using input with clear button
Expected behavior Dates updates either if they are updated through calendar GUI or by typing inside the input field.
Desktop (please complete the following information):