Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0907e62

Browse files
committedJan 1, 2021
Scroll to setSelectedDate() (#240)
Add prop `scrollToOnSetSelectedDate` to control repositioning.
1 parent a3cd828 commit 0907e62

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed
 

‎README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ $ yarn add react-native-calendar-strip
6161

6262
The `scrollable` prop was introduced in 2.0.0 and features a bi-directional infinite scroller. It recycles days using RecyclerListView, shifting the dates as the ends are reached. The Chrome debugger can cause issues with this updating due to a [RN setTimeout bug](https://github.com/facebook/react-native/issues/4470). To prevent date shifts at the ends of the scroller, set the `minDate` and `maxDate` range to a year or less.
6363

64-
The refactor to support `scrollable` introduced internal changes to the `CalendarDay` component. Users of the `dayComponent` prop may need to adjust their custom day component to accomomdate the props passed to it.
64+
The refactor to support `scrollable` introduced internal changes to the `CalendarDay` component. Users of the `dayComponent` prop may need to adjust their custom day component to accommodate the props passed to it.
6565

6666
<div align="center">
6767
<img src="https://user-images.githubusercontent.com/6295083/82712731-54a98780-9c4e-11ea-9076-eddf0b756239.gif" alt="">
@@ -192,7 +192,7 @@ AppRegistry.registerComponent('Example', () => Example);
192192
| **`startingDate`** | Date to be used for centering the calendar/showing the week based on that date. It is internally wrapped by `moment` so it accepts both `Date` and `moment Date`. | Any |
193193
| **`selectedDate`** | Date to be used as pre selected Date. It is internally wrapped by `moment` so it accepts both `Date` and `moment Date`. | Any |
194194
| **`onDateSelected`** | Function to be used as a callback when a date is selected. Receives param `date` Moment date. | Function |
195-
| **`onWeekChanged`** | Function to be used as a callback when a week is changed. Receives params `(start, end)` Moment dates. | Function |
195+
| **`onWeekChanged`** | Function to be used as a callback when a week is changed. Receives params `(start, end)` Moment dates. | Function |
196196
| **`onHeaderSelected`**| Function to be used as a callback when the header is selected. Receives param object `{weekStartDate, weekEndDate}` Moment dates. | Function |
197197
| **`headerText`** | Text to use in the header. Use with `onWeekChanged` to receive the visible start & end dates. | String |
198198
| **`updateWeek`** | Update the week view if other props change. If `false`, the week view won't change when other props change, but will still respond to left/right selectors. | Bool | **`True`** |
@@ -202,6 +202,7 @@ AppRegistry.registerComponent('Example', () => Example);
202202
| **`datesWhitelist`** | Array of dates that are enabled, or a function callback which receives a date param and returns true if enabled. Array supports ranges specified with an object entry in the array. Check example <a href="#dateswhitelist-array-example">Below</a> | Array or Func |
203203
| **`datesBlacklist`** | Array of dates that are disabled, or a function callback. Same format as _datesWhitelist_. This overrides dates in _datesWhitelist_. | Array or Func |
204204
| **`markedDates`** | Dates that are marked with dots or lines. Format as <a href="#markeddates-example">markedDatesFormat</a>. | Array or Func | **[]**
205+
| **`scrollToOnSetSelectedDate`** | Controls whether to reposition the scroller to the date passed to `setSelectedDate`. | Bool | **`True`** |
205206

206207

207208
##### datesWhitelist Array Example
@@ -435,7 +436,7 @@ Methods may be accessed through the instantiated component's [ref](https://react
435436
| Prop | Description |
436437
| ------------------------------------- | --------------------------------------------------------------------------------- |
437438
| **`getSelectedDate()`** | Returns the currently selected date. If no date is selected, returns undefined. |
438-
| **`setSelectedDate(date)`** | Sets the selected date. `date` may be a Moment object, ISO8601 date string, or any format that Moment is able to parse. It is the responsibility of the caller to select a date that makes sense (e.g. within the current week view). Passing in a value of `0` effectively clears the selected date. |
439+
| **`setSelectedDate(date)`** | Sets the selected date. `date` may be a Moment object, ISO8601 date string, or any format that Moment is able to parse. It is the responsibility of the caller to select a date that makes sense (e.g. within the current week view). Passing in a value of `0` effectively clears the selected date. `scrollToOnSetSelectedDate` controls whether the scroller repositions to the selected date. |
439440
| **`getNextWeek()`** | Advance to the next week. |
440441
| **`getPreviousWeek()`** | Rewind to the previous week. |
441442
| **`updateWeekView(date)`** | Show the week starting on `date`. |

‎index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ interface CalendarStripProps {
8484
datesWhitelist?: TDateRange[] | ((date: Moment) => void);
8585
datesBlacklist?: TDateRange[] | ((date: Moment) => void);
8686
markedDates?: any[] | ((date: Moment) => void);
87+
scrollToOnSetSelectedDate?: boolean;
8788

8889
showMonth?: boolean;
8990
showDayName?: boolean;

‎src/CalendarStrip.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class CalendarStrip extends Component {
4141
headerText: PropTypes.string,
4242

4343
markedDates: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
44+
scrollToOnSetSelectedDate: PropTypes.bool,
4445

4546
showMonth: PropTypes.bool,
4647
showDayName: PropTypes.bool,
@@ -108,7 +109,8 @@ class CalendarStrip extends Component {
108109
minDayComponentSize: 10,
109110
shouldAllowFontScaling: true,
110111
markedDates: [],
111-
useNativeDriver: true
112+
useNativeDriver: true,
113+
scrollToOnSetSelectedDate: true,
112114
};
113115

114116
constructor(props) {
@@ -298,6 +300,12 @@ class CalendarStrip extends Component {
298300
setSelectedDate = date => {
299301
let mDate = moment(date);
300302
this.onDateSelected(mDate);
303+
if (this.props.scrollToOnSetSelectedDate) {
304+
// Scroll to selected date, centered in the week
305+
const scrolledDate = moment(mDate);
306+
scrolledDate.subtract(Math.floor(this.props.numDaysInWeek / 2), "days");
307+
this.scroller.scrollToDate(scrolledDate);
308+
}
301309
}
302310

303311
// Gather animations from each day. Sequence animations must be started

0 commit comments

Comments
 (0)
Please sign in to comment.