From 44ca05272a2e70417341825722f5f4e61c388fc1 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Thu, 21 May 2020 14:51:18 +0200 Subject: [PATCH] Query history: Add keyboard shortcut support for commenting (#24736) * Shorten filter history label * Add keyboard shortcuts for update comment in Query history * Add test coverage for new keyboard shortcuts * Update changed aria-label in tests * Add test scenario for enter and ctr key --- .../RichHistory/RichHistoryCard.test.tsx | 34 +++++++++++++++++++ .../explore/RichHistory/RichHistoryCard.tsx | 22 +++++++++--- .../RichHistory/RichHistoryQueriesTab.tsx | 7 ++-- 3 files changed, 53 insertions(+), 10 deletions(-) diff --git a/public/app/features/explore/RichHistory/RichHistoryCard.test.tsx b/public/app/features/explore/RichHistory/RichHistoryCard.test.tsx index 25b12b9580b..a93271d9185 100644 --- a/public/app/features/explore/RichHistory/RichHistoryCard.test.tsx +++ b/public/app/features/explore/RichHistory/RichHistoryCard.test.tsx @@ -100,6 +100,40 @@ describe('RichHistoryCard', () => { expect(wrapper.find({ title: 'Add comment' })).toHaveLength(1); expect(wrapper.find({ title: 'Edit comment' })).toHaveLength(0); }); + it('should open update comment form when edit comment button clicked', () => { + const wrapper = setup({ query: starredQueryWithComment }); + const editCommentButton = wrapper.find({ title: 'Edit comment' }); + editCommentButton.simulate('click'); + expect(wrapper.find({ 'aria-label': 'Update comment form' })).toHaveLength(1); + }); + it('should close update comment form when escape key pressed', () => { + const wrapper = setup({ query: starredQueryWithComment }); + const editCommentButton = wrapper.find({ title: 'Edit comment' }); + editCommentButton.simulate('click'); + wrapper.simulate('keydown', { key: 'Escape' }); + expect(wrapper.find({ 'aria-label': 'Update comment form' })).toHaveLength(0); + }); + it('should close update comment form when enter and shift keys pressed', () => { + const wrapper = setup({ query: starredQueryWithComment }); + const editCommentButton = wrapper.find({ title: 'Edit comment' }); + editCommentButton.simulate('click'); + wrapper.simulate('keydown', { key: 'Enter', shiftKey: true }); + expect(wrapper.find({ 'aria-label': 'Update comment form' })).toHaveLength(0); + }); + it('should close update comment form when enter and ctrl keys pressed', () => { + const wrapper = setup({ query: starredQueryWithComment }); + const editCommentButton = wrapper.find({ title: 'Edit comment' }); + editCommentButton.simulate('click'); + wrapper.simulate('keydown', { key: 'Enter', ctrlKey: true }); + expect(wrapper.find({ 'aria-label': 'Update comment form' })).toHaveLength(0); + }); + it('should not close update comment form when enter key pressed', () => { + const wrapper = setup({ query: starredQueryWithComment }); + const editCommentButton = wrapper.find({ title: 'Edit comment' }); + editCommentButton.simulate('click'); + wrapper.simulate('keydown', { key: 'Enter', shiftKey: false }); + expect(wrapper.find({ 'aria-label': 'Update comment form' })).toHaveLength(1); + }); }); describe('starring', () => { diff --git a/public/app/features/explore/RichHistory/RichHistoryCard.tsx b/public/app/features/explore/RichHistory/RichHistoryCard.tsx index 43c689a4a78..04aa14d5522 100644 --- a/public/app/features/explore/RichHistory/RichHistoryCard.tsx +++ b/public/app/features/explore/RichHistory/RichHistoryCard.tsx @@ -195,16 +195,26 @@ export function RichHistoryCard(props: Props) { const onUpdateComment = () => { updateRichHistory(query.ts, 'comment', comment); - toggleActiveUpdateComment(); + setActiveUpdateComment(false); }; const onCancelUpdateComment = () => { - toggleActiveUpdateComment(); + setActiveUpdateComment(false); setComment(query.comment); }; + const onKeyDown = (keyEvent: React.KeyboardEvent) => { + if (keyEvent.key === 'Enter' && (keyEvent.shiftKey || keyEvent.ctrlKey)) { + onUpdateComment(); + } + + if (keyEvent.key === 'Escape') { + onCancelUpdateComment(); + } + }; + const updateComment = ( -
+