From 044d7f61c7e507845eb476e9e306957ee0b9295c Mon Sep 17 00:00:00 2001 From: Oscar Kilhed Date: Wed, 26 Apr 2023 09:08:32 +0200 Subject: [PATCH] DataSourcePicker: fix flickering datasource dropdown (#67206) * fix flickering * refactor onClose/onOpen * do not set value of input, make the placeholder look like the value instead * Show search icon when the dropdown is open --------- Co-authored-by: Ivan Ortega --- .../components/picker/DataSourceDropdown.tsx | 103 +++++++++--------- 1 file changed, 49 insertions(+), 54 deletions(-) diff --git a/public/app/features/datasources/components/picker/DataSourceDropdown.tsx b/public/app/features/datasources/components/picker/DataSourceDropdown.tsx index 29c9c63b814..2ae3ac8900f 100644 --- a/public/app/features/datasources/components/picker/DataSourceDropdown.tsx +++ b/public/app/features/datasources/components/picker/DataSourceDropdown.tsx @@ -1,6 +1,5 @@ import { css } from '@emotion/css'; import { useDialog } from '@react-aria/dialog'; -import { FocusScope } from '@react-aria/focus'; import { useOverlay } from '@react-aria/overlays'; import React, { useCallback, useRef, useState } from 'react'; import { usePopper } from 'react-popper'; @@ -37,6 +36,7 @@ export function DataSourceDropdown(props: DataSourceDropdownProps) { const openDropdown = () => { reportInteraction(INTERACTION_EVENT_NAME, { item: INTERACTION_ITEM.OPEN_DROPDOWN }); setOpen(true); + markerElement?.focus(); }; const currentDataSourceInstanceSettings = useDatasource(current); @@ -45,13 +45,15 @@ export function DataSourceDropdown(props: DataSourceDropdownProps) { placement: 'bottom-start', }); + const onClose = useCallback(() => { + setOpen(false); + markerElement?.blur(); + }, [setOpen, markerElement]); + const ref = useRef(null); const { overlayProps, underlayProps } = useOverlay( { - onClose: () => { - setFilterTerm(undefined); - setOpen(false); - }, + onClose: onClose, isDismissable: true, isOpen, shouldCloseOnInteractOutside: (element) => { @@ -66,56 +68,46 @@ export function DataSourceDropdown(props: DataSourceDropdownProps) { return (
+
+ + ) : ( + + ) + } + suffix={} + placeholder={dataSourceLabel(currentDataSourceInstanceSettings)} + onFocus={openDropdown} + onClick={openDropdown} + onChange={(e) => { + setFilterTerm(e.currentTarget.value); + }} + ref={setMarkerElement} + > +
{isOpen ? ( - - - ) : ( - - ) - } - suffix={} - placeholder={dataSourceLabel(currentDataSourceInstanceSettings)} - onChange={(e) => { - setFilterTerm(e.currentTarget.value); - }} - ref={setMarkerElement} - > - -
-
- ) => { - setFilterTerm(undefined); - setOpen(false); - onChange(ds); - }} - onClose={() => { - setOpen(false); - }} - current={currentDataSourceInstanceSettings} - style={popper.styles.popper} - ref={setSelectorElement} - {...restProps} - onDismiss={() => {}} - > -
- - - ) : ( -
- } - suffix={} - value={dataSourceLabel(currentDataSourceInstanceSettings)} - onFocus={openDropdown} - /> -
- )} + +
+
+ ) => { + onClose(); + onChange(ds); + }} + onClose={onClose} + current={currentDataSourceInstanceSettings} + style={popper.styles.popper} + ref={setSelectorElement} + {...restProps} + onDismiss={onClose} + > +
+ + ) : null}
); } @@ -132,6 +124,9 @@ function getStylesDropdown(theme: GrafanaTheme2) { input { cursor: pointer; } + input::placeholder { + color: ${theme.colors.text.primary}; + } `, }; }