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 = ( -
+