react-day-picker icon indicating copy to clipboard operation
react-day-picker copied to clipboard

Bug: Using numberOfMonths >= 2, the dropdowns that are past the toDate should be disabled.

Open ZakisM opened this issue 2 years ago • 5 comments
trafficstars

Bug description

The dropdowns that are out of range should be disabled if they are after toDate.

To reproduce

https://codesandbox.io/s/react-daypicker-forked-ymyrvh

Steps

  1. On the first calendar, set the month to December and the Year to 2025.

Expected behavior

The dropdown for the month January and dropdown for the year 2026 should be disabled, as this is out of range. The arrow is disabled correctly in this case, but the dropdowns are not.

Screenshots

Screenshot 2023-06-20 at 11 18 38

Additional context

It's possible to fix this currently but it requires duplicating a lof the components and creating a custom Caption component. If we had access to the displayMonth prop inside of the Dropdown component it would make it possible to disable this, which would also be useful:

For example a custom Dropdown Component with our own data-disabled prop.

const DropdownCustom = (props: DropdownCustomPropsType): ReactElement => {
    const { onChange, value, children, caption, className, style } = props;
    const dayPicker = useDayPicker();

    const IconDropdownComponent = dayPicker.components?.IconDropdown ?? IconDropdown;

    return (
        <div className={className} style={style} data-disabled={isAfter(displayMonth, dayPicker.toDate!)}>
            <span className={dayPicker.classNames.vhidden}>{props["aria-label"]}</span>
            <select
                name={props.name}
                aria-label={props["aria-label"]}
                className={dayPicker.classNames.dropdown}
                style={dayPicker.styles.dropdown}
                value={value}
                onChange={onChange}
                disabled={disabled === true ? true : undefined}>
                {children}
            </select>
            <div
                className={dayPicker.classNames.caption_label}
                style={dayPicker.styles.caption_label}
                aria-hidden="true">
                {caption}
                {
                    <IconDropdownComponent
                        className={dayPicker.classNames.dropdown_icon}
                        style={dayPicker.styles.dropdown_icon}
                    />
                }
            </div>
        </div>
    );
};`

ZakisM avatar Jun 20 '23 10:06 ZakisM

Hey @ZakisM thanks for your report, definitely there's space for improvements here. Will check it out better later.

If we had access to the displayMonth prop inside of the Dropdown component it would make it possible to disable this, which would also be useful:

There's a way to know the displayMonths: the useNavigation hook returns that information and it is accessible by a custom component:

https://codesandbox.io/s/react-daypicker-forked-62vjs7?file=/src/App.tsx:213-314

import { useNavigation } from "react-day-picker";

function CustomDropdown(props: DropdownProps) {
  const navigation = useNavigation();
  console.log("Displayed months: ", navigation.displayMonths);
  return <Dropdown {...props} />;
}

gpbl avatar Jun 20 '23 13:06 gpbl

Hey, I think I tried this, however when you have multiple months an array is returned. How would I then know which index I need to use for the CustomDropdown?

ZakisM avatar Jun 21 '23 09:06 ZakisM

Hey, I think I tried this, however when you have multiple months an array is returned. How would I then know which index I need to use for the CustomDropdown?

Ah I think this is the reason I chose to not disable these dropdowns. I think this issue doesn't really affect the usability of DayPicker. What's the actual problem when the dropdown are not disabled?

gpbl avatar Jun 21 '23 14:06 gpbl

Ah I see, the issue is that when the dropdowns are not disabled, if I pick one of the months from the dropdown, the date picker is now showing dates that are outside of the fromDate/toDate I set:

https://github.com/gpbl/react-day-picker/assets/8143258/189d2e0a-b3ca-4c1a-879f-21f82a1699a9

ZakisM avatar Jun 27 '23 10:06 ZakisM

With https://github.com/gpbl/react-day-picker/issues/1884 we are adding the displayIndex to the caption components. This could help fixing this issue too 🤔 .

gpbl avatar Aug 16 '23 11:08 gpbl

Ah I see, the issue is that when the dropdowns are not disabled, if I pick one of the months from the dropdown, the date picker is now showing dates that are outside of the fromDate/toDate I set:

recording.mov

I do not use the "disabled" prop as @ZakisM does, so when I just use the fromDate and toDate prop and I select a month which is out of range, the calendar does not show at all. Therefor I think that months that are out of range, should be disabled in the dropdown.

dmvdven avatar Jun 05 '24 11:06 dmvdven

Good news: this is being fixed with #2194 (next beta release)

Screenshot 2024-06-05 at 7 27 10 PM

gpbl avatar Jun 06 '24 00:06 gpbl