hypeserver / react-date-range

A React component for choosing dates and date ranges.
MIT License
2.57k stars 658 forks source link

How to create and add a custom defined range to the list of defined range already present #507

Closed AdityaKallur closed 2 years ago

AdityaKallur commented 2 years ago

I have to add a defined range of past 12months date range to the already existing list of defined ranges

image

AdityaKallur commented 2 years ago

I got the solution for this

keyurparalkar commented 2 years ago

@AdityaKallur Can you please tell me how you achieved it? I have the same requirement

AdityaKallur commented 2 years ago

@keyurparalkar

import {defaultStaticRanges} from "react-date-range"; // this will give you already defined list import { createStaticRanges } from "react-date-range"; //pass your object to this method

let myValue= { // this is your predefined date range object label: 'My Defined Range',
range: () => ({ startDate: pass the start date, endDate: pass the end date }), };

let yourDateRange= createStaticRanges([myValue]);

<DateRangePicker className={"DateRangeWrapper"} ranges={[selectionRange]} onChange={this.handleDateRangeSelect} showSelectionPreview={true} moveRangeOnFirstSelection={false} direction="horizontal" staticRanges = {[...defaultStaticRanges, ...yourDateRange]} //combine to one array and pass it here maxDate={new Date()}

atefBB commented 2 years ago

@AdityaKallur you saved my day ! Thank you !