Skip to content

Commit

Permalink
Add props onWeekScrollStart and onWeekScrollEnd for scrollable mo…
Browse files Browse the repository at this point in the history
…de (#279, #281)
  • Loading branch information
psolom authored and peacechen committed Mar 3, 2021
1 parent 4caf1b3 commit 73b9847
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ AppRegistry.registerComponent('Example', () => Example);
| **`selectedDate`** | Date to be used as pre selected Date. It is internally wrapped by `moment` so it accepts both `Date` and `moment Date`. | Any |
| **`onDateSelected`** | Function to be used as a callback when a date is selected. Receives param `date` Moment date. | Function |
| **`onWeekChanged`** | Function to be used as a callback when a week is changed. Receives params `(start, end)` Moment dates. | Function |
| **`onWeekScrollStart`**| Function to be used as a callback in `scrollable` mode when dates page starts gliding. Receives params `(start, end)` Moment dates. | Function |
| **`onWeekScrollEnd`**| Function to be used as a callback in `scrollable` mode when dates page stops gliding. Receives params `(start, end)` Moment dates. | Function |
| **`onHeaderSelected`**| Function to be used as a callback when the header is selected. Receives param object `{weekStartDate, weekEndDate}` Moment dates. | Function |
| **`headerText`** | Text to use in the header. Use with `onWeekChanged` to receive the visible start & end dates. | String |
| **`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`** |
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ interface CalendarStripProps {
selectedDate?: Moment;
onDateSelected?: ((date: Moment) => void);
onWeekChanged?: ((start: Moment, end: Moment) => void);
onWeekScrollStart?: ((start: Moment, end: Moment) => void);
onWeekScrollEnd?: ((start: Moment, end: Moment) => void);
onHeaderSelected?: ((dates: {weekStartDate: Moment, weekEndDate: Moment}) => void);
updateWeek?: boolean;
useIsoWeekday?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions src/CalendarStrip.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class CalendarStrip extends Component {
selectedDate: PropTypes.any,
onDateSelected: PropTypes.func,
onWeekChanged: PropTypes.func,
onWeekScrollStart: PropTypes.func,
onWeekScrollEnd: PropTypes.func,
onHeaderSelected: PropTypes.func,
updateWeek: PropTypes.bool,
useIsoWeekday: PropTypes.bool,
Expand Down Expand Up @@ -560,6 +562,8 @@ class CalendarStrip extends Component {
maxDate={this.props.maxDate}
updateMonthYear={this.updateMonthYear}
onWeekChanged={this.props.onWeekChanged}
onWeekScrollStart={this.props.onWeekScrollStart}
onWeekScrollEnd={this.props.onWeekScrollEnd}
externalScrollView={this.props.externalScrollView}
/>
);
Expand Down
50 changes: 50 additions & 0 deletions src/Scroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export default class CalendarScroller extends Component {
maxSimultaneousDays: PropTypes.number,
updateMonthYear: PropTypes.func,
onWeekChanged: PropTypes.func,
onWeekScrollStart: PropTypes.func,
onWeekScrollEnd: PropTypes.func,
externalScrollView: PropTypes.func,
pagingEnabled: PropTypes.bool
}
Expand Down Expand Up @@ -253,6 +255,51 @@ export default class CalendarScroller extends Component {
});
}

onScrollStart = (event) => {
const {onWeekScrollStart} = this.props;
const {prevStartDate, prevEndDate} = this.state;

if (onWeekScrollStart && prevStartDate && prevEndDate) {
onWeekScrollStart(prevStartDate.clone(), prevEndDate.clone());
}
}

onScrollEnd = () => {
const {onWeekScrollEnd} = this.props;
const {visibleStartDate, visibleEndDate, prevEndDate} = this.state;

if (onWeekScrollEnd && visibleStartDate && visibleEndDate) {
if (!visibleEndDate.isSame(prevEndDate, "day")) {
onWeekScrollEnd(visibleStartDate.clone(), visibleEndDate.clone());
}
}
}

onScrollBeginDrag = () => {
const {
onWeekScrollStart,
onWeekScrollEnd,
} = this.props;
// Prev dates required only if scroll callbacks are defined
if (!onWeekScrollStart && !onWeekScrollEnd) {
return;
}
const {
data,
visibleStartDate,
visibleEndDate,
} = this.state;
const prevStartDate = visibleStartDate ? visibleStartDate
: (data[visibleStartIndex] ? data[visibleStartIndex].date : moment());
const prevEndDate = visibleEndDate ? visibleEndDate
: (data[visibleEndIndex] ? data[visibleEndIndex].date : moment());

this.setState({
prevStartDate,
prevEndDate,
});
}

onLayout = event => {
let width = event.nativeEvent.layout.width;
this.setState({
Expand Down Expand Up @@ -292,6 +339,9 @@ export default class CalendarScroller extends Component {
scrollViewProps={{
showsHorizontalScrollIndicator: false,
contentContainerStyle: { paddingRight: this.state.itemWidth / 2 },
onMomentumScrollBegin: this.onScrollStart,
onMomentumScrollEnd: this.onScrollEnd,
onScrollBeginDrag: this.onScrollBeginDrag,
...pagingProps
}}
/>
Expand Down

0 comments on commit 73b9847

Please sign in to comment.