react-datepicker icon indicating copy to clipboard operation
react-datepicker copied to clipboard

Date Range using input does not update date values on input update.

Open w-gluza opened this issue 2 years ago • 6 comments

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

  1. Create date range input using:
() => {
  const [dateRange, setDateRange] = useState([null, null]);
  const [startDate, endDate] = dateRange;
  return (
    <DatePicker
      selectsRange={true}
      startDate={startDate}
      endDate={endDate}
      onChange={(update) => {
        setDateRange(update);
      }}
      isClearable={true}
    />
  );
};
  1. Set up a date range either by input or calendar GUI.
  2. Try to change the date using the input field. The date does not update.

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):

  • OS: [iOS]
  • Browser [chrome]
  • Version [109]

w-gluza avatar Jan 25 '23 12:01 w-gluza

Any updates on this issue? It seems that the current "solution" for this is to revert to v3.

galmeida12 avatar Mar 29 '23 09:03 galmeida12

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.

oatmealproblem avatar Nov 09 '23 22:11 oatmealproblem

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}
                        />

jeffmang avatar Nov 10 '23 23:11 jeffmang

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.

github-actions[bot] avatar Jun 05 '24 01:06 github-actions[bot]

Any updates? 👀

ArturoDucasse avatar Jun 20 '24 16:06 ArturoDucasse

See also #3596 and #4002 which report the same issue.

laug avatar Aug 15 '24 09:08 laug