Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add overrideDrillUp prop Fixes #487 #504

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Displays a complete, interactive calendar.
|onClickYear|Function called when the user clicks a year.|n/a|`(value, event) => alert('Clicked year: ', value)`|
|onDrillDown|Function called when the user drills down by clicking a tile.|n/a|`({ activeStartDate, view }) => alert('Drilled down to: ', activeStartDate, view)`|
|onDrillUp|Function called when the user drills up by clicking drill up button.|n/a|`({ activeStartDate, view }) => alert('Drilled up to: ', activeStartDate, view)`|
|overrideDrillUp|Determines whether the calendar cycles between the minimum detail view and maximum view when clicking the drill up button|`false`|`true`
|prevAriaLabel|`aria-label` attribute of the "previous" button on the navigation pane.|n/a|`"Previous"`|
|prevLabel|Content of the "previous" button on the navigation pane. Setting the value explicitly to null will hide the icon.|`"‹"`|<ul><li>String: `"‹"`</li><li>React element: `<PreviousIcon />`</li></ul>|
|prev2AriaLabel|`aria-label` attribute of the "previous on higher level" button on the navigation pane.|n/a|`"Jump backwards"`|
Expand Down
27 changes: 19 additions & 8 deletions src/Calendar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,25 @@ export default class Calendar extends Component {
}

drillUp = () => {
if (!this.drillUpAvailable) {
const { activeStartDate, view, views } = this;
const {
onDrillUp,
maxDetail,
overrideDrillUp,
minDate,
} = this.props;
if (!this.drillUpAvailable && !overrideDrillUp) {
return;
}

const { activeStartDate, view, views } = this;
const { onDrillUp } = this.props;

const nextView = views[views.indexOf(view) - 1];
let nextView = -1;
if (!this.drillUpAvailable && overrideDrillUp) {
nextView = views[views.indexOf(maxDetail)];
} else {
nextView = views[views.indexOf(view) - 1];
}
const nextActiveStartDate = getBegin(nextView, activeStartDate);

this.setStateAndCallCallbacks({
activeStartDate: nextActiveStartDate,
activeStartDate: nextActiveStartDate < minDate ? minDate : nextActiveStartDate,
view: nextView,
}, undefined, onDrillUp);
}
Expand Down Expand Up @@ -572,6 +579,7 @@ export default class Calendar extends Component {
next2Label,
nextAriaLabel,
nextLabel,
overrideDrillUp,
prev2AriaLabel,
prev2Label,
prevAriaLabel,
Expand All @@ -594,6 +602,7 @@ export default class Calendar extends Component {
next2Label={next2Label}
nextAriaLabel={nextAriaLabel}
nextLabel={nextLabel}
overrideDrillUp={overrideDrillUp}
prev2AriaLabel={prev2AriaLabel}
prev2Label={prev2Label}
prevAriaLabel={prevAriaLabel}
Expand Down Expand Up @@ -645,6 +654,7 @@ Calendar.defaultProps = {
maxDetail: 'month',
minDate: defaultMinDate,
minDetail: 'century',
overrideDrillUp: false,
returnValue: 'start',
showNavigation: true,
showNeighboringMonth: true,
Expand Down Expand Up @@ -691,6 +701,7 @@ Calendar.propTypes = {
onDrillDown: PropTypes.func,
onDrillUp: PropTypes.func,
onViewChange: PropTypes.func,
overrideDrillUp: PropTypes.bool,
prev2AriaLabel: PropTypes.string,
prev2Label: PropTypes.node,
prevAriaLabel: PropTypes.string,
Expand Down
11 changes: 11 additions & 0 deletions src/Calendar.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,17 @@ describe('Calendar', () => {

expect(onDrillUp).not.toHaveBeenCalled();
});
it('drill up cycles correctly to maximum detail view when already at minimum detail view', () => {
const component = shallow(
<Calendar
overrideDrillUp
/>,
);
component.setState({ view: 'century' });
component.instance().drillUp();

expect(component.instance().view).toBe('month');
});
});

describe('handles drill down properly', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/Calendar/Navigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export default function Navigation({
showDoubleView,
view,
views,
overrideDrillUp,
}) {
const drillUpAvailable = views.indexOf(view) > 0;
const drillUpAvailable = views.indexOf(view) > 0 || overrideDrillUp;
const shouldShowPrevNext2Buttons = view !== 'century';

const previousActiveStartDate = getBeginPrevious(view, activeStartDate);
Expand Down Expand Up @@ -71,7 +72,6 @@ export default function Navigation({
})();

const nextButtonDisabled = maxDate && maxDate <= nextActiveStartDate;

const next2ButtonDisabled = (
shouldShowPrevNext2Buttons
&& maxDate
Expand Down Expand Up @@ -220,6 +220,7 @@ Navigation.propTypes = {
next2Label: PropTypes.node,
nextAriaLabel: PropTypes.string,
nextLabel: PropTypes.node,
overrideDrillUp: PropTypes.bool,
prev2AriaLabel: PropTypes.string,
prev2Label: PropTypes.node,
prevAriaLabel: PropTypes.string,
Expand Down
2 changes: 0 additions & 2 deletions src/Calendar/Navigation.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ describe('Navigation', () => {

expect(centurySetActiveStartDateFn).toHaveBeenCalledWith(new Date(2101, 0, 1));
});

it('correctly marks drillUp button as disabled when already on top allowed view', () => {
const component = shallow(
<Navigation
Expand All @@ -373,7 +372,6 @@ describe('Navigation', () => {

expect(button.prop('disabled')).toBeTruthy();
});

it('disallows navigating before minDate', () => {
const component = shallow(
<Navigation
Expand Down
2 changes: 1 addition & 1 deletion src/TileGroup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ TileGroup.propTypes = {
...tileGroupProps,
activeStartDate: PropTypes.instanceOf(Date),
count: PropTypes.number,
dateType: PropTypes.string,
dateTransform: PropTypes.func.isRequired,
dateType: PropTypes.string,
offset: PropTypes.number,
step: PropTypes.number,
tile: PropTypes.func.isRequired,
Expand Down