Skip to content

Commit

Permalink
onRangeChange prop (#641)
Browse files Browse the repository at this point in the history
* Added onRangeChange prop

* Normalized date range paramater to always be an object

* onRangeChange can work with custom views if range() is implemented, used warning package instead of console.warn

* Elaborated onRangeChange documentation

* Fixed handleViewChange passing stale prop to onRangeChange
  • Loading branch information
jhockett authored and jquense committed Mar 19, 2018
1 parent e2fd1b3 commit c1ddac4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/Agenda.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ class Agenda extends React.Component {
}
}

Agenda.range = (start, { length = Agenda.defaultProps.length }) => {
let end = dates.add(start, length, 'day');
return { start, end };
};

Agenda.navigate = (date, action, { length = Agenda.defaultProps.length }) => {
switch (action) {
case navigate.PREVIOUS:
Expand Down
29 changes: 27 additions & 2 deletions src/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
dateRangeFormat,
views as componentViews,
} from './utils/propTypes'
import warning from 'warning';

import { notify } from './utils/helpers'
import { navigate, views } from './utils/constants'
Expand Down Expand Up @@ -251,6 +252,14 @@ class Calendar extends React.Component {
*/
onDrillDown: PropTypes.func,

/**
* Callback fired when the visible date range changes. Returns an Array of dates
* or an object with start and end dates for BUILTIN views.
*
* Cutom views may return something different.
*/
onRangeChange: PropTypes.func,

/**
* A callback fired when a date selection is made. Only fires when `selectable` is `true`.
*
Expand Down Expand Up @@ -807,6 +816,18 @@ class Calendar extends React.Component {
)
}

handleRangeChange = (date, view) => {
let { onRangeChange } = this.props
if(onRangeChange) {
if(view.range) {
onRangeChange(view.range(date, {}))
}
else {
warning(true, 'onRangeChange prop not supported for this view')
}
}
}

handleNavigate = (action, newDate) => {
let { view, date, getNow, onNavigate, ...props } = this.props
let ViewComponent = this.getView()
Expand All @@ -819,13 +840,17 @@ class Calendar extends React.Component {
})

onNavigate(date, view, action)
}
this.handleRangeChange(date, ViewComponent)
};

handleViewChange = view => {
if (view !== this.props.view && isValidView(view, this.props)) {
this.props.onView(view)
}
}

let views = this.getViews();
this.handleRangeChange(this.props.date, views[view])
};

handleSelectEvent = (...args) => {
notify(this.props.onSelectEvent, args)
Expand Down
15 changes: 10 additions & 5 deletions src/Day.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ class Day extends React.Component {
}

render() {
let { date, ...props } = this.props
let { date, ...props } = this.props;
let range = Day.range(date);

return (
<TimeGrid
{...props}
range={[dates.startOf(date, 'day')]}
range={range}
eventOffset={10}
/>
)
);
}
}

Day.navigate = (date, action) => {
switch (action) {
Day.range = (date) => {
return [dates.startOf(date, 'day')]
}

Day.navigate = (date, action)=>{
switch (action){
case navigate.PREVIOUS:
return dates.add(date, -1, 'day')

Expand Down
6 changes: 6 additions & 0 deletions src/Month.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ class MonthView extends React.Component {
}
}

MonthView.range = (date, { culture }) => {
let start = dates.firstVisibleDay(date, culture)
let end = dates.lastVisibleDay(date, culture)
return { start, end }
}

MonthView.navigate = (date, action) => {
switch (action) {
case navigate.PREVIOUS:
Expand Down
6 changes: 6 additions & 0 deletions src/WorkWeek.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class WorkWeek extends React.Component {
}
}

WorkWeek.range = (date, options) => {
return Week.range(date, options).filter(
d => [6, 0].indexOf(d.getDay()) === -1
)
}

WorkWeek.navigate = Week.navigate

WorkWeek.title = (date, { formats, culture }) => {
Expand Down

0 comments on commit c1ddac4

Please sign in to comment.