Skip to content
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

fix(DatePicker): add override behavior for start date deletion bug #13443

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions packages/react/src/components/DatePicker/DatePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,39 @@ const DatePicker = React.forwardRef(function DatePicker(
}
}, []);
const endInputField = useRef(null);

const lastStartValue = useRef('');

// fix datepicker deleting the selectedDate when the calendar closes
const onCalendarClose = (selectedDates, dateStr) => {
setTimeout(() => {
if (
lastStartValue.current &&
selectedDates[0] &&
!startInputField.current.value
) {
startInputField.current.value = lastStartValue.current;
calendarRef.current.setDate(
[startInputField.current.value, endInputField.current.value],
true,
calendarRef.current.config.dateFormat
);
}
if (onClose) {
onClose(
calendarRef.current.selectedDates,
dateStr,
calendarRef.current
);
}
});
};

const calendarRef = useRef(null);
const savedOnChange = useSavedCallback(onChange);
const savedOnClose = useSavedCallback(onClose);
const savedOnClose = useSavedCallback(
datePickerType === 'range' ? onCalendarClose : onClose
);
const savedOnOpen = useSavedCallback(onOpen);

const datePickerClasses = cx(`${prefix}--date-picker`, {
Expand Down Expand Up @@ -350,6 +380,7 @@ const DatePicker = React.forwardRef(function DatePicker(
carbonFlatpickrFixEventsPlugin({
inputFrom: startInputField.current,
inputTo: endInputField.current,
lastStartValue,
}),
],
clickOpens: !readOnly,
Expand Down Expand Up @@ -398,11 +429,16 @@ const DatePicker = React.forwardRef(function DatePicker(
}
}

function handleOnChange() {
function handleOnChange(event) {
if (datePickerType == 'single') {
calendar.calendarContainer.classList.remove('open');
}

const { target } = event;
if (target === start) {
lastStartValue.current = start.value;
}

if (start.value !== '') {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default (config) => (fp) => {
* set the date again, triggering the calendar to update.
*/
const handleBlur = (event) => {
const { inputFrom, inputTo } = config;
const { inputFrom, inputTo, lastStartValue } = config;
const { target } = event;

// Only fall into this logic if the event is on the `to` input and there is a
Expand Down Expand Up @@ -91,6 +91,22 @@ export default (config) => (fp) => {
);
}
}

// overriding the flatpickr bug where the startDate gets deleted on blur
if (inputTo === target && !inputFrom.value && lastStartValue.current) {
let currentStartDate = new Date(lastStartValue.current);

if (currentStartDate.toString() !== 'Invalid Date') {
inputFrom.value = lastStartValue.current;
if (inputTo.value) {
fp.setDate(
[inputFrom.value, inputTo.value],
true,
fp.config.dateFormat
);
}
}
}
};

/**
Expand Down