From 05e3a5c074e2d0dd18945f2ec0f3e744849f2a0c Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Wed, 13 Dec 2023 17:03:21 -0800 Subject: [PATCH] [Fix] Remove empty suggestion history (#5349) (#5607) * Add search input filter * Add CHANGELOG.md * Add tests for empty suggestions * Add code comment --------- Signed-off-by: Willie Hung Signed-off-by: Josh Romero Signed-off-by: Anan Zhuang Co-authored-by: Josh Romero Co-authored-by: Anan Zhuang (cherry picked from commit 808591a245da5d2d58ca327c39509b283a56e116) Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] --- .../typeahead/suggestion_component.test.tsx | 41 +++++++++++++++++++ .../ui/typeahead/suggestion_component.tsx | 7 ++++ 2 files changed, 48 insertions(+) diff --git a/src/plugins/data/public/ui/typeahead/suggestion_component.test.tsx b/src/plugins/data/public/ui/typeahead/suggestion_component.test.tsx index 9769e4a19346..f75007444f48 100644 --- a/src/plugins/data/public/ui/typeahead/suggestion_component.test.tsx +++ b/src/plugins/data/public/ui/typeahead/suggestion_component.test.tsx @@ -45,6 +45,14 @@ const mockSuggestion: QuerySuggestion = { type: QuerySuggestionTypes.Value, }; +const mockEmptySuggestion: QuerySuggestion = { + description: '', + end: 0, + start: 0, + text: '', + type: QuerySuggestionTypes.Value, +}; + describe('SuggestionComponent', () => { it('Should display the suggestion and use the provided ariaId', () => { const component = shallow( @@ -135,4 +143,37 @@ describe('SuggestionComponent', () => { component.simulate('mouseenter'); expect(mockHandler).toHaveBeenCalledTimes(1); }); + + it('Should return null for empty suggestion text', () => { + const component = shallow( + + ); + + expect(component.isEmptyRender()).toBeTruthy(); + }); + + it('Should return null for suggestion text with only whitespace', () => { + const whitespaceSuggestion = { ...mockEmptySuggestion, text: ' ' }; + const component = shallow( + + ); + + expect(component.isEmptyRender()).toBeTruthy(); + }); }); diff --git a/src/plugins/data/public/ui/typeahead/suggestion_component.tsx b/src/plugins/data/public/ui/typeahead/suggestion_component.tsx index 7d97f5b58283..67243df6415a 100644 --- a/src/plugins/data/public/ui/typeahead/suggestion_component.tsx +++ b/src/plugins/data/public/ui/typeahead/suggestion_component.tsx @@ -61,6 +61,13 @@ interface Props { } export function SuggestionComponent(props: Props) { + // Removing empty suggestions from the history is for maintaining a clean user experience. + // Empty suggestions, which typically result from inadvertent keystrokes or incomplete queries, + // do not provide value to the user. + if (!props.suggestion.text.trim()) { + return null; + } + return ( // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/interactive-supports-focus