From d66601a5f539661eb81dd1938bd831482b6ddd7a Mon Sep 17 00:00:00 2001 From: kay delaney <45561153+kaydelaney@users.noreply.github.com> Date: Wed, 14 Aug 2019 13:37:04 +0100 Subject: [PATCH] Explore/Prometheus: More consistently allows for multi-line queries (#18362) * Explore/Prometheus: More consistently allows for multi-line queries Allows a user to hit shift+enter to create a new line in the query field, even when the autocomplete suggestions are displayed. Also fixes an issue where a new line was inserted when selecting a suggestion Closes #18341 * Fixes behavior where query wasn't running on pressing Enter Also adds test to verify this behavior --- .../app/features/explore/QueryField.test.tsx | 18 +++++++++-- public/app/features/explore/QueryField.tsx | 32 ++++++++----------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/public/app/features/explore/QueryField.test.tsx b/public/app/features/explore/QueryField.test.tsx index 9b3e915a93b..e58127345f0 100644 --- a/public/app/features/explore/QueryField.test.tsx +++ b/public/app/features/explore/QueryField.test.tsx @@ -4,16 +4,28 @@ import { shallow } from 'enzyme'; import { QueryField } from './QueryField'; describe('', () => { - it('renders with null initial value', () => { + it('should render with null initial value', () => { const wrapper = shallow(); expect(wrapper.find('div').exists()).toBeTruthy(); }); - it('renders with empty initial value', () => { + + it('should render with empty initial value', () => { const wrapper = shallow(); expect(wrapper.find('div').exists()).toBeTruthy(); }); - it('renders with initial value', () => { + + it('should render with initial value', () => { const wrapper = shallow(); expect(wrapper.find('div').exists()).toBeTruthy(); }); + + it('should execute query when enter is pressed and there are no suggestions visible', () => { + const wrapper = shallow(); + const instance = wrapper.instance() as QueryField; + instance.executeOnChangeAndRunQueries = jest.fn(); + const handleEnterAndTabKeySpy = jest.spyOn(instance, 'handleEnterAndTabKey'); + instance.onKeyDown({ key: 'Enter', preventDefault: () => {} } as KeyboardEvent, {}); + expect(handleEnterAndTabKeySpy).toBeCalled(); + expect(instance.executeOnChangeAndRunQueries).toBeCalled(); + }); }); diff --git a/public/app/features/explore/QueryField.tsx b/public/app/features/explore/QueryField.tsx index 9474b22ebf4..0bbc97ba16c 100644 --- a/public/app/features/explore/QueryField.tsx +++ b/public/app/features/explore/QueryField.tsx @@ -307,29 +307,23 @@ export class QueryField extends React.PureComponent { const { typeaheadIndex, suggestions } = this.state; - if (this.menuEl) { - // Dont blur input - event.preventDefault(); - if (!suggestions || suggestions.length === 0) { - return undefined; - } + event.preventDefault(); - const suggestion = getSuggestionByIndex(suggestions, typeaheadIndex); - const nextChange = this.applyTypeahead(change, suggestion); - - const insertTextOperation = nextChange.operations.find((operation: any) => operation.type === 'insert_text'); - if (insertTextOperation) { - return undefined; - } - - return true; - } else if (!event.shiftKey) { - // Run queries if Shift is not pressed, otherwise pass through + if (event.shiftKey) { + // pass through if shift is pressed + return undefined; + } else if (!this.menuEl) { this.executeOnChangeAndRunQueries(); - return true; + } else if (!suggestions || suggestions.length === 0) { + return undefined; } - return undefined; + + const suggestion = getSuggestionByIndex(suggestions, typeaheadIndex); + const nextChange = this.applyTypeahead(change, suggestion); + + const insertTextOperation = nextChange.operations.find((operation: any) => operation.type === 'insert_text'); + return insertTextOperation ? true : undefined; }; onKeyDown = (event: KeyboardEvent, change: Change) => {