From 4e57ead38d7b1899674e7aace1ea7804c39cf06b Mon Sep 17 00:00:00 2001 From: Peter Holmberg Date: Wed, 6 Mar 2019 13:07:08 +0100 Subject: [PATCH] Prevent search in VizPicker from stealing focus (#15802) Fixes #15569 focus issue in viz picker search * added state to not set focus on search every render * move to new component to handle focus --- .../panel_editor/VisualizationTab.tsx | 34 ++++++++----------- .../panel_editor/VizPickerSearch.tsx | 33 ++++++++++++++++++ 2 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 public/app/features/dashboard/panel_editor/VizPickerSearch.tsx diff --git a/public/app/features/dashboard/panel_editor/VisualizationTab.tsx b/public/app/features/dashboard/panel_editor/VisualizationTab.tsx index 5330baf1be6..884615821eb 100644 --- a/public/app/features/dashboard/panel_editor/VisualizationTab.tsx +++ b/public/app/features/dashboard/panel_editor/VisualizationTab.tsx @@ -14,10 +14,10 @@ import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp'; import { FadeIn } from 'app/core/components/Animations/FadeIn'; // Types -import { PanelModel } from '../state/PanelModel'; -import { DashboardModel } from '../state/DashboardModel'; +import { PanelModel } from '../state'; +import { DashboardModel } from '../state'; import { PanelPlugin } from 'app/types/plugins'; -import { FilterInput } from 'app/core/components/FilterInput/FilterInput'; +import { VizPickerSearch } from './VizPickerSearch'; interface Props { panel: PanelModel; @@ -33,18 +33,19 @@ interface State { isVizPickerOpen: boolean; searchQuery: string; scrollTop: number; + hasBeenFocused: boolean; } export class VisualizationTab extends PureComponent { element: HTMLElement; angularOptions: AngularComponent; - searchInput: HTMLElement; constructor(props) { super(props); this.state = { isVizPickerOpen: this.props.urlOpenVizPicker, + hasBeenFocused: false, searchQuery: '', scrollTop: 0, }; @@ -162,7 +163,7 @@ export class VisualizationTab extends PureComponent { this.props.updateLocation({ query: { openVizPicker: null }, partial: true }); } - this.setState({ isVizPickerOpen: false }); + this.setState({ isVizPickerOpen: false, hasBeenFocused: false }); }; onSearchQueryChange = (value: string) => { @@ -173,23 +174,16 @@ export class VisualizationTab extends PureComponent { renderToolbar = (): JSX.Element => { const { plugin } = this.props; - const { searchQuery } = this.state; + const { isVizPickerOpen, searchQuery } = this.state; - if (this.state.isVizPickerOpen) { + if (isVizPickerOpen) { return ( - <> - elem && elem.focus()} - /> - - + ); } else { return ( diff --git a/public/app/features/dashboard/panel_editor/VizPickerSearch.tsx b/public/app/features/dashboard/panel_editor/VizPickerSearch.tsx new file mode 100644 index 00000000000..ddf9485dab9 --- /dev/null +++ b/public/app/features/dashboard/panel_editor/VizPickerSearch.tsx @@ -0,0 +1,33 @@ +import React, { PureComponent } from 'react'; + +import { FilterInput } from 'app/core/components/FilterInput/FilterInput'; + +import { PanelPlugin } from 'app/types'; + +interface Props { + plugin: PanelPlugin; + searchQuery: string; + onChange: (query: string) => void; + onClose: () => void; +} + +export class VizPickerSearch extends PureComponent { + render() { + const { searchQuery, onChange, onClose } = this.props; + return ( + <> + element && element.focus()} + /> + + + ); + } +}