question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RangePicker: choose which months are displayed on calendars

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

We develop an application that is used for financial bookkeeping and use RangePickers to filter past operations. Our use case focuses on dates in the past, and since filtering for the future makes no sense, we disable these dates using disabledDate property of RangePicker. A simplified example of the disabling logic:

disabledDate={currentDate => currentDate.isAfter(moment())}

By default, when RangePicker has value set to a single day or the date range fits on the same month, the value is displayed on the left calendar and right calendar displays the following month – this setting is controlled by showDate variable in component’s state. But since we disable future dates, the following month is always disabled in our pickers:

visualisation of default behaviour

It would make much more sense in out case to be able to decide which month is displayed by default on which side, so that the user by default sees this:

visualisation of desired behaviour with value on the right pane

We believe the use case is universal enough to make into the library – it can be used to guide the user to pick the date range in the very specific month and be complemented by a advanced date disabling logic. Other solutions like predefined date ranges do not solve it for us and we do not want to maintain internal fork of a entire library for a simple change like this.

What does the proposed API look like?

We tried different approaches and the simplest and most obvious seems to be the most effective: allow replacing the logic of the getShowDateFromValue function with function from component props.

Suggested changes:

replace every occurrence of getShowDateFromValue(value) || showDate and derivatives inside the class component (like here and here) with method call:

class RangePicker extends React.Component<any, RangePickerState> {
  // ...
  getShowDate = (value: RangePickerValue, previousValue: RangePickerValue) => {
    let showDate = null;
    if ('getCustomShowDate' in this.props) {
      showDate = this.props.getCustomShowDate(value);
    } else {
      showDate = getShowDateFromValue(value) || previousValue;
    }
    return showDate;
  }
  // ...
}

So I propose to add a property

getCustomShowDate(value: RangePickerValue) => RangePickerValue

to DatePicker.RangePicker component.

If you approve of the suggestion, I can send a pull request with implementation.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
Bosseskompiscommented, Mar 10, 2022

I would also like this feature. As others have pointed out, using defaultPickerValue only works for the initial rendering, but we need a way to update it based on selected dates.

1reaction
someone635commented, Oct 10, 2022

Faced this issue, found a quick and easy hack to sidestep it temporarily:

const [key, setKey] = useState("1");

useEffect(
    ()=>{
        setKey(key === "1" ? "2" : "1")
    },
    [someDate.getTime(), someOtherDate.getTime()]
)

)
<RangePicker
                key={key}
                defaultPickerValue={[someDate, someOtherDate]}
                ...
            />

Now every time any of the defaultPickerValues change the react key is changed, remounting the component from scratch. Meaning defaultPickerValue will be used every time it remounts, acting somewhat like a controlled component. Not optimal perfornance-wise for sure, but gets the job done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Date Range Picker — JavaScript Date & Time Picker Library
showDropdowns : (true/false) Show year and month select boxes above calendars to jump to a specific month and year. minYear : (number) The...
Read more >
Need to show only one month in range picker selection area ...
I'm using antd calendar RangePicker for my react app.i want to show only one month of calendar dropdown upon selection, instead of two ......
Read more >
DatePicker - Ant Design
To select or input a date. ... By clicking the input box, you can select a date from a popup calendar. ... Set...
Read more >
React Datepicker crafted by HackerOne
Calendar open state callbacks ... Custom header with two months displayed ... subDays(new Date(), 1)]} placeholderText="Select a date other than today or ...
Read more >
Date Range Picker — JavaScript Date & Time Picker ... - Bangor
showDropdowns : (true/false) Show year and month select boxes above calendars to jump to a specific month and year. minYear : (number) The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found