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

Current day highlighted across all months

Open qasimalyas opened this issue 3 years ago • 29 comments

Describe the bug Current day is selected across all months which is bad for UX. It can cause confusion making it look like two dats might have been selected. This has been raised multiple times but no real solution exists to disabled the highlighting. https://github.com/Hacker0x01/react-datepicker/issues/2376, https://github.com/Hacker0x01/react-datepicker/issues/2456

To Reproduce Steps to reproduce the behavior:

  1. Go to https://reactdatepicker.com/ where you'll see the default calendar open image
  2. Click to go to next month image
  3. You'll see day 4 being highlighted
  4. This occurs for all months followed

Expected behavior There shouldn't be any highlight dates.

Screenshots See above

Desktop (please complete the following information):

Applicable on all browsers and devices

Additional context This is confusing and disabling keyboard navigation seems to be the only option but that breaks accessibility. Ideally this shouldn't be happening or at the very least be an optional opt-in.

qasimalyas avatar May 04 '21 07:05 qasimalyas

The highlighting is necessary to know where keyboard focus is. @martijnrusschen Probably it would be a good idea to change the styling to make it more different from the selected day styling?

A workaround is to change the styling via this class: .react-datepicker__day--keyboard-selected

relwiwa avatar May 06 '21 06:05 relwiwa

I don't have anything against keyboard focus - which is absolutely necessary for usability & accessibility. But I don't think this is about that. The issue is this:

  1. todays date is 19 may 2021
  2. As a user I click to open the date picker
  3. 19th is selected by default
  4. I click next month
  5. The same date 19th is selected

I could be missing something but I don't see how this is useful. It adds to the confusion as I haven't even selected a date yet but it makes it seems so. If I change keyboard specific styling then that will change point 5 but will also affect any keyaboard navigation - which I don't want to do.

I think the solution would be to exclude highlighting dates like this from future months. If its necessary for whatever reason then make it optional.

qasimalyas avatar May 19 '21 12:05 qasimalyas

  1. The same date 19th is selected

This is not correct. The day is not "selected", because in the input field is still 19 may 2021, not 19 june 2021. There is a (probably too) subtle styling difference between the selected date and the highlighted date

relwiwa avatar May 19 '21 13:05 relwiwa

This Problem is also appearing in the range datepicker. The Range gets highlighted in every month (even on the official docs page https://reactdatepicker.com/#example-date-range).

I found a workaround that fixes the problem for me. I will just assign my own classes and disable the problem causing styles.

// value : string
// startDate?: Date
// endDate?: Date

const activeClass = (date: Date) => {
    const activeClass =
      date.getTime() === new Date(value).getTime()
        ? 'date-picker-field__active'
        : undefined;

    const startActiveClass =
      startDate && date.getTime() === startDate.getTime()
        ? 'date-picker-field__active--start'
        : undefined;

    const endActiveClass =
      endDate && date.getTime() === endDate.getTime()
        ? 'date-picker-field__active--end'
        : undefined;

    const rangeColor =
      startDate &&
      endDate &&
      date.getTime() >= startDate.getTime() &&
      date.getTime() <= endDate.getTime()
        ? 'date-picker-field__range'
        : undefined;

    // classNames from 'classnames'
    return classNames(
      rangeColor,
      startActiveClass,
      endActiveClass,
      activeClass,
    );
  };

return (
   <DatePicker
        {...someAttributes}
        dayClassName={activeClass}
    />
);
.date-picker-field__range {
  background-color: $pickerSelectionRangeColor !important;
}

.date-picker-field__active--start {
  background-color: $pickerSelectionRangeColor !important;
}

.date-picker-field__active--end {
  background-color: $pickerSelectionRangeColor !important;
}

.date-picker-field__active {
  background-color: $pickerSelectedColor !important;
}

.react-datepicker__day--in-selecting-range,
.react-datepicker__month-text--in-selecting-range,
.react-datepicker__quarter-text--in-selecting-range,
.react-datepicker__year-text--in-selecting-range {
  background-color: transparent;
  color: $pickerTextColor;
}

annika-linke avatar Jun 03 '21 13:06 annika-linke

@annika-linke your solution is great but I don't think it allows for updating the range while hovering to select a new date as in the example below.

image image

The bug occurs when selecting a range and then editing either start or end dates

image

Even though I'm not hovering and the mouse is outside the calendar element react-datepicker__day--in-selecting-range class is added to every day in other months that is included within the original interval.

image

image

alex-streza avatar Jun 04 '21 09:06 alex-streza

@alex-streza that wasnt a dealbreaker for me, but its true! For me it was a better solution than the bug. It still needs to be handled in react-datepicker though...

annika-linke avatar Jun 04 '21 09:06 annika-linke

I think a similar issue is happening for the month picker as well - the selected month is highlighted for different years Even when the date is disabled

ezgif com-gif-maker

kidroca avatar Jun 17 '21 12:06 kidroca

Related #3051

kidroca avatar Jun 17 '21 12:06 kidroca

Guys, try setting the selected value to sth, or null. Then this problem dissaper. I encounter similar probolem trying to set up datepicker for multiple day selection (not range). So from my another issue -> "I was able to do it using highlightDates option. On onChange concat or filter array of dates, set selectedDate to null, and style everything so that selected dates wont be visible. Now your highlightDates is the new value of datepicker."

mpiorowski avatar Aug 25 '21 07:08 mpiorowski

Had the same issue, had to basically remove the styles for the class @relwiwa mentioned. I don't really get why it's showing what day the keyboard would tab to, when the focus outline clearly illustrates that once it's tabbed to. Maybe I'm missing something. 🤷‍♂️

Here's the SCSS I used if anyone's interested:

.react-datepicker__day--keyboard-selected {
  background-color: inherit;
  color: inherit;

  &:hover {
    background-color: #f0f0f0;
  }
}

as-zlynn-philipps avatar Aug 27 '21 21:08 as-zlynn-philipps

This is happening in the date range example as well.

  1. Select the start date
  2. Navigate to the previous month, a couple of dates are highlighted.

I've tried disabling keyboard navigation as well but still having this issue. Any idea?

In the below screenshot, I have selected 29 Jan, 2014 but navigating to the previous month is highlighting 29 Dec, 2013 as well 😕

image

Am I missing something? This is really confusing 😭

bhavin-a avatar Oct 11 '21 07:10 bhavin-a

The bad dates (one day difference) maybe Qt5 bug again. Few days ago all Plasma widget date is wrong date highlighted,

blackPantherOS avatar Oct 30 '21 10:10 blackPantherOS

Im also having this issue, when we are switching months, the focus should stay on arrow buttons so i don't understand why its keyboard selecting same day number

snax4a avatar Jan 26 '22 17:01 snax4a

@annika-linke @Bhavin789 per-https://github.com/Hacker0x01/react-datepicker/issues/2872#issue-853667583, I was able to fix this in my range datepicker by manually passing disabledKeyboardNavigation/minDate={null}

https://user-images.githubusercontent.com/9257785/158491032-b78f959d-6956-4a80-aa56-09d685787c46.mov

first example in vid is the default minDate (i.e., not passing the prop down at all) second example is minDate={null} behavior

JohnDDuncanIII avatar Mar 15 '22 23:03 JohnDDuncanIII

I think a similar issue is happening for the month picker as well - the selected month is highlighted for different years Even when the date is disabled

ezgif com-gif-maker

Peter Velkov you got any solution for this?

mohanbalasubramani avatar Mar 25 '22 14:03 mohanbalasubramani

I think a similar issue is happening for the month picker as well - the selected month is highlighted for different years Even when the date is disabled ezgif com-gif-maker

Peter Velkov you got any solution for this?

Having the same issue too It would be great if I could disable the highlighted month

omer-alon avatar Apr 20 '22 13:04 omer-alon

I think a similar issue is happening for the month picker as well - the selected month is highlighted for different years Even when the date is disabled

Peter Velkov you got any solution for this?

Having the same issue too It would be great if I could disable the highlighted month

@mohanbalasubramani, @omer-alon No we just live with it the way it is, for our use case it rarely happens and no one notices but QA

kidroca avatar Apr 20 '22 16:04 kidroca

I think a similar issue is happening for the month picker as well - the selected month is highlighted for different years Even when the date is disabled ezgif com-gif-maker

Peter Velkov you got any solution for this?

I used this workaround in CSS

.react-datepicker__day--keyboard-selected,
.react-datepicker__month-text--keyboard-selected,
.react-datepicker__quarter-text--keyboard-selected,
.react-datepicker__year-text--keyboard-selected {
  border-radius: inherit;
  background-color: inherit;
  color: inherit;
}
.react-datepicker__month--selected,
.react-datepicker__month--in-selecting-range,
.react-datepicker__month--in-range,
.react-datepicker__quarter--selected,
.react-datepicker__quarter--in-selecting-range,
.react-datepicker__quarter--in-range {
  border-radius: 0.3rem;
  background-color: #216ba5;
  color: #fff;
}

Seijinx avatar Apr 20 '22 16:04 Seijinx

Just integrated react-datepicker into a new project and stumbled upon this bug. Are there any news when this will get resolved? I think from an UI perspective it's critical!! Thanks in advance!!

knoefel avatar Jun 27 '22 11:06 knoefel

I want to remove just the default highlights whenever the user hasn't selected a date yet. Adding the following isn't working for me since it also removes the highlight when the user actually chose a date. Any workaround?

.react-datepicker__day.react-datepicker__day--keyboard-selected {
    background-color: inherit;
    color: inherit;
    font-weight: inherit;
}

theresa-de-ocampo avatar Jul 05 '22 08:07 theresa-de-ocampo

This is happening because of the &--keyboard-selected style and this part of the code getclassnames in day.jsx

I made some css changes that fix this issue. If anyone wants I can make a pull request for it.

luddensdesir avatar Sep 26 '22 06:09 luddensdesir

@luddens please!

ghost avatar Sep 28 '22 15:09 ghost

Alright, I made a PR here:

https://github.com/Hacker0x01/react-datepicker/pull/3782

modified colors

luddensdesir avatar Sep 29 '22 21:09 luddensdesir

The issue can be resolved by using the 'disabledKeyboardNavigation' option.

seong-ji-sue avatar Sep 20 '23 02:09 seong-ji-sue

The issue can be resolved by using the 'disabledKeyboardNavigation' option.

Thanks.

date Issue has resolved. but in time if i navigate to other month / day, time is auto selected as same as selected time.

example : selected date and time - 23-09-2023 21:30

when i navigate to January 2024 time auto highlighted as 21:30 (as Selected date time)

image

rajesh2020in avatar Sep 23 '23 13:09 rajesh2020in

To limit the highlight mechanism only for the selected date (or other specific dates) you can use Highlight dates with custom class names and ranges combining with the onMonthChange prop.

const [displayedMonth, setDisplayedMonth] = useState<null | number>(null);

const highlightedDates = useMemo(() => {
    const highlights = [];
    // if selected date is in the same month, as the displayed month, or no display month
    if (selected && (selected.getMonth() === displayedMonth || displayedMonth === null)) {
      highlights.push(new Date(rest.selected));
    }
    
   // optional: push specific dates other than the selected date here  (for example `openToDate`)
 
    if (highlights.length) {
      return [
        {
          'react-datepicker__day--highlighted-custom': highlights
        }
      ];
    }

    return null;
  }, [selected, displayedMonth]);

Use the props like this:

 <DateInput
     {...yourProps}
     {...(highlightedDates && { highlightDates: highlightedDates })} 
     onMonthChange={(date) => setDisplayedMonth(date.getMonth())}
   />

Don't forget to apply css properties for the highlighted-custom class:

.react-datepicker__day--highlighted-custom {
    color: white;
    background-color: orange;
}

tfazekas avatar Dec 18 '23 12:12 tfazekas

In case of DateRangePicker, I passed minDate={null} into my endDate datePicker and it works, based on @JohnDDuncanIII. Didn't add disabledKeyboardNavigation

paduck1408 avatar Jan 11 '24 18:01 paduck1408