From 2e07d31108907f41d4ff51633fb05ee3bd1f015c Mon Sep 17 00:00:00 2001 From: Hannah Mudge Date: Mon, 17 Jul 2023 15:56:54 -0600 Subject: [PATCH] Switch focus back to search on filter popover close --- .../components/field_picker/field_picker.tsx | 9 +- .../field_picker/field_type_filter.tsx | 99 ++++++++++--------- 2 files changed, 60 insertions(+), 48 deletions(-) diff --git a/src/plugins/presentation_util/public/components/field_picker/field_picker.tsx b/src/plugins/presentation_util/public/components/field_picker/field_picker.tsx index 776f87a1d77ba..bde1a5a0c8d64 100644 --- a/src/plugins/presentation_util/public/components/field_picker/field_picker.tsx +++ b/src/plugins/presentation_util/public/components/field_picker/field_picker.tsx @@ -8,7 +8,7 @@ import classNames from 'classnames'; import { sortBy, uniq } from 'lodash'; -import React, { useEffect, useMemo, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { i18n } from '@kbn/i18n'; import { FieldIcon } from '@kbn/react-field'; @@ -41,6 +41,7 @@ export const FieldPicker = ({ selectableProps, ...other }: FieldPickerProps) => { + const [searchRef, setSearchRef] = useState(null); const [typesFilter, setTypesFilter] = useState([]); const [fieldSelectableOptions, setFieldSelectableOptions] = useState([]); @@ -90,9 +91,14 @@ export const FieldPicker = ({ [dataView, filterPredicate] ); + const setFocusToSearch = useCallback(() => { + searchRef?.focus(); + }, [searchRef]); + const fieldTypeFilter = ( setTypesFilter(types)} fieldTypesValue={typesFilter} availableFieldTypes={uniqueTypes} @@ -128,6 +134,7 @@ export const FieldPicker = ({ defaultMessage: 'Search field names', }), disabled: Boolean(selectableProps?.isLoading), + inputRef: setSearchRef, }} listProps={{ isVirtualized: true, diff --git a/src/plugins/presentation_util/public/components/field_picker/field_type_filter.tsx b/src/plugins/presentation_util/public/components/field_picker/field_type_filter.tsx index 429e53ab4f077..a26899fb3bcb1 100644 --- a/src/plugins/presentation_util/public/components/field_picker/field_type_filter.tsx +++ b/src/plugins/presentation_util/public/components/field_picker/field_type_filter.tsx @@ -7,6 +7,7 @@ */ import React, { useState } from 'react'; + import { i18n } from '@kbn/i18n'; import { EuiFilterGroup, @@ -15,7 +16,6 @@ import { EuiInputPopover, EuiContextMenuPanel, EuiContextMenuItem, - EuiOutsideClickDetector, EuiFilterButton, EuiPopoverTitle, EuiFilterButtonProps, @@ -25,15 +25,17 @@ import { FormattedMessage } from '@kbn/i18n-react'; export interface Props { onFieldTypesChange: (value: string[]) => void; - fieldTypesValue: string[]; - availableFieldTypes: string[]; buttonProps?: Partial; + setFocusToSearch: () => void; + availableFieldTypes: string[]; + fieldTypesValue: string[]; } export function FieldTypeFilter({ + availableFieldTypes, onFieldTypesChange, + setFocusToSearch, fieldTypesValue, - availableFieldTypes, buttonProps, }: Props) { const [isPopoverOpen, setPopoverOpen] = useState(false); @@ -61,48 +63,51 @@ export function FieldTypeFilter({ ); return ( - {}} isDisabled={!isPopoverOpen}> - - { - setPopoverOpen(false); - }} - fullWidth - input={buttonContent} - > - - {i18n.translate('presentationUtil.fieldSearch.filterByTypeLabel', { - defaultMessage: 'Filter by type', - })} - - ( - { - if (fieldTypesValue.includes(type)) { - onFieldTypesChange(fieldTypesValue.filter((f) => f !== type)); - } else { - onFieldTypesChange([...fieldTypesValue, type]); - } - }} - > - - - - - {type} - - - ))} - /> - - - + + { + setPopoverOpen(false); + }} + fullWidth + input={buttonContent} + // BLOCKED BY: https://github.com/elastic/eui/pull/6955 + focusTrapProps={{ + returnFocus: false, // we will be manually returning the focus to the search + onDeactivation: setFocusToSearch, + }} + > + + {i18n.translate('presentationUtil.fieldSearch.filterByTypeLabel', { + defaultMessage: 'Filter by type', + })} + + ( + { + if (fieldTypesValue.includes(type)) { + onFieldTypesChange(fieldTypesValue.filter((f) => f !== type)); + } else { + onFieldTypesChange([...fieldTypesValue, type]); + } + }} + > + + + + + {type} + + + ))} + /> + + ); }