From c6ea87f3ea72eee18364ec7f7c093205ba3aa3e0 Mon Sep 17 00:00:00 2001 From: Vignesh Joglekar Date: Fri, 22 Jan 2021 05:29:16 -0600 Subject: [PATCH] Adds date selection keybindings (#630) * Main Pass * Changelog * Resolves comments --- CHANGELOG.md | 1 + assets/js/dashboard/datepicker.js | 39 ++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8e85cffe2e4..ba1115b6d0d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. - Unique Visitors (last 30 min) as a top stat in realtime view plausible/analytics#500 - Pinned filter and date selector rows while scrolling plausible/analytics#472 - Escape keyboard shortcut to clear all filters plausible/analytics#625 +- Keybindings for selecting dates/ranges plausible/analytics#630 ### Changed diff --git a/assets/js/dashboard/datepicker.js b/assets/js/dashboard/datepicker.js index 42882e5b7d89..1b9493c399ce 100644 --- a/assets/js/dashboard/datepicker.js +++ b/assets/js/dashboard/datepicker.js @@ -25,6 +25,8 @@ class DatePicker extends React.Component { super(props); this.handleKeyup = this.handleKeyup.bind(this); this.handleClick = this.handleClick.bind(this); + this.openCalendar = this.openCalendar.bind(this); + this.open = this.open.bind(this); this.state = { mode: "menu", open: false }; } @@ -96,7 +98,16 @@ class DatePicker extends React.Component { } } - if (newSearch.date) { + this.setState({open: false}); + + const keys = ['d', 'r', 'w', 'm', 'y']; + const redirects = [{date: false, period: 'day'}, {period: 'realtime'}, {date: false, period: '7d'}, {date: false, period: 'month'}, {date: false, period: '12mo'}]; + + if (keys.includes(e.key.toLowerCase())) { + navigateToQuery(history, query, {...newSearch, ...(redirects[keys.indexOf(e.key) % 5])}); + } else if (e.key.toLowerCase() === 'c') { + this.setState({mode: 'calendar', open: true}, this.openCalendar); + } else if (newSearch.date) { navigateToQuery(history, query, newSearch); } } @@ -226,8 +237,8 @@ class DatePicker extends React.Component { ref={(node) => (this.dropDownNode = node)} >
{text} + {keybinds[text]} ); } @@ -332,11 +352,11 @@ class DatePicker extends React.Component {
this.setState({mode: 'calendar'}, this.openCalendar.bind(this))} - onKeyPress={() => this.setState({mode: 'calendar'}, this.openCalendar.bind(this))} - className="block px-4 py-2 md:text-sm leading-tight hover:bg-gray-100 + onClick={() => this.setState({mode: 'calendar'}, this.openCalendar)} + onKeyPress={() => this.setState({mode: 'calendar'}, this.openCalendar)} + className="px-4 py-2 md:text-sm leading-tight hover:bg-gray-100 dark:hover:bg-gray-900 hover:text-gray-900 dark:hover:text-gray-100 - cursor-pointer" + cursor-pointer flex items-center justify-between" tabIndex="0" role="button" aria-haspopup="true" @@ -344,6 +364,7 @@ class DatePicker extends React.Component { aria-controls="calendar" > Custom range + C