You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{isSameDay,isWithinInterval}from'date-fns';importReact,{useState,useCallback}from'react';import{Box}from'../../box';importDateCalendarfrom'../DateCalendar';constDateRangeCalendar=({
value,// Controlled value
defaultValue =[null,null],// Uncontrolled initial value
onChange,// Callback for value change
...rest})=>{const[internalValue,setInternalValue]=useState(defaultValue);const[hoveredDate,setHoveredDate]=useState(null);constisControlled=value!==undefined;const[startDate,endDate]=isControlled ? value : internalValue;consthandleDateChange=useCallback((date)=>{letnewValue;if(!startDate||(startDate&&endDate)){// Reset selection or start a new rangenewValue=[date,null];}else{// Set the end dateif(date>=startDate){newValue=[startDate,date];}else{newValue=[date,startDate];}}if(!isControlled){setInternalValue(newValue);}onChange?.(newValue);},[startDate,endDate,onChange,isControlled]);constisDateInRange=useCallback((date)=>{if(startDate&&endDate){returnisWithinInterval(date,{start: startDate,end: endDate});}if(startDate&&hoveredDate){conststart=startDate<hoveredDate ? startDate : hoveredDate;constend=startDate>hoveredDate ? startDate : hoveredDate;returnisWithinInterval(date,{ start, end });}returnfalse;},[startDate,endDate,hoveredDate]);constrenderDay=useCallback((day)=>{constisSelectedStart=startDate&&isSameDay(day,startDate);constisSelectedEnd=endDate&&isSameDay(day,endDate);constisInRange=isDateInRange(day);conststyle={backgroundColor: isSelectedStart||isSelectedEnd ? 'blue' : isInRange ? 'lightblue' : 'transparent',color: isSelectedStart||isSelectedEnd ? 'white' : 'black',borderRadius: isSelectedStart ? '50%' : isSelectedEnd ? '50%' : '0%',};return(<divstyle={style}>{day.getDate()}</div>);},[startDate,endDate,isDateInRange]);return(<Boxdisplay="flex"gap="16px"{...rest}><DateCalendarvalue={startDate}onChange={handleDateChange}onDayHover={setHoveredDate}//renderDay={renderDay}/><DateCalendarvalue={endDate}onChange={handleDateChange}onDayHover={setHoveredDate}//renderDay={renderDay}/></Box>);};exportdefaultDateRangeCalendar;
The text was updated successfully, but these errors were encountered:
cheton
changed the title
New enhancements for Calendar and DatePicker
Add localization and range selection to Calendar and DatePickerApr 27, 2022
cheton
changed the title
Add localization and range selection to Calendar and DatePicker
Support localization and range selection for Calendar and DatePicker components
Apr 27, 2022
cheton
changed the title
Support localization and range selection for Calendar and DatePicker components
Support localization and DateRangePickerJan 14, 2025
https://mui.com/x/react-date-pickers/date-picker/
The text was updated successfully, but these errors were encountered: