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

feat(datepicker): add keyboard navigation #1273

Closed

Conversation

divdavem
Copy link
Member

@divdavem divdavem commented Jan 30, 2017

The following keyboard shortcuts are now available:
Arrow left/right: previous/next day
Arrow up/down: previous/next week
Page up/down: previous/next month
Shift+page up/down: previous/next year
Home/end: beginning/end of the currently displayed month(s)
Shift+home/end: min/max selectable date

Note that the previous/next month and previous/next year shortcuts should probably keep the current day in the previous/next month and the current month in the previous/next year. I think this should be implemented by a change in the behavior of getNext/getPrev in ngb-calendar. This probably has to be discussed, especially with @maxokorokov who designed this API. I have opened another PR about it: #1277

@divdavem divdavem force-pushed the datepickerKeyboardNavigation branch 7 times, most recently from 2ad3d45 to 76f5994 Compare January 31, 2017 15:59
@divdavem divdavem force-pushed the datepickerKeyboardNavigation branch 2 times, most recently from ea6bee6 to efcae51 Compare February 1, 2017 14:39
@divdavem divdavem changed the title feat(datepicker): add keyboard navigation (work in progress) feat(datepicker): add keyboard navigation Feb 1, 2017
Copy link
Member

@maxokorokov maxokorokov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

A couple of comments I have:

  1. The only big concern I have is that now keyboard focusing and navigation between months are very much tied to each other in the code.

I would have done focus handling as an optional service that works with the datepicker. Now it starts to get huge, so I think we should think about something like that. Currently 25% of the datapicker.ts code is focus handling

  1. I'm not sure everybody would want to have the keyboard support ON all the time. Like for time range selection cases, etc. But this could be optionally turned off later I guess

Minor comments:

  • when two months are displayed, clicking on navigation arrows changes focus between months, when it should do navigation actually
  • please order members/methods alphabetically

return event;
}

function triggerFocus(element: DebugElement) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused function I suppose

@@ -250,17 +260,26 @@ export class NgbDatepicker implements OnChanges,
}
}

onDateSelect(date: NgbDate) {
this._setViewWithinLimits(date);
isDisplayedDateSelectable(date: NgbDate) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be private _isDisplayedDateSelectable

break;
}

this._updateData();
}

@HostListener('keydown', ['$event'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other components the events are usually grouped in the host like so:

(keydown): onKeyDown($event)

I think I would prefer to do the same here to quickly see what kind of handlers we have on the datepicker

return;
}
switch (event.keyCode) {
case 33 /* page up */:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In rating and typeahead we have an enum Keys that contains key codes (and ideally should be shared), so switch reads like: case Key.PageUp:

Not sure which one I prefer though...

}

@HostListener('blur', ['$event'])
onBlur(event: FocusEvent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be '(blur)': 'focusedDate = null' I suppose

break;
}

this._updateData();
}

@HostListener('keydown', ['$event'])
onKeyDown(event: KeyboardEvent) {
let focusedDate = this.focusedDate;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be const, or just use this.focusedDate altogether?

@@ -310,6 +416,46 @@ export class NgbDatepicker implements OnChanges,
}
}

private getFirstDisplayedDate() { return this.months[0].firstDate; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method name should start with _

@@ -310,6 +416,46 @@ export class NgbDatepicker implements OnChanges,
}
}

private getFirstDisplayedDate() { return this.months[0].firstDate; }

private getLastDisplayedDate() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

method name should start with _

@divdavem divdavem force-pushed the datepickerKeyboardNavigation branch 2 times, most recently from 8ad8679 to f03190d Compare February 16, 2017 14:48
@divdavem
Copy link
Member Author

@maxokorokov Thank you for your review. I have pushed some changes following your comments.

Regarding the idea of implementing keyboard navigation as a separate service, I had actually asked the question to @pkozlowski-opensource but he had answered to keep it simple in the same file for now.
We can do it later for sure to allow customization of navigation shortcuts, but, however, I don't see the point of actually disabling keyboard navigation. Cases such as range-selection can also benefit from keyboard navigation.

@divdavem divdavem force-pushed the datepickerKeyboardNavigation branch from f03190d to 97e6d91 Compare March 15, 2017 14:09
@maxokorokov maxokorokov mentioned this pull request Mar 29, 2017
10 tasks
@divdavem divdavem force-pushed the datepickerKeyboardNavigation branch 2 times, most recently from cd1e3f7 to fd7d0b3 Compare March 29, 2017 13:50
@divdavem
Copy link
Member Author

@maxokorokov I have rebased this PR and resolved the conflict with the master branch. Do you think it could be merged soon?

@maxokorokov
Copy link
Member

As we've discussed @divdavem, we'll try to refactor this quite a bit:

  • merge navigation and focusing logic, because it's essentially the same thing
  • isolate it into something like NgbDatepickerNavigationService and remove from NgbDatepicker itself

@maxokorokov maxokorokov dismissed their stale review April 13, 2017 16:20

Refactoring required

The following shortcuts are available:
Arrow left/right: previous/next day
Arrow up/down: previous/next week
Page up/down: previous/next month
Shift+page up/down: previous/next year
Home/end: beginning/end of the current view
Shift+home/end: min/max selectable date
@maxokorokov
Copy link
Member

Moved everything from from here in the #1551, closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants