From 6520b7992228ca9ab9c4c378ae86fdfb019bf6f7 Mon Sep 17 00:00:00 2001 From: mohammadazimi Date: Sun, 14 Dec 2025 23:06:16 +0330 Subject: [PATCH 1/2] feat: added goToDate method currently not supporting scrollable --- CalendarPicker/index.js | 40 ++++++++++++++++++++++++++++++++++++++++ example/example/App.js | 14 ++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/CalendarPicker/index.js b/CalendarPicker/index.js index 6d1887e..36e3873 100644 --- a/CalendarPicker/index.js +++ b/CalendarPicker/index.js @@ -237,6 +237,46 @@ export default class CalendarPicker extends Component { return { minRangeDuration, maxRangeDuration }; } + goToDate = (date = new Date(), options = {}) => { + + if (this.props.scrollable) { + console.error('goToDate() is not supported when scrollable is true'); + return; + } + + const defaultOptions = { + /* + * isSelected - if true, the date will be shown as selected. + * useful when you have different style for today and selected date. + */ + isSelected: true, + ...options, + } + + const cloneDate = new Date(date); + const fullYear = cloneDate.getFullYear(); + const fullMonth = cloneDate.getMonth(); + const fullDay = cloneDate.getDate(); + + if (!Utils.isSameMonthAndYear(cloneDate, this.state.selectedStartDate)) { + + if (defaultOptions.isSelected) { + this.handleOnPressDay({ year: fullYear, month: fullMonth, day: fullDay, setAsCurrentDate: true }); + const currentMonthYear = new Date(fullYear, fullMonth, 1, 12); + this.props.onMonthChange && this.props.onMonthChange(currentMonthYear); + } else { + this.handleOnPressFinisher({ year: fullYear, month: fullMonth, scrollFinisher: null, extraState: null }); + } + } else { + // the date is in the same month and year as the selected date + + if (defaultOptions.isSelected) { + this.handleOnPressDay({ year: fullYear, month: fullMonth, day: fullDay }); + + } + } + } + handleOnPressDay = ({ year, month, day }) => { const { selectedStartDate: prevSelectedStartDate, diff --git a/example/example/App.js b/example/example/App.js index b2f2173..496a633 100644 --- a/example/example/App.js +++ b/example/example/App.js @@ -46,6 +46,9 @@ export default class App extends Component { this.toggleEnableRange = this.toggleEnableRange.bind(this); this.onMinRangeDuration = this.onMinRangeDuration.bind(this); this.onMaxRangeDuration = this.onMaxRangeDuration.bind(this); + this.goToToday = this.goToToday.bind(this); + + this.calendarPickerRef = React.createRef(); } onDateChange(date, type) { @@ -110,6 +113,12 @@ export default class App extends Component { } } + goToToday() { + this.calendarPickerRef.current.goToDate(new Date(), { + isSelected: false, + }); + } + render() { const { customDatesStyles, @@ -127,6 +136,7 @@ export default class App extends Component { return ( + +