Skip to content

Commit 952ccb0

Browse files
Add goToDate() method available via ref to go to any date including today (#409)
1 parent 6397d13 commit 952ccb0

3 files changed

Lines changed: 58 additions & 3 deletions

File tree

CalendarPicker/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,46 @@ export default class CalendarPicker extends Component {
237237
return { minRangeDuration, maxRangeDuration };
238238
}
239239

240+
goToDate = (date = new Date(), options = {}) => {
241+
242+
if (this.props.scrollable) {
243+
console.error('goToDate() is not supported when scrollable is true');
244+
return;
245+
}
246+
247+
const defaultOptions = {
248+
/*
249+
* isSelected - if true, the date will be shown as selected.
250+
* useful when you have different style for today and selected date.
251+
*/
252+
isSelected: true,
253+
...options,
254+
}
255+
256+
const cloneDate = new Date(date);
257+
const fullYear = cloneDate.getFullYear();
258+
const fullMonth = cloneDate.getMonth();
259+
const fullDay = cloneDate.getDate();
260+
261+
if (!Utils.isSameMonthAndYear(cloneDate, this.state.selectedStartDate)) {
262+
263+
if (defaultOptions.isSelected) {
264+
this.handleOnPressDay({ year: fullYear, month: fullMonth, day: fullDay, setAsCurrentDate: true });
265+
const currentMonthYear = new Date(fullYear, fullMonth, 1, 12);
266+
this.props.onMonthChange && this.props.onMonthChange(currentMonthYear);
267+
} else {
268+
this.handleOnPressFinisher({ year: fullYear, month: fullMonth, scrollFinisher: null, extraState: null });
269+
}
270+
} else {
271+
// the date is in the same month and year as the selected date
272+
273+
if (defaultOptions.isSelected) {
274+
this.handleOnPressDay({ year: fullYear, month: fullMonth, day: fullDay });
275+
276+
}
277+
}
278+
}
279+
240280
handleOnPressDay = ({ year, month, day }) => {
241281
const {
242282
selectedStartDate: prevSelectedStartDate,

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,9 +405,10 @@ These internal methods may be accessed through a ref to the CalendarPicker.
405405
| Name | Params | Description |
406406
| :-------------------------- | :------------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
407407
| **`handleOnPressDay`** | `{year, month, day} (Integers)` | Programmatically select date. `year`, `month` and `day` are numbers. `day` is the day of the current month. date-fns example for today's day of month: `getDate(new Date())` |
408-
| **`handleOnPressNext`** | | Programmatically advance to next month. |
409-
| **`handleOnPressPrevious`** | | Programmatically advance to previous month. |
410-
| **`resetSelections`** | | Clear date selections. Useful for resetting date range selection when user has picked a start date but not an end date. |
408+
| **`handleOnPressNext`** | | Programmatically advance to next month. |
409+
| **`handleOnPressPrevious`** | | Programmatically advance to previous month. |
410+
| **`resetSelections`** | | Clear date selections. Useful for resetting date range selection when user has picked a start date but not an end date. |
411+
| **`goToDate`** | `date, {isSelected}` | Goes to the passed `date` and is type of Date. `isSelected` is optional boolean and marks the passed date as selected. Default values are like `goToDate(new Date(), {isSelected: true})` |
411412

412413
## TypeScript
413414

example/example/App.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ export default class App extends Component {
4646
this.toggleEnableRange = this.toggleEnableRange.bind(this);
4747
this.onMinRangeDuration = this.onMinRangeDuration.bind(this);
4848
this.onMaxRangeDuration = this.onMaxRangeDuration.bind(this);
49+
this.goToToday = this.goToToday.bind(this);
50+
51+
this.calendarPickerRef = React.createRef();
4952
}
5053

5154
onDateChange(date, type) {
@@ -110,6 +113,12 @@ export default class App extends Component {
110113
}
111114
}
112115

116+
goToToday() {
117+
this.calendarPickerRef.current.goToDate(new Date(), {
118+
isSelected: false,
119+
});
120+
}
121+
113122
render() {
114123
const {
115124
customDatesStyles,
@@ -127,6 +136,7 @@ export default class App extends Component {
127136
return (
128137
<View style={styles.container}>
129138
<CalendarPicker
139+
ref={this.calendarPickerRef}
130140
scrollable
131141
selectedStartDate={selectedStartDate}
132142
selectedEndDate={selectedEndDate}
@@ -143,6 +153,10 @@ export default class App extends Component {
143153
headerWrapperStyle={styles.headerWrapperStyle}
144154
/>
145155

156+
<View style={styles.topSpacing}>
157+
<Button onPress={this.goToToday} title="Go to Today" />
158+
</View>
159+
146160
<View style={styles.topSpacing}>
147161
<Text style={styles.text}>Selected (Start) date: {formattedStartDate}</Text>
148162
{!!formattedEndDate &&

0 commit comments

Comments
 (0)