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.

Handling multiple date-ranges in a single parent

See original GitHub issue

I’m using react-dates in an advanced search component that incorporates multiple date-range fields.

I’m trying to figure out the best way to detect which of the date-range pickers is being changed and handle the event accordingly in the onFocusChange and onDateChange callbacks without writing a separate handler with superficial differences to target the appropriate attribute for each instance of the component.

Is there a way to access the event target within the defined callbacks?

I’m imagining something along the lines of:

onFocusChange(event, focusedInput){
    var newState = object.assign({}, this.state);
    newState[event.target.id].focusedInput = focusedInput;
    this.setState(newState);
}

But maybe it would be a better approach to store another variable on the state which stores the focused date-range picker?

Any insight would be greatly appreciated!

Update: I’ve written a closure to return a case-specific handler for each field which is adequate to my use case, but I am still curious if it’s possible to access the source event.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

2reactions
munkhjin0223commented, May 9, 2017

I am using multiple DateRangePicker that dynamically added in single page. There is another solution.

constructor(props) {
    super(props);
    this.onDatesChange = this.onDatesChange.bind(this);
    this.onFocusChange = this.onFocusChange.bind(this);
    this.state = {
      startDate: {},
      endDate: {},
      focusedInput: {},
    };
  }
// id is unique value for each instance and you can define it
onDatesChange(id, { startDate, endDate }) {
    const start = this.state.startDate;
    const end = this.state.endDate;
    start[id] = startDate;
    end[id] = endDate;
    this.setState({
      startDate: start,
      endDate: end,
    });
  }

  onFocusChange(id, value) {
    const focusedInput = this.state.focusedInput;
    focusedInput[id] = value;
    this.setState({
      focusedInput,
    });
  }
<DateRangePicker
                    startDate={this.state.startDate[id]}
                    endDate={this.state.endDate[id]}
                    onDatesChange={({ startDate, endDate }) => this.onDatesChange(id, { startDate, endDate })}
                    focusedInput={this.state.focusedInput[id]}
                    onFocusChange={(focusedInput) => this.onFocusChange(id, focusedInput)}
                  />
1reaction
andosteinmetzcommented, Feb 23, 2017

Hi @tyleralves , My solution was to write a closure that returns a function specific to the active react dates instance.

In my case there was one instance to set a range for when a record was created, and another range for the date the record was most recently modified: called ‘created’ and ‘changed’.

Here are my two handler functions for focus and date changes respectively:

function handleFocusChange(attr){
    return function(focusedInput){
        var dates = this.state[attr];
        dates.focusedInput = focusedInput;
        let newState = {};
        newState[attr] = dates;
        this.setState(newState);
    }
}

function handleDatesChange(attr){
    return function({startDate, endDate}){
        var dates = this.state[attr];
        dates.startDate = startDate;
        dates.endDate = endDate;
        startDate ? dates.gte = startDate.format('YYYY[-]MM[-]DD') : delete dates.gte;
        endDate ? dates.lte = endDate.format('YYYY[-]MM[-]DD') : delete dates.lte;
        let newState = {};
        newState[attr] = dates;
        this.setState(newState);
    }
}

and here’s how I used it in my component:

class MyComponent extends React.Component
{
    constructor(props){
        super(props);
        this._onCreatedFocusChange = handleFocusChange('created').bind(this);
        this._onCreatedDatesChange = handleDatesChange('created').bind(this);
        this._onUpdatedFocusChange = handleFocusChange('changed').bind(this);
        this._onUpdatedDatesChange = handleDatesChange('changed').bind(this);
    }

and then in the ReactDates instances:

render(){
        const createdRange = (
            <div className="date-range">
                <DateRangePickerField
                    isOutsideRange = {() => false }
                    showClearDates = {true}
                    label = 'Created:'
                    focusedInput = {this.state.created.focusedInput}
                    startDate = {this.state.created.startDate}
                    endDate = {this.state.created.endDate}
                    onDatesChange = {this._onCreatedDatesChange}
                    onFocusChange = {this._onCreatedFocusChange}
                />
            </div>
        );

        const changedRange = (
            <div className="date-range">
                <DateRangePickerField 
                    isOutsideRange = {() => false}
                    showClearDates = {true}
                    label = 'Updated:'
                    focusedInput = {this.state.changed.focusedInput}
                    startDate = {this.state.changed.startDate}
                    endDate = {this.state.changed.endDate}
                    onDatesChange = {this._onUpdatedDatesChange}
                    onFocusChange = {this._onUpdatedFocusChange}
                />
            </div>
        );

Hope this is helpful in your case as well!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handling multiple date-ranges in a single parent #144 - GitHub
I'm using react-dates in an advanced search component that incorporates multiple date-range fields. I'm trying to figure out the best way to ...
Read more >
Formula with Multiple Date Ranges - Microsoft Community Hub
I need to figure out a formula for when a date within a certain range is entered in one cell, a certain character...
Read more >
Multiple date range slicers - Microsoft Power BI Community
Hi guys! Need your assistance on my project. Currently, we're migrating from one visualization to Power BI. The visualization has below filter date...
Read more >
Split multiple date ranges from cells (English VO) - YouTube
For Cantonese VO, please visit: https://youtu.be/IvclTra1CgAIn this video, you will see how Power Query can do something not so possible ...
Read more >
Javascript Calendar Multiple date selection Example | Mobiscroll
Dynamically switching between single, multiple or range select can be done with option changes. Customize the selection. Select a maximum of
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