Skip to content

Commit

Permalink
[Discover] move focus on show chart
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaanj committed Jun 23, 2021
1 parent b7aaa1f commit b44f856
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { useCallback } from 'react';
import React, { useCallback, useEffect, useRef } from 'react';
import moment from 'moment';
import { EuiFlexGroup, EuiFlexItem, EuiButtonEmpty, EuiSpacer } from '@elastic/eui';
import { IUiSettingsClient } from 'kibana/public';
Expand Down Expand Up @@ -47,8 +47,20 @@ export function DiscoverChart({
stateContainer: GetStateReturn;
timefield?: string;
}) {
const chartRef = useRef<{ element: HTMLElement | null; moveFocus: boolean }>({
element: null,
moveFocus: false,
});

useEffect(() => {
if (chartRef.current.moveFocus && chartRef.current.element) {
chartRef.current.element.focus();
}
}, [state.hideChart]);

const toggleHideChart = useCallback(() => {
stateContainer.setAppState({ hideChart: !state.hideChart });
chartRef.current.moveFocus = true;
}, [state, stateContainer]);

const onChangeInterval = useCallback(
Expand Down Expand Up @@ -102,9 +114,7 @@ export function DiscoverChart({
<EuiButtonEmpty
size="xs"
iconType={!state.hideChart ? 'eyeClosed' : 'eye'}
onClick={() => {
toggleHideChart();
}}
onClick={toggleHideChart}
data-test-subj="discoverChartToggle"
>
{!state.hideChart
Expand All @@ -122,6 +132,8 @@ export function DiscoverChart({
{!state.hideChart && chartData && (
<EuiFlexItem grow={false}>
<section
ref={(element) => (chartRef.current.element = element)}
tabIndex={-1}
aria-label={i18n.translate('discover.histogramOfFoundDocumentsAriaLabel', {
defaultMessage: 'Histogram of found documents',
})}
Expand Down

0 comments on commit b44f856

Please sign in to comment.